我正在使用Ember.js应用程序并拥有一个设备表,我希望每个列出的设备在单击时打开模式对话框,使用该特定设备作为对话框的模型。 官方文档对于如何做到这一点略显模糊,所以我一直在做一些猜测。我试图将每个设备的id作为参数传递给模板:
{{#each}}
<button data-toggle="modal" data-target="#modal" {{action 'openViewModal' 'viewDeviceModal' id}} class="btn btn-default btn-xs">View details</button>
{{/each}}
{{id}}
按照模板中其他位置的方式工作。
现在,在路线内,我把它:
actions: {
openModal: function(modalName, id) {
this.controllerFor(modalName).set("model", this.store.find('device', id));
return this.render(modalName, {
into: "application",
outlet: "deviceModal"
});
},
这给了我以下错误:
"The value that #each loops over must be an Array. You passed (generated devices controller)"
据推测,设备列表的模型(#each
)和模态对话框的模型存在一些混淆。模态对话框里面没有#each
。
我也缺乏一种方法来确定我的变量是否真的是他们应该是的。 console.log
不起作用。我还没有阅读有关调试ember的所有文档,所以我希望那里会有一些指针。同时,非常感谢任何有关如何使我的模态对话框模型起作用的帮助。
答案 0 :(得分:1)
这不是每个循环的问题,而是与您的模型有关。您将控制器的model
设置为单个记录,当它看起来需要一组记录时。将操作的第一行更改为:
this.controllerFor(modalName).set("model", [this.store.find('device', id)]);
另外,作为提示,当您已经拥有该记录时,请不要再次从商店加载该记录。只需在动作助手中传递整个记录:
{{action 'openViewModal' 'viewDeviceModal' this}}
然后你的动作处理程序可能如下所示:
openModal: function(modalName, record) {
this.controllerFor(modalName).set("model", [record]);