2012-07-20 85 views
1

相關的我JSON的格式如下:ExtJS的4.1 - 嵌套hasOne在JSON

{ 
    "id": 1, 
    "arbitraryAttribute": "arbitraryValue", 
    "thing": { 
     "thingKey1": "thingValue1", 
     "thinkKey2": "thingValue2" 
    } 
} 

這種模式表示像這樣:

Ext.define("MyModel", { 
    extends: "Ext.data.Model", 
    fields: [ 
     {name: "id", type: "string"}, 
     {name: "arbitraryAttribute", type: "string"}, 
    ], 
    hasOne: [ 
     { 
      model: "Thing", 
      name: "thing" 
     } 
    ] 
}); 

Ext.define("Thing", { 
    extends: "Ext.data.Model", 
    fields: [ 
     {name: "thingKey1", type: "string"}, 
     {name: "thingKey2", type: "string"} 
    ] 
}); 

代理是一個簡單的JSON代理。它所獲得的JSON看起來像我所呈現的,但我的記錄似乎沒有Thing模型的知識。是否有任何額外的配置需要設置讓MyModel拉動嵌套的Thing json?

回答

1

你忘了在MyModel中設置thing_id。此外,這在ExtJs中完全不起作用。 現在,您可以通過JSON設置thing_id,但不像您這樣(並且應該)設置整個對象。

如果需要,它會通過模型代理自動加載完整的對象。

0
Ext.define('User', { 
    extend:'Ext.data.Model', 
    fields: ['id', 'name', 'status'], 
    associations: [ 
     { type: 'hasOne', model: 'Status', associationKey: 'status' } 
    ] 
}); 

Ext.define('Status', { 
    extend:'Ext.data.Model', 
    fields: ['id', 'title'], 
}); 

Demo here