我有以下 json
数据:
{
"type": "type1",
"name": "Name1",
"properties": {
"age": 23,
"address": "Sample"
}
}
我使用 Ember Data
对此进行建模,如下所示:
App.Node = DS.Model.extend({
type: DS.attr('string'),
name: DS.attr('string'),
properties: DS.belongsTo('App.NodeProperties')
});
App.NodeProperties = DS.Model.extend({
age: DS.attr('number'),
address: DS.attr('string')
});
是否有比使用 DS.belongsTo
更好的方式来建模嵌套属性
?
我如何访问模板中的年龄。我目前正在做
{{node.properties.age}}
但我不确定这是否有效。
最佳答案
Is there a better way to model the nested properties than using a DS.belongsTo?
考虑到您的用例,DS.belongsTo 是一个不错的选择。
How would I access the age in my templates?
{{node.properties.age}}
是正确的,假设 {{node}}
是有效引用
But I am not sure this is working.
您还需要执行一些步骤才能使其正常工作。首先,将 App.Node 的映射添加到其余适配器,指定将嵌入属性:
DS.RESTAdapter.map('App.Node', {
properties: { embedded: 'always' }
};
然后更新 NodeProperties 以包含关系:
App.NodeProperties = DS.Model.extend({
age: DS.attr('number'),
address: DS.attr('string'),
node: DS.belongsTo('App.Node')
});
有关详细信息,请查看这些答案:
关于ember.js - 建模嵌套 json 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809219/