PDA

View Full Version : Problem loading data with store.loadData()



kmgonyou
3 Nov 2012, 11:16 AM
I have the following code that when I set a breakpoint in the code and continue and do a console.log I can see data in the store object. If I just let it run and then console.log the store object the data is empty.

Ext.create('CA.store.DiscussionsByModule', {
storeId: 'DiscussionsByModule'
});


Ext.Ajax.request({
url: base_url() + 'discussions/getDiscussionsByModule',

params: {
'moduleId': module.data.moduleId
},

callback: function(options, success, response)
{
if (success) {

var json = Ext.decode(response.responseText);

if(json.success) {

//var data = [{
// 'discussionId': json.discussions[0].discussionId,
// 'ownerId': json.discussions[0].ownerId,
// 'nodeId': json.discussions[0].nodeId
//}];

Ext.getStore('DiscussionsByModule').loadData(json['discussions']);
//Ext.getStore('DiscussionsByModule').loadData(data);

}
}
},
scope: this
});

vietits
3 Nov 2012, 3:19 PM
Where is the point you put the code of console.log() to dump out the value of the store? I think this is the problem of asynchronous loading, requests need time to get back response from server. When you break the execution of your code, ajax request has enough time to complete its request. However, when you let it run, the speed of execution is very fast and execution reachs to the code of console.log(store) before request finishes its loading.

kmgonyou
3 Nov 2012, 3:28 PM
I was thinking the same thing. I forgot to mention that I put a button on the view to test. Using Firebug I would wait for the post to complete and that the response was succesful and JSON data was populated. I then would click the button to do the console.log().

vietits
3 Nov 2012, 3:40 PM
Then, has your problem been solved?

kmgonyou
3 Nov 2012, 4:17 PM
No. I was just providing addition information on my troubleshooting process.

vietits
3 Nov 2012, 5:11 PM
So you should wait for completing request to do the next thing.

kmgonyou
3 Nov 2012, 5:18 PM
Sorry I am new to Ext Js. Isn't that what the callback trigger is for?

callback: function(options, success, response)
{
if (success) {

var json = Ext.decode(response.responseText);

if(json.success) {

Ext.getStore('DiscussionsByModule').loadData(json['discussions']);

}
}
},
scope: this
});

vietits
3 Nov 2012, 5:35 PM
The request callback is for handling response from request and you used it right. So, you mean even with this, store is still empty? Try to answer my questions (marked in red color) in your code below:


Ext.create('CA.store.DiscussionsByModule', {
storeId: 'DiscussionsByModule'
});


Ext.Ajax.request({
url: base_url() + 'discussions/getDiscussionsByModule',
params: {
'moduleId': module.data.moduleId
},
callback: function(options, success, response) {
if (success) {
var json = Ext.decode(response.responseText);


console.log('json', json); // what do you get here?


if(json.success) {
//var data = [{
// 'discussionId': json.discussions[0].discussionId,
// 'ownerId': json.discussions[0].ownerId,
// 'nodeId': json.discussions[0].nodeId
//}];


Ext.getStore('DiscussionsByModule').loadData(json['discussions']);
//Ext.getStore('DiscussionsByModule').loadData(data);
console.log(Ext.getStore('DiscussionsByModule')); // what does store get here?
}
}
},
scope: this
});

BTW, you should put your code between CODE tag for readability.

kmgonyou
4 Nov 2012, 4:39 AM
Thanks for the help. I will have to respond to your questions on Monday. Sorry about the code tag.

kmgonyou
5 Nov 2012, 5:41 AM
I figured out what I was doing wrong. Since I was sending additional parameters it was doing a POST instead of the default GET. It was staring me right in the face the whole time in Firebug. Thanks for the help and sorry to waste your time vietits! (http://www.sencha.com/forum/member.php?362966-vietits)