问题
In Extjs, I am trying to select a value accrued
from a variable Store
.
I tried:
var newStore = Store.data.items[0].data.accrued;
The above one is not working. I am getting error:
Uncaught TypeError: Cannot read property 'data' of undefined
Whenever I add items[0]
, it is making the variable undefined.
When I checked:
var newStore = Store.data.items.length;
output is: 0 //?
But I can see that there are items in Store
variable here as shown in the above image. Anyway, I just want to get the value of accrued
from the variable Store
.
I also checked:
var newStore = Store.data.items;
output is: [ ]
回答1:
Hey Mr_Green you should look into the API sometimes, cause this is really straight forward ;) Store load event
Store.on('load', function(store,records){
records[0].data.accrued;
}, this, {single:true})
Update - a bit more in depth
The tiny word on applies a listener for a event to a component. It will be available for classes that mixin Ext.util.Observable where the last argument I applied identifies that this listener should get removed after it get called once. This is useful for anonymous listeners cause you cannot unregister them otherwise.
load( this, records, successful, eOpts )
Parameters
- this : Ext.data.Store
- records : Ext.data.Model[] An array of records
- successful : Boolean True if the operation was successful.
- eOpts : Object The options object passed to Ext.util.Observable.addListener.
So the records argument contains all the records that are returned by the current load operation.
回答2:
Store loading is asynchronous. When you are logging, the values aren't loaded yet.
The console always shows the most up to date version of the object. You need to listen to the store load event.
来源:https://stackoverflow.com/questions/15377900/select-a-value-from-a-variable