jeudi 24 avril 2014

How to load multiple stores from a nested JSON response

Hi,

I need to be able to load a set of stores with JSON data from a nested response


Currently the stores all have proxies and readers configured to consume the data they need in responses that contain the data in the root of the response.


So a typical store I have is:



Code:



Ext.define('PinpointersTouch.store.CurrentLocationStore', {
extend: 'Ext.data.Store',


requires: [
'PinpointersTouch.model.CurrentLocation'
],


config: {
model: 'PinpointersTouch.model.CurrentLocation',
storeId: 'CurrentLocationStore',
proxy: {
type: 'ajax',
enablePagingParams: false,
url: '/location/GetLastReportedEvents.json?FieldSet=1',
reader: {
type: 'json',
idProperty: 'UntID',
rootProperty: 'data'
}
},
sorters: [
{
direction: 'DESC',
property: 'EventDT'
},
{
property: 'UnitName'
}
]
}
});

Which happily loads data in the format Simple enough...

However, I have occasions in the application where I need to make a request that returns multiple result sets in a nested fashion, so the server can currently return:



Code:



{"data":{
"LastReportedEvents": {
"data":[{...}]
},
"Tasks": {
"data":[{...}]
}
}}

I am kicking off an AJAX request and in the success handler trying to add the parts of the response into each store as required:

Code:



success: function(result) {
var response = Ext.JSON.decode(result.responseText), lreRows;
if(response.success === true) {
locationStore.addData(response.data.LastReportedEvents);
taskStore.addData(response.data.Tasks);
}
}

I can see changes in the data when inspecting the LastReportedEvents and Tasks objects, but when the addData() methods are called, my UI does not update.

I tried using the add() method instead on my stores, but I end up with duplicate rows in my UI (Lists) for already existing rows of data, I thought add() would overwrite any existing models that shared the same id's as data elements included in the response.


I have seen the docs on associations, but, none of the nested responses contain data that are related to one another.


Can anyone advise further please?





Aucun commentaire:

Enregistrer un commentaire