PDA

View Full Version : Loading JSON into a Store from Ajax request



RyanPays
16 Dec 2012, 7:35 PM
I am trying to load some JSON data and it is throwing a Object doesn't support this property or method error.

This is the data in a JSON file i am loading and it is returning fine:


{ "data": [
{
"id": 1,
"source": "1445261",
"target": "1437043",
"sourceType": "user",
"redirectUrl": "http://www.google.co.uk",
"message": "this is a notification message",
"targetType": "user",
"messageType": "notification",
"sentDate": "1354758001",
"notificationType": "notification",
"parameters": "null",
"read": "false",
"readDate": 1354758001
}
]
}

This is the Store and associated ajax request:



var infoStagingStore = Ext.create('Ext.data.Store', {
model: 'SCB.RMWB.InfoBar.Model.Message',
storeId: 'Staging',
autoLoad: false,
pageSize: 10,
proxy: {
type: 'ajax',
reader: {
type: 'json',
root: 'data'
}
},
listeners: {
load: function(){
console.log('loaded');
}
}
});


Ext.Ajax.request({
url: '/rmwb/resources/js/app/infoBar/data/data.json',
timeout: 60000,
method: 'GET',
scope: this,
success: function(resp) {
console.log(resp.responseText);
var store = Ext.data.StoreManager.lookup('Staging');
store.loadRawData(Ext.JSON.decode(resp.responseText), true);
},
failure: function(resp, opts) {


},
callback: function(options, success, resp) {
}
});

evant
16 Dec 2012, 7:47 PM
Why are you doing it like that? Just load it via the proxy, doing a separate ajax request is just making more work.

RyanPays
16 Dec 2012, 8:35 PM
A fair point.

I tried this which is fine:



var infoStagingStore = Ext.create('Ext.data.Store', {
model: 'SCB.RMWB.InfoBar.Model.Message',
storeId: 'Staging',
autoLoad: true,
proxy: {
type: 'ajax',
url: '/rmwb/resources/js/app/infoBar/data/data.json',
reader: {
type: 'json',
root: 'data'
}
},
listeners: {
load: function(){
console.log('loaded');
}
}
});


But how do i load data into a pagingmemory store on page load from the server?

Would i need to load the data from the above store to a subsequent pagingmemory store.

The requirement is to pull historical data from the server and then accept new data via polling which will then be added to the pagingmemory store.

Apologies for not being clear on requirements.

RyanPays
16 Dec 2012, 8:47 PM
Would the following work?



var infoStagingStore = Ext.create('Ext.data.Store', {
model: 'SCB.RMWB.InfoBar.Model.Message',
storeId: 'Staging',
autoLoad: true,
proxy: {
type: 'ajax',
url: '/rmwb/resources/js/app/infoBar/data/data.json',
reader: {
type: 'json',
root: 'data'
}
},
listeners: {
load: function(){
console.log(this.getProxy().type);
this.getProxy().type = 'pagingmemory';
console.log(this.getProxy().type);
console.log('loaded');
}
}
});


So the store then becomes a paging memory type and then i can manually add records to the store.

I need the ability to page in memory the historical data and the new polling data as it comes in.