2011-08-18 26 views
2

我試圖從我的模型商店訪問以下JSON。我沒有訪問根(名稱和地址)的問題,但我無法訪問任何財務(NetWorth或收入)。我已驗證下面的JSON正從服務器返回。Sencha Touch - 訪問1對1模型關係

我知道這一定很容易,但我見過的每個示例都展示瞭如何訪問1對多關係。我的模型是1比1。人員 - >財務。現在我沒有任何關聯設置,因爲我不知道如何設置1對1關係。我嘗試過關聯,我試過belongsTo,我從我的itemTPL裏面試過了Financials.NetWorth。沒有。

有人能給我看光嗎?

JSON從服務器返回

[{ 
"PersonName": "John Smith", 
"Address": "123 main street", 
"Financials": 
     [{ 
      "NetWorth": "$500,000", 
      "Income":"$67,000" 
     }] 
}] 

我註冊了我的模特。

Ext.regModel('Person', { 
    fields: [ 
    {name: 'Name', type:'string'}, 
    {name: 'Address', type: 'string'}, 
    ], 
}); 

Ext.regModel('Financials',{ 
    fields: [ 
    {name: 'NetWorth', type:'string'}, 
    {name: 'Income', type: 'string'}, 
    ], 
}); 

登記的商店

var commStore = new Ext.data.Store({ 
      model: 'Person', 
      proxy: { 
       type: 'ajax', 
       url : 'Business/GetData', 
       reader: { 
        type: 'json', 
       } 
      }, 
      autoLoad:false, 
     }); 

並以列表

var commList = new Ext.List({ 
     fullscreen: false, 
     itemTpl : '<div>{PersonName}</div><div>{Business.NetWorth}</div>', 
     store: commStore, 
     height:500, 
     width:"100%", 
    }); 

任何幫助將非常感激顯示回來。

回答

1

初學者我自己也一樣,但似乎follwing可以幫助(不與你的代碼測試)

僅型號有:

Ext.regModel('Person', { 
    fields: [ 
    {name: 'Name', type:'string'}, 
    {name: 'Address', type: 'string'}, 
    {name: 'NetWorth', type:'string', mapping 'Financials.NetWorth'}, 
    {name: 'Income', type: 'string', mapping 'Financials.Income'}, 
    ], 
}); 

,並在模板代碼與名稱是指

itemTpl : '<div>{PersonName}</div><div>{Business.NetWorth}</div>', 

變成

itemTpl : '<div>{PersonName}</div><div>{NetWorth}</div>', 
0

你說你的關係是1對1的關係,但是你的JSON數據返回一個數組(因爲你使用了「[]」括號),所以對於財務來說,數組返回一個數組(1項,但仍然是一個數組)。 這意味着人應該變成這樣:

Ext.regModel('Person', { 
    fields: [ 
    {name: 'Name', type:'string'}, 
    {name: 'Address', type: 'string'}, 
    ], 
    hasMany: [ 
    {name: 'person', model: 'Person'} 
    ] 
});