2013-06-25 98 views
0

我试图从Web服务收到的JSON加载商店。但是,JSON中的所有数据都在商店中物品的“原始”列中... 我无法弄清楚为什么,我的代码似乎正确。 欢迎任何帮助。Sencha Touch 2.2从JSON加载商店,数据转到原始列

我的模型:

Ext.define('App.model.Node', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [ 
      { name: 'id', type: 'int' }, 
      { name: 'version', type: 'int' }, 
      { name: 'user_id', type: 'int' }, 
      { name: 'tstamp', type: 'date' }, 
      { name: 'changeset_id', type: 'int' }, 
      { name: 'tags', type: 'string' }, 
      { name: 'geom', type: 'string'} 
     ], 

     idProperty: 'id' 
    } 
}); 

我的商店:

Ext.define('App.store.NodeStore', { 
    extend: 'Ext.data.Store', 
    xtype: 'nodestore', 
    requires: [ 
     'Ext.data.proxy.Rest' 
    ], 
    config: { 
     model: 'App.model.Node', 
     storeId: 'nodeStore', 
     autoLoad: true, 
     proxy: { 
      type:'rest', 
      url:'http://localhost/server/nodes', 
      reader: { 
       type:'json', 
       rootProperty: 'nodes' 
      }, 
      noCache: false, 
      limitParam: false, 
      headers: {     
       'Accept' : 'application/json'     
      } 
     } 
    } 
}); 

我的JSON:

{ 
    "nodes": [ 
     { 
      "id": "454467", 
      "version": 6, 
      "user_id": 52015, 
      "tstamp": "2008-12-27 21:38:45", 
      "changeset_id": "634766", 
      "tags": "", 
      "geom": "0101000020E6100000409CD1A0B29321405455682096804740" 
     }, 
     { 
      "id": "454468", 
      "version": 8, 
      "user_id": 52015, 
      "tstamp": "2009-12-23 20:47:15", 
      "changeset_id": "3437205", 
      "tags": "", 
      "geom": "0101000020E6100000357C0BEBC69321409EC02ACD9C804740" 
     }, 
     { 
      "id": "454469", 
      "version": 7, 
      "user_id": 52015, 
      "tstamp": "2009-12-23 20:47:15", 
      "changeset_id": "3437205", 
      "tags": "", 
      "geom": "0101000020E6100000347914F8D4932140B8BBBD5AA4804740" 
     } 
    ] 
} 

而且当我做了

var nodeStore = Ext.getStore('nodeStore'); 
nodeStore.load(); 
console.log(nodeStore.getData()); 

我们可以看到下面的对象,我的数据项下的原始列...

Data in the store

+0

什么是关于“数据”标签中的数据?有没有一样的? –

+0

@LukasK。是的,_data,data和raw是一样的 –

+1

然后你可以忽略我认为的原始数据。原始数据总是显示所有数据加载(通过您的案例JSON)。在原始数据中,您可以看到已加载的字段。除了未在模型中定义的字段之外,这是模型中定义的字段。在你的情况下,它们是相同的,因为你定义了模型中的所有内容。 –

回答

1

我想通了,我的代码是正确的,唯一缺少的是在负载回调()函数:

nodeStore.load({ 
    callback: function(records, operation, success) { 
     console.log(records); 
     console.log(nodeStore.getCount()); 
     nodeStore.each(function(element) { 
      console.log(element.data.id); 
     }); 
    }, 
    scope: this, 
}); 

问题是我试图在加载数据之前访问存储。现在我在等待所有的数据被加载来访问它,并且它工作。

相关问题