2011-11-17 54 views
2

如何从函数外部的Ext.data.Store获取JSON数组? 代码:从外面的Ext.data.Store获取JSON数组?

var store = new Ext.data.Store({ 
       model: 'nested' + type, 
       proxy: { 
        type: 'ajax', 
        url: '/Grid/GetDetailed?InvoiceId=' + $(row).attr('id'), 
        reader: { 
         type: 'json', 
         root: 'items', 
         totalProperty: 'totalCount' 
        } 
       } 
      }); 
      store.load(); 

而且我想用这样的:

store.getAt(0); 

但它是不确定的。有人说它是异步的ajax beacaouse。

回答

6

如果在store.load()被调用之后立即使用store.getAt(0),那么问题在于负载是异步的,因此您应该使用负载的回调方法来解决此问题。

store.load({ 
    scope : this, 
    callback: function(records, operation, success) { 
     //here the store has been loaded so you can use what functions you like 
     store.getAt(0); 
    } 
}); 
0

在创建商店时使用Ext.create而不是new关键字,并为商店定义storeId。然后您可以使用Ext.getStore()方法检索商店。

+0

它没有帮助! – Hadas

+0

“store”本身是未定义的,还是'getAt(0)'调用返回未定义的记录?在后一种情况下,原因可能是@nscrob写道的。您需要确保在尝试访问其内容之前加载了商店。 – Tommi

+0

谢谢!你是对的,我没有很好地解释自己 – Hadas

0

你也可以把它做的工作如下:

//This function will be called only after the store has been loaded successfully. 
store.on('load',function(this, records, successful, eOpts){ 
    store.getAt(0); 
}, this);