从json store extjs获取记录

cra*_*ies 3 javascript json extjs store

我有一个json存储加载,我需要从中获取一条记录.我用:getAt(index),find(),getById(),但没有结果.这是我的代码:

var appSettingReader = new Ext.data.JsonReader({     
                root: 'results',
              },[
                {name: 'id', type: 'int', mapping: 'id'},
                {name: 'projetId', type: 'int', mapping: 'projetId'},
                {name: 'resLevels', type: 'int', mapping: 'resLevels'},
                {name: 'maxResToLock',  type: 'int', mapping: 'maxResToLock'},
                {name: 'maxTimeToLock', type: 'int', mapping: 'maxTimeToLock'},
                {name: 'infosToPrint', type: 'string', mapping: 'infosToPrint'}
              ])

var appSettingStore = new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({
                        url: 'inc/getSettings.php',
                        method: 'POST'
                    }),
                baseParams:{task: "app"}, 
                reader : appSettingReader,
                sortInfo:{field: 'id', direction: "DESC"}
               })

appSettingStore.load(); 
Run Code Online (Sandbox Code Playgroud)

此代码返回undefined:

console.log(appSettingStore.getAt(0));

console.log(appSettingStore.find("id","1")); 
Run Code Online (Sandbox Code Playgroud)

这是从服务器返回的json字符串:

{success:true,"results":[{"id":"1","projetId":"1","resLevels":"1","maxResToLock":"40","maxTimeToLock":"10","infosToPrint":"1_2_3_5","hotlineMail":"admin@app.com"}]}
Run Code Online (Sandbox Code Playgroud)

我也测试了这段代码:

var records = new Array()       
var test = appSettingStore.each(function(rec){
            records.push(rec)
         })
console.log(records)
Run Code Online (Sandbox Code Playgroud)

我得到一个空数组!

PS:这个商店不受任何组件的约束; 我只想读写它.

小智 7

您需要在商店上放置一个回调,它将在加载后触发.然后,您可以根据需要使用数据.

store.load({
    callback : function(r, options, success) {
        console.log(r.data)
    }
})
Run Code Online (Sandbox Code Playgroud)