PDA

View Full Version : Ext.Store & dynamic params



adkent
6 Sep 2007, 12:00 PM
I'm trying to send some additional params to GET request (using HttpProxy), and have reviewing a number of the threads relating to the topic, however, I can't seem to add a dynamic paramter to the request. I've attempted to use ds.baseParams to add a a parameter, but they never sent across the wire.

Here's essentially what I'm doing


var ds = new Ext.data.Store({
// load using HTTP
proxy: new Ext.data.HttpProxy({method: 'GET', url: '/BH.Net/RESTService.aspx?action=doSearch'}),

reader: new Ext.data.XmlReader({
record: 'Item',
id: 'ItemKey',
totalRecords: 'Items/@TotalResults'
}, [
'Name','Address','City','State'
]),
remoteSort: true
});

// Override parameters -- my service doesn't use start/limit for paging
Ext.Ajax.on('beforerequest', function(ajax, options){
// rename parameters globally here (options.params)
options.params = {
"Start" : options.params.start,
"MaxResults" : options.params.limit,
"SortBy" : options.params.sort,
"SortDirection" : options.params.dir
};
});

ds.baseParams = {"foo" : "bar"}; // I don't see this on the wire
ds.load({params:{start:0, limit:25}});


I can't even get this simple example working. Ideally, I'd like to pass a dynamic parameter (name determined at run-time -- how to do that?).

fay
6 Sep 2007, 12:04 PM
baseParams is used in the store's config (http://extjs.com/deploy/ext/docs/output/Ext.data.Store.html#config-baseParams)

If you want 'dynamic' parameters when calling load, use params:


ds.load({params:{start:0, limit:25, foo: 'bar'}});

Animal
6 Sep 2007, 12:10 PM
The event you want to intercept is http://extjs.com/deploy/ext/docs/output/Ext.data.Store.html#event-beforeload

baseParams should work though. They should be included in every request.

adkent
6 Sep 2007, 3:10 PM
Thanks. I tried baseParams (as shown), and they're definitely not getting passed. I suppose I could just set the param in the event, but I would have thought that baseParams would have worked.

Related question -- how can I go about creating a dynamic parameter? In other words, I have 4 Ext.menu.CheckItem items in a group, and want to pass the one that is "checked" as a param (name:value). The value is obvious, but how can I bind the name?

catacaustic
6 Sep 2007, 4:51 PM
You were almost there. When you're using a Ext data store you don't need to have any special Ajax calls for it, just define the proper proxy and reader options in the stores config, and it does all that for you. As for setting the variables, use this...


ds.on('beforerequest', function() {
ds.baseParams = {
foo: "bar"
};
});
ds.load();
I've got that in my systems here, and it works just like it's supposed to. You don't need to set any event for "load" to have this work because the load event fires after the store has loaded, meaning that any changes made to the parameters there won't go with the request.

adkent
7 Sep 2007, 4:35 AM
A couple of things. First, I don't see a 'beforerequest' event for the Ext.data.Store. I do see a 'beforeload', however. I attempted to wire-up a dummy parameter in this event, as follows:



ds.on('beforeload', function() {
var foo="myparam";
ds.baseParams = {
foo: "bar"
};
});


Unfortunately, I didn't see this param on the wire. So, I unhooked my 'beforerequest' event handler for the Ajax call (Ext.ajax.on -- see initial post for actual code), just to see if this might be overriding everything -- that did the trick. However, now I can't override my start/limit parameter names, as I was doing this in the ajax beforerequest event.

Animal
7 Sep 2007, 4:39 AM
beforeload is passed the options object which contains params.

Just poke stuff into it.

http://extjs.com/deploy/ext-1.1.1/docs/output/Ext.data.Store.html#event-beforeload

paha
7 Sep 2007, 7:25 AM
Unfortunately, I didn't see this param on the wire. So, I unhooked my 'beforerequest' event handler for the Ajax call (Ext.ajax.on -- see initial post for actual code), just to see if this might be overriding everything -- that did the trick. However, now I can't override my start/limit parameter names, as I was doing this in the ajax beforerequest event.

this works fine for me:



ds.on('beforeload', function() {
ds.baseParams.param1 = 'value1';
ds.baseParams.param2 = 'value2';
});

dende1979
12 Sep 2007, 11:20 PM
this works fine for me:



ds.on('beforeload', function() {
ds.baseParams.param1 = 'value1';
ds.baseParams.param2 = 'value2';
});


Great!
I had the same problem and now It works!