1
votes

In Extjs, I am trying to select a value accrued from a variable Store.

enter image description here

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: [ ]

2
Store or store ? The first one with a up-cased 'S'.coderLMN
Sorry it was typo. it's Store @JinzhaoWuMr_Green
Try for(x in Store.data.items) console.log(x); to make sure the index is indeed 0 ?techfoobar
@techfoobar It is not showing anything because Store.data.items is [ ] (I don't know how).Mr_Green
I don't know if this will be helpful but try this Ext.getStore('store_id').data.items[0].data.accruedcclerv

2 Answers

4
votes

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
votes

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.