2013-01-08 44 views
0

我創建像煎茶觸摸店 - 幻影數據

Ext.define('MyApp.model.ContainerDetailsModel', { 
extend: 'Ext.data.Model', 
alias: 'model.ContainerDetailsModel', 
config: { 
    fields: [ 
     { 
      name: 'id', 
      allowNull: false, 
      type: 'string' 
     }, 
     { 
      name: 'container_types_id', 
      type: 'string' 
     } 
    ] 
    } 
}); 

一個模型,這樣

Ext.define('MyApp.store.ContainerDetailsStore', { 
extend: 'Ext.data.Store', 

requires: [ 
    'MyApp.model.ContainerDetailsModel' 
], 

config: { 
    model: 'MyApp.model.ContainerDetailsModel', 
    storeId: 'ContainerDetailsStore', 
    proxy: { 
     type: 'ajax', 
     enablePagingParams: false, 
     url: 'hereIsServiceUrl', 
     reader: { 
      type: 'json' 
     } 
    } 
} 
}); 

商店現在某處應用程序,我試圖讓一個記錄,如:

var detailsStore = Ext.getStore("ContainerDetailsStore"); 
detailsStore.load(); 
var detailsRecord = detailsStore.last(); 

但它給了我未定義。由服務返回的json是好的,它在不同的地方使用它作爲列表的源。我已經嘗試將allowNull更改爲true,但源代碼中沒有空id。我嘗試設置類型爲'int',結果相同。

所以我試圖

console.log(detailsStore); 

結果是這個樣子(只是重要的價值):

Class { 
... 
loaded: true, 
data: Class { 
    ... 
    all: Array[1] { 
     length: 1, 
     0: Class { 
      container_types_id: "1", 
      id: "726", 
      .... 
     } 
    ... 
    } 
    ... 
}, 
... 
} 

在同一個地方

console.log(detailsStore.data); 

收益(因爲它應該):

Class { 
    ... 
    all: Array[1] { 
     length: 1, 
     0: Class { 
      container_types_id: "1", 
      id: "726", 
      .... 
     } 
    ... 
    } 

但(下一行)

console.log(detailsStore.data.all); 

回報

[] 

而且它的空數組。當我嘗試從商店的任何方法,它說商店是空的。 我一個接一個地寫了console.log()行 - 所以肯定它們不會在它們之間改變(我也嘗試使用不同的順序或組合)。

我的瀏覽器是谷歌瀏覽器23.0.1271.97米 我用煎茶從https://extjs.cachefly.net/touch/sencha-touch-2.0.1.1/sencha-touch-all-debug.js

我如何可以採取記錄該商店?

回答

2

store.load()載荷經由配置的代理數據到商店。它使用代理作出異步呼叫到任何存儲後端代理使用,自動添加檢索到的實例進店,如果需要調用一個可選的回調。然而,該方法在獲取數據之前返回。因此,回調函數可以執行操作商店中新數據的邏輯。 試試吧,

detailsStore.load({ 
    callback: function(records, operation, success) { 
    var detailsRecord = detailsStore.last(); 
    }, 
    scope: this 
}); 
+0

非常感謝這個解決方案。 –