PDA

View Full Version : How to read data from store sync GET variables



iPrezentr
18 Jan 2013, 4:08 AM
Hi,

I am trying to save unsynced data records to the server using the JSONP Proxy.
I get the data from a form, references the store in my app and calls the .add() method on it passing the form input data as a record.

I am using the CRUD api configuration ability of the proxy.

Everything is sent to the server, but in a problematic way.

The record looks like this:

{email: "test@mail.com", lastname: "Jameson", firstname: "Jack"}

But the request url looks like this:

http://myurl/control/user/create?_dc=1358510327507&records=firstname=Jack&lastname=Jameson&email=test%40mail.com&callback=Ext.data.JsonP.callback4

resulting in an object on the server looking like this:


{
_dc: '1358510327507',

records: 'firstname',

lastname: 'Jameson',

email: 'test@mail.com',

callback: 'Ext.data.JsonP.callback4'
}



I.e. the firstname variable is treated as the records' value. Why does the proxy build the url like this? And I suppose this would be even more problematic if multiple records were to be synced.
Ajax is not a possibility as I am working on localhost and the server is remote, so I would face cross-domain issues.

Please help me understand what is going on here.
Thank you!

existdissolve
18 Jan 2013, 5:49 AM
Take a look at the recordParam option of the JsonP proxy: http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.proxy.JsonP-cfg-recordParam

While I've not ever used it, it seems to explain what's going on. You will most likely need to configure your writer to modify how the store will produce the JsonP request.

iPrezentr
18 Jan 2013, 6:37 AM
Thanks for your reply!

Yes I have been looking at this parameter already and I'm afraid that this only controls the keyword for accessing the records.

I see what you mean in regards to configuration of the writer. As far as I can tell, though, the only way to bypass this problem via the writer is to set the "encode" property to true and define the "root" property. This would cause a separate variable (called whatever you define in root property) to be appended to the request with the string-encoded record as its value.
The original record parameters, though, would still be sent along with the request, essentially resulting in a doubling of data in each request.

The request would then look like this:

http://url.com/control/user/create?_dc=1358519555485&test=firstname=Jack&lastname=Jameson&email=test%40mail.com&data=%7B%22firstname%22%3A%22Jack%22%2C%22lastname%22%3A%22Jameson%22%2C%22email%22%3A%22test%40mail.com%22%2C%22department%22%3A%22%22%2C%22_id%22%3A%22%22%2C%22rights%22%3A0%7D&callback=Ext.data.JsonP.callback4

While this does allow me to access all the needed variables, it does seem a bit clunky to me, and more like a hack than the right way to do it?

It also means that I need to set these properties for all future proxies in my project, which isn't to desirable to me either.

Am I missing something else maybe?

existdissolve
18 Jan 2013, 4:41 PM
To overcome similar issues, I've extended the writer to create the format of request params that I want. I'll share an example if I can find it.

iPrezentr
19 Jan 2013, 5:24 AM
I'd appreciate that. Thank you

existdissolve
21 Jan 2013, 7:47 AM
Ok, I basically created a new writer which extends Ext.data.writer.Json. In my writer, I override the writeRecords() method:



writeRecords: function( request, data ) {
Ext.Object.each( data[0], function( key, value, object ) {
request.params[ key ] = value;
});
return request;
}

This is a simplified version of what I'm doing, but it gives the basic gist. That is, I simply loop over each of the fields, and add a key/value pair for each field.

So instead of bundling everything in a root (or not adding a root at all), it creates a param for each field.

Obviously, you'll want to modify this significantly to deal with submitting complex structures like arrays and objects, but this should get you started. I'd begin by looking at the writeRecords() method of the Ext.data.writer.Json class, and go from there with creating your own writer.

Good luck!

iPrezentr
21 Jan 2013, 8:35 AM
Thank you very much. This is really helpful!

I Think I'll definitely go with this approach too. I still can't help but wonder, though, that this is a hack that I need to apply because I have have a hole in my basic knowledge hot to properly develop with this framework.

Still I appreciate your assistance a lot!

Marc