Mr_Green
Mr_Green

Reputation: 41842

Select a value from a variable

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

Upvotes: 1

Views: 210

Answers (2)

sra
sra

Reputation: 23983

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.

Upvotes: 4

Evan Trimboli
Evan Trimboli

Reputation: 30092

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.

Upvotes: 2

Related Questions