PDA

View Full Version : Passing numeric "0" to Ajax Requests



alexdemers
10 Jun 2009, 6:20 AM
Hey,

I tried passing 0 (as numeric type) to baseParams of a JSONReader and it doesn't get sent.


reader.on('beforeload', function(){
reader.baseParams( {mode: 'new_user', id: UserID, name: UserName, active:UserActive } );
}And the request being made:

http://localhost/xmlrpc.php?mode=new_user&id=123345&name=alexdemers&active=In my case, UserActive is 0 numeric but I need to pass it.

I know, I can parseInt() or concatenate with +'' and it will work but I should be able to pass 0 as numeric for whatever the application is. Also, 1 and -1 works. Anything except 0. Does JavaScript see 0 as being false, thus not passing it? Should it use the identity operator for this, so that false and 0 are not the same, if it's the case of Ext seeing 0 as false.

Is this a bug or it is intended to be?

Thanks.

Animal
10 Jun 2009, 6:25 AM
Yes, Javascript sees 0 as false in an if test.



try



reader.on('beforeload', function(){
reader.baseParams( {mode: 'new_user', id: UserID || new Number(0), name: UserName, active:UserActive } );
}

Animal
10 Jun 2009, 6:28 AM
But



Ext.urlEncode({foo:0})


Works for me in 3.0 and 2.2.1, so what version are you on?

alexdemers
10 Jun 2009, 6:28 AM
Yes, Javascript sees 0 as false in an if test.



try



reader.on('beforeload', function(){
reader.baseParams( {mode: 'new_user', id: UserID || new Number(0), name: UserName, active:UserActive } );
}



Yes it worked using " || new Number(0)" and so does the other ways I described in my earlier post. The reason I'm asking this is is this a bug or intended to be?

alexdemers
10 Jun 2009, 6:29 AM
Ext 3.0RC1

alexdemers
10 Jun 2009, 6:30 AM
So, should Ext integrate urlEncode for its Readers... without the users having to do the encoding for each requests they do in an application?

jay@moduscreate.com
10 Jun 2009, 6:34 AM
try RC2

Animal
10 Jun 2009, 6:34 AM
that code



reader.baseParams(...)


What is it? There is no baseParams method.

alexdemers
10 Jun 2009, 6:35 AM
that code



reader.baseParams(...)
What is it? There is no baseParams method.


I meant JSON Store. My bad.

Yes I'm downloading RC2 right now, didn't know they updated it. *subscribes to RSS feed*

Animal
10 Jun 2009, 6:38 AM
You mean



myStore.on('beforeload', function(store, opts) {
Ext.apply(opts.params, {
mode: 'new_user',
id: UserID,
name: UserName,
active: UserActive
})
});


?

alexdemers
10 Jun 2009, 6:42 AM
Yes, the store. I confused it on the forums but my code is correct.

Animal
10 Jun 2009, 6:52 AM
where is the baseParams method? On what class?

alexdemers
10 Jun 2009, 6:56 AM
where is the baseParams method? On what class?

I guess it changed to setBaseParams. But it still does work using baseParams. That's not my issue.

Animal
10 Jun 2009, 6:59 AM
Whats the issue then?

Animal
10 Jun 2009, 7:00 AM
Nope, there's no setBaseParams method.

What you need is a beforeload listener which adds some params to the params option as a demonstrated.

What your code did is a mystery. You developing on IE or something?

alexdemers
10 Jun 2009, 7:14 AM
Firefox 3.0.10

The application is huge and I'm upgrading it from 1.1.1 to 3.0 (RC2). My issue is passing 0 as numeric in any parameters of the Ajax call.


var UserActive = 0;
store.on('beforeload', function(){
store.baseParams( {mode: 'new_user', id: UserID, name: UserName, active:UserActive } );
}Again this will request exactly this:

http://localhost/xmlrpc.php?mode=new_user&id=123345&name=alexdemers&active=The UserActive variable is not getting requested because it is being seen as false and thus not passing it.

My question is:
Is it a bug that 0 (as numeric) is not getting passed (unintentional) or it is intentional to not pass 0 as numeric because only "==" was use in the comparison?

I know I didn't show you the Store setup and the Reader and all. That's not the issue.

Animal
10 Jun 2009, 7:19 AM
It must be your baseParams method which you must have added in an override. Show us it.

hendricd
10 Jun 2009, 7:21 AM
WIthout RC2 SVN access you'll need to do something like this:



store.on('beforeload', function(store, options){
options.params || (options.params = {}); //may not be defined yet
Ext.apply( options.params , {
mode: 'new_user',
id: UserID,
name: UserName,
active:String(UserActive)
});
});

Animal
10 Jun 2009, 7:23 AM
Ah, so



// make sure options has a params key
options = Ext.applyIf(options||{}, {
params: {}
});


went in post RC2?

hendricd
10 Jun 2009, 7:29 AM
No, but at this point, asserting options.params yourself has been a long-standing practice (for me anyway) for all Ext releases.

I was referring primarily to the '0 vs false' encoding problem, thus the temporary String(value) solution. That, I believe, was fixed in SVN since RC2. ;)

alexdemers
10 Jun 2009, 7:29 AM
Never mind all that. RC2 fixed it I guess. Now it's using POST instead of GET as I can see. And passing numeric 0 works.

Everything works with store.baseParams(object) go figure.

Animal
10 Jun 2009, 7:31 AM
Yuo don't need to stringify it. It just works. I just tested



g.store.on('beforeload', function(s,o){o.params.param = 0});


On http://extjs.com/deploy/ext-3.0-rc2/examples/grid/paging.html

and the params sent on paging were:

_dc 1244647764517
callback stcCallback1003
dir DESC
limit 25
param 0
sort lastpost
start 50

Animal
10 Jun 2009, 7:33 AM
Never mind all that. RC2 fixed it I guess. Now it's using POST instead of GET as I can see. And passing numeric 0 works.

Everything works with store.baseParams(object) go figure.

You have something way whacky going on then.

Because baseParams is a property which is used to add parameters to each request.

If you have added it to your Store as a function, then I imagine it will be iterating over it as an object and adding what it finds as parameters!

alexdemers
10 Jun 2009, 7:34 AM
I just saw that baseParams is a public property. All of my questions answered. Thanks guys.

hendricd
10 Jun 2009, 7:34 AM
Encoding problem obviously was RC1 then.

Press on ;)

alexdemers
10 Jun 2009, 7:39 AM
Haha, I just realised that my code actually is store.baseParams = { }; and not store.baseParams({}); My bad.