PDA

View Full Version : A Simple DataWriter



kesteb
12 Mar 2010, 10:54 AM
I haven't seen anything like this yet, which surprised me. Here is a simple DataWriter for those who want to have their parameters in HTTP POST fashion, instead of a glob of JSON or XML. It works for me, use at your own risk.




/*
* File: Ext.ux.data.HttpWriter.js
* Date: 11-Mar-2010
* By : Kevin Esteb
*
* A DataWriter that renders the parameters in standard HTTP POST fashion.
*
* kesteb(at)wsipc.org
*
* Use and abuse as you see fit, but please leave this attribution.
* Thank you.
*
*/

Ext.namespace('Ext.ux.data');

Ext.ux.data.HttpWriter = Ext.extend(Ext.data.DataWriter, {

constructor: function(config) {
Ext.ux.data.HttpWriter.superclass.constructor.call(this, config);
},

render: function(params, baseParams, data) {
Ext.apply(params, baseParams);
for (var x in data) {
params[x] = data[x];
}
},

createRecord: function(rec) {
return this.toHash(rec);
},

updateRecord: function(rec) {
return this.toHash(rec);
},

destroyRecord: function(rec) {
var data = {};
data[this.meta.idProperty] = rec.id;
return data;
}

});

moegal
13 Mar 2010, 7:07 AM
I have not looked at data writer for this reason. I will try soon.

Marty

TopKatz
15 Mar 2010, 3:47 AM
This is cool,

however this was less of an issue then the api, and that it uses a URI. I eventually worked around the URI issue by using the HTTP_SERVER_VARS['PATH_INFO'] variable in php. The data blob can just be decoded on the server side.

Katz

moegal
15 Mar 2010, 4:26 AM
Katz,

I was hoping to try this today, what is URI? Do you mean url?

BLOB? The data writer just post data to the server via post or get, right?

Marty

TopKatz
15 Mar 2010, 5:30 AM
URI is part of the URL. In the api with this it basically goes like :

your/path/file.php/someAction

So because Im not using a MVC I needed to read the someAction part to do my work. Using the PATH_INFO I'm able to use the "someAction" part and switch on that to handle the different acctions.

It does use POST or GET, it just uses a data object (blob) with sub objects in it. This UX breaks out the "data" blob into traditional post items.

Again, for me dealing with the data object was not a issue. Handling the api part was what took me some time to figure out how to deal with.

There is a good writer example in the grid section that I found helpful

Katz

moegal
15 Mar 2010, 5:52 AM
Thanks, I am not using MVC either.

Can I do something like:

update: {
url: 'mypage.asp',
method: "DELETE",
params:{
type: 'update'
}
}

Or add ?type=update to my url?

I will check out the grid example.

Marty

TopKatz
15 Mar 2010, 6:25 AM
I was unable to gt it to submit the "type" as a post param with out using a baseParam. Thats why I ended up using the path stuff. So in your api it would look like this:

update:'mypage.asp/updateSomeThing'

then you just switch on the "updateSomeThing" part

kesteb
15 Mar 2010, 12:46 PM
I wrote this because I wanted a consistent presentation of parameters to the back end. Having a special case of "data" that needs to be decoded, is not worth the effort in my opinion.