陳同學
陳同學

Reputation: 149

How to get value from Ext.data.Store ?

this is my code

var store = {
    user_store: new Ext.data.Store({
        autoLoad: false,
        proxy: new Ext.data.HttpProxy({
            url: 'a/b/c',
        }),
        remoteSort: true,
        baseParams: {

        },
        reader: new Ext.data.JsonReader({
            totalProperty: 'total',
            root: 'root',
            fields: ['roles']
        })
    })
};
store.user_store.load();​

this is my json

{"roles":"2"}

I wnat to ask. How do I get the roles's value is "2".

(PS:Sorry,my English is not very well.)

Upvotes: 8

Views: 36628

Answers (1)

Carl
Carl

Reputation: 760

If there is only one item in the response, you can add a callback function to the load method:

var store = {
    user_store: new Ext.data.Store({
        autoLoad: false,
        proxy: new Ext.data.HttpProxy({
            url: 'a/b/c',
        }),
        remoteSort: true,
        baseParams: {

        },
        reader: new Ext.data.JsonReader({
            totalProperty: 'total',
            root: 'root',
            fields: ['roles']
        })
    })
};
store.user_store.load(function(){
    this.getAt(0).get('roles')
});​

If the the response consist of several items like: [{"roles":"2"},{"roles":"1"}] you can iterate the store to retrieve all values.

store.user_store.load(function(){
    this.each(function(record){
       var roles = record.get('roles');
       // Do stuff with value
    });
});​

Upvotes: 12

Related Questions