2012-07-19 38 views
3

我有Person模型,它包含id,first_name,last_name等和merital_id字段。我也有Merital模型只包含2個字段:id和title。 服務器響應JSON,如:Extjs hasone association

{ 
    success: true, 
    items: [ 
     { 
      "id":"17", 
      "last_name":"Smith", 
      "first_name":"John", 
      ... 
      "marital_id":1, 
      "marital": { 
       "id":1, 
       "title":"Female" 
      } 
     }, 
     ... 
    ] 
} 

因此,如何能連接我的機型聯想?我仍然可以在我的column.renderer中使用record.raw.merital.title,但不能在像{last_name} {first_name}({merital.title})這樣的模板中使用這些字段。 我需要使用哪種結社王,我試過belongsTo,但是當我嘗試使用record.getMarital()時,我得到一個錯誤「沒有這樣的記錄方法」。

我使用ExtJS的4

回答

7

您應該使用ExtJS的模型,和協會,特別是HasOne關聯。

文檔:

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.association.HasOne

例子:

http://jsfiddle.net/el_chief/yrTVn/2/

Ext.define('Person', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     'id', 
     'first_name', 
     'last_name' 
     ], 

    hasOne: [ 
     { 
     name: 'marital', 
     model: 'Marital', 
     associationKey: 'marital' // <- this is the same as what is in the JSON response 
     } 
    ], 

    proxy: { 
     type: 'ajax', 
     url: 'whatever', 
     reader: { 
      type: 'json', 
      root: 'items' // <- same as in the JSON response 
     } 
    } 
}); 
+0

好了,但我用煎茶架構師,我不看這類物業電網協會( – 2012-07-20 06:52:50

+1

然後手動將它寫入 – 2012-07-20 06:58:51

+0

嗯,但每次我部署我的項目它都會消失? – 2012-07-20 08:16:50