2013-06-25 93 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, 
}); 

問題是我試圖在加載數據之前訪問存儲。現在我在等待所有的數據被加載來訪問它,並且它工作。

相關問題