PDA

View Full Version : How to load ajax POST response to a data store?



philr
24 Aug 2012, 5:23 AM
Hi everyone.

Is it possible to load an ajax POST response to a data store?

Here is what I have.

Ext.Ajax.request ({
url: 'some/url/location1',
method: 'POST',
headers: {'Content-Type': 'application/json'},
jsonData: {
"customerName" : "ExtUser"
},
success: function(response){
Ext.StoreManager.lookup('customerNameStore').load(response);
}
});
var customerNameStore = Ext.create('Ext.data.Store', {
model: 'customerNameModel',
autoLoad: false,
storeId: 'customerNameStore',
?????? <---- this is where I'm lost as to what type of store to use.
});

I've seen a lot of examples that are using a proxy, but I'm not sure of the type to use, as I'm already making the ajax request elsewhere.

I'm sure there has to be an easier way to do this, but I've seen far to many examples and complicated the solution. :-/

vietits
24 Aug 2012, 5:33 AM
Try this:


var customerNameStore = Ext.create('Ext.data.Store', {
model: 'customerNameModel',
autoLoad: false,
storeId: 'customerNameStore',
proxy: 'memory'
});

Ext.Ajax.request ({
url: 'some/url/location1',
method: 'POST',
headers: {'Content-Type': 'application/json'},
jsonData: {
"customerName" : "ExtUser"
},
success: function(response){
var data = Ext.decode(response.responseText)
Ext.StoreManager.lookup('customerNameStore').loadData(data);
}
});

Farish
24 Aug 2012, 5:36 AM
yes its possible. In the success of the AJAX request:


success: function(response, opts)
{
var data = Ext.decode(response.responseText, true);
store.loadData(data);
}