PDA

View Full Version : How to specify jsonData as option in store.load()?



dbassett74
16 Jul 2009, 2:13 PM
It seems that the JsonStore.load() does not accept jsonData as an option. I can't use the params because it doesn't send it as json formatted. Here is the code I'm trying to use, but it doesn't pass the jsonData to the web service. When initially setting the jsonData for the store, it works, but I need to be able to change the jsonData at run time based on user actions.



var options = {
jsonData: {
projectNo: '0120'
}
};
grid.store.load(options);


Any help would be appreciated.

tryanDLS
16 Jul 2009, 2:21 PM
loadData instead of load

dbassett74
16 Jul 2009, 2:28 PM
Don't think that's what I'm looking for. Perhaps I didn't fully explain how I was using the store. I am using a proxy. Here is how the store is defined:



var store = new Ext.data.JsonStore({
autoLoad: false
, proxy: new Ext.data.HttpProxy({
method: 'POST'
, jsonData: {id: '1'} <------- I need to change this at any time
, url: 'process.asmx/getFiles'
, headers: { 'Content-Type': 'application/json;charset=utf-8'}
})
, root: 'd' //the root node of the json object - always 'd' from asp.net web service
, id: 'id' //the record id
, fields: ['id', 'name']
});


So how would I reload the above store and specify what the jsonData should be?

tryanDLS
16 Jul 2009, 2:36 PM
Your question doesn't make sense. HttpProxy doesn't have a jsonData config. The data comes from the result of the proxy's request - you can't inject data into it.

Are you trying to pass additional parms with the request? That's done via params/baseParams on the store

dbassett74
16 Jul 2009, 2:48 PM
I use it in several places to pass jsonData to the web service. It definitely works. And according to the documentation for Connection, which is what HttpProxy uses, jsonData IS a valid parameter.

mjlecomte
16 Jul 2009, 4:20 PM
What about declaration:


store = new Ext.data.JsonStore({
autoLoad: false,
proxy: new Ext.data.HttpProxy({
method: 'POST',
jsonData: {
id: '1'
},//
url: 'test.php',
headers: {
'Content-Type': 'application/json;charset=utf-8'
}
}),
root: 'd', //the root node of the json object
id: 'id', //the record id
fields: ['id', 'name']
});


Initial load test:


store.load();


modify proxy's jsonData property


store.proxy.conn.jsonData = {
foo: 'bar'
}


Another load test:


store.load();

dbassett74
16 Jul 2009, 4:27 PM
Yep, that did the trick. Thanks!

christocracy
16 Jul 2009, 5:03 PM
It seems that the JsonStore.load() does not accept jsonData as an option.

I know exactly what you mean. When adding REST support, I added support for using the jsonData param of Ext.Ajax#request.

Here's a little block from HttpProxy#doRequest


if (typeof(params[reader.meta.root]) === 'object') {
o.jsonData = params;
} else if (params.xmlData) {
o.xmlData = params.xmlData;
} else {
o.params = params || {};
}


I just added the support for xmlData yesterday; that's related to some work I'm doing to support XmlWriter which hasn't yet been committed.

The bit in red though, I'm thinking of changing to params.jsonData for consistency (that data is appended to the params in Ext.data.DataWriter). Therefore, I think it's valid to expect to be able to add jsonData as in the code snippet you posted.



var options = {
jsonData: {
projectNo: '0120'
}
};


Currently though, I think you could trick the HttpProxy into sending jsonData by simply defining an object {} in the params using a key matching your JsonReader's "root" config property.

eg:



var reader = new Ext.data.JsonReader({
root: 'my_data'
});
var hproxy = new Ext.data.HttpProxy({
url: 'my_url',
listeners: {
beforeload: function(proxy, options) {
options.params.my_data = {
projectNo : '0120'
}
}
}

mjlecomte
16 Jul 2009, 5:19 PM
Is there likelihood that someone would send jsonData and xmlData?

I posted separately about this about the various reader files which store an arrayData, jsonData, or xmlData property depending on the reader type.

My contention is why all the renaming? Why not just have the reader store readData? That way the code can be more loosely coupled and just refer to readData when it wants to access that property regardless of what the reader type was. (granted each one is unique to it's data format, but I don't see the need to change the property name because of that ... we don't have xml_url to configure an XML store do we?)

Same goes for this particular thread (perhaps?). Do we want to reconfigure the store or proxy to have a unique property of jsonData, xmlData, params, or baseParams? Or do we really just care to say hey, these are my "params" I want sent to the server and I want to send them in format:'foo' and with method:'POST' or 'GET'.

christocracy
16 Jul 2009, 5:34 PM
My contention is why all the renaming?

Yes, I think you're right MJ. As I expand into XmlWriter, it's becoming more clear.