2012-07-20 84 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