PDA

View Full Version : Process extjs store proxy data after data received



jgate
6 Feb 2013, 11:39 AM
Hi all,

I am using extjs sencha store for storing data. I make a proxy call to a web-service to get data. I refresh the data using store.load() function.

Here is how I get the data when a button is clicked in UI.

var store = Ext.getStore('myStore');
store.proxy.url = "ws/getData.php/users?mydata=city";
store.load();

I am looking to edit the data that is received before it is given to the grid.I know about the load event, but this function is executed after the load is completed and data is populated with the current data. I am looking to see how I can edit the returned data from web-service before it is assigned to the store. Basically data returned from my webservice is in different format than the format we give to extjs datagrid. So, want to do a data operation before we give to the grid.

I tried this method. I override read function inside the proxy and changed the content of the returned data.

proxy: {
type: 'rest',
url: 'ws/getData.php/users?mydata=city',
reader: {
type: 'json',
root: 'campaigns',
successProperty: 'success'
},
read: function (operation, callback, scope) {
var thisProxy = this;
console.log("Read method called for the campaign");
var cityInfo = [{
name: 'New Jersey',
location: 'East Coast'
}, {
name: 'California',
location: 'West Coast'
}];
operation.resultSet = new Ext.data.ResultSet({
records: cityInfo,
total: cityInfo.length
});
if (typeof callback == "function") {
callback.call(scope || thisProxy, operation);
}
}
}

This doesn't seem to work.

I am using sencha 4.1.3 version. Hope you can let me know how I can do this.

Thanks

Arg0n
6 Feb 2013, 1:11 PM
Easy going ^^
You would need to define a model and write it into the "model (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-cfg-model)" config of the store. Then you can use the convert (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Field-cfg-convert)method of the model field.

Exaaaample:



Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstname', type: 'string'},
{name: 'lastname', type: 'string'},
{name: 'fullname', type: 'string', convert: function(value, record) {
return record.get('firstname') + ' ' + record.get('lastname');
}}
]
});


This would set the fullname by adding both strings =)

sdt6585
6 Feb 2013, 1:16 PM
I would suggest overriding the load function of the store entirely and not calling the parent method. Then inside your load function, do a regular Ext.Ajax.request call, decode the data, edit whatever you want, then either create model instances or just anonymous objects and add them to the store using store.add(). I do something similar for the tree store. If you need an example of it implemented, let me know.

EDIT
Just saw Arg0n's post. If you can do what you need in the model, that's definitely the way to go.

jgate
12 Feb 2013, 10:54 AM
@sdt6585 (http://www.sencha.com/forum/member.php?397944-sdt6585) - I would love to see a code sample that I can use to override the load function.

jgate
12 Feb 2013, 11:04 AM
Arg0n (http://www.sencha.com/forum/member.php?409369-Arg0n) - Thanks.

Basically, the JSON structure has lot of extra fields and parameters, which we don't want to use for the store. If the JSON format itself is fully different, and I would need to a decoding to get the values, will this type of method help ?

juger
12 Feb 2013, 12:09 PM
Instead of using a proxy, why don't you do the AJAX request by hand and then load the contents into the store manually. You can use something like this:


Ext.Ajax.request({ url: 'requesturl.ashx',
success: function(response) {
//do your processing here and load the data into the store by hand

}

});

alphaquest86
7 May 2013, 9:45 PM
Instead of using a proxy, why don't you do the AJAX request by hand and then load the contents into the store manually. You can use something like this:


Ext.Ajax.request({ url: 'requesturl.ashx',
success: function(response) {
//do your processing here and load the data into the store by hand

}

});

Hi juger,
can u give an example of how you store the data from the xml response into the store manually?