我有一个显示的列表,其中包含hasMany关系。我没有显示初始列表时遇到问题,但是当我点击项目中的链接时,我希望显示来自第二个JSON的数据。
以下是我的模特:
App.TemplateInstance = DS.Model.extend({
name: DS.attr(),
template_instances_audiences: DS.hasMany("TemplateInstanceAudience", {async:true})
});
App.TemplateInstanceAudience = DS.Model.extend({
name: DS.attr(),
template_instance: DS.belongsTo("TemplateInstance",{async:true}),
asset_instances: DS.hasMany("AssetInstance")
});
App.AssetInstance = DS.Model.extend({
name: DS.attr(),
template_instance_audience: DS.belongsTo("TemplateInstanceAudience")
});
我正在使用mockjax来模拟发出GET请求。从mockjax可以看出,我有不同的嵌套路由显示不同的数据:
$.mockjax({
url: '/templateInstances/4',
dataType: 'json',
responseText: {
template_instances: {
id: 1,
name: "bill",
links: {
template_instances_audiences: "/templateInstances/4/audiences"
}
}
}
});
$.mockjax({
url: "/templateInstances/4/audiences",
dataType: "json",
responseText: {
template_instance_audiences: [{
id: 1,
name: "bar 1",
template_instance: 1,
links: {
asset_instances: "/templateInstances/4/audiences/1/assets"
}
},{
id: 2,
name: "bar 2",
template_instance: 1,
links: {
asset_instances: "/templateInstances/4/audiences/2/assets"
}
}]
}
});
$.mockjax({
url: "/templateInstances/4/audiences/1/assets",
dataType: "json",
responseText: {
asset_instances: [{
id: 1,
asset_id: 1,
name: "Assets 1",
template_instance: 1
},{
id: 2,
asset_id: 2,
name: "Assets 2",
template_instance: 1
}]
}
});
$.mockjax({
url: "/templateInstances/4/audiences/2/assets",
dataType: "json",
responseText: {
asset_instances: [{
id: 3,
asset_id: 3,
name: "Assets 3",
template_instance: 1
},{
id: 4,
asset_id: 4,
name: "Assets 4",
template_instance: 1
}]
}
});
我的模板显示模板实例模型中的项目。
<ul>
{{#each item in template_instances_audiences}}
<li><a href="#" {{action "displayAssets"}}>{{item.id}} - {{item.name}}</a></li>
{{/each}}
</ul>
..单击某个项目时,该列表将显示在
中 <ul>
{{#each item in asset_instances}}
<li>{{item.name}}</li>
{{/each}}
如何从assets_instances链接属性获取链接,然后在前端显示它?当我执行console.log时,我无法获得与该项目相关的链接。
希望jsbin能让它更清晰:http://emberjs.jsbin.com/yebuzi/2/edit