PDA

View Full Version : chrome console can show answer,but alert() can't?



softball
22 May 2014, 4:43 PM
Dear?
I have the sample code as below:

I can use the consloe command of chrome debug to see
userStore.getCount() ==>show 2
userStore.getAt(0)

but?when I use alert(userStore.getCount()); in the Ext.onReady();
it show 0, not 2 ?

why????
THX



<script>
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
]
});

var userStore;
Ext.require('Ext.data.Store');
Ext.onReady(function() {
userStore = Ext.create('Ext.data.Store', {
model: 'User',
autoLoad: true,

proxy: {
type: 'ajax',
url: 'users.json',
reader: {
type: 'json',
root: 'usersData',
successProperty: 'success'
}
}
});

//alert(userStore.getCount());
});
</script>

----users.json
{
success: true,
usersData: [
{id: 1, name: 'Ed', email: '[email protected]'},
{id: 2, name: 'Tommy', email: '[email protected]'}
]
}

mitchellsimoens
23 May 2014, 3:13 AM
An ajax request is asynchronous so at the time of the alert, the request is still pending.

softball
25 May 2014, 4:30 PM
Dear mitchellsimoens: Thx for your help. Could you tell me how to show the alert in this ajax request case?

evant
25 May 2014, 4:33 PM
Listen to the load event on the store.

softball
25 May 2014, 5:09 PM
Dear evant?

I add the code to store:


listeners: {
load: function () { alert(userStore.getCount()); }
},


it' work, thx!