【发布时间】:2013-05-07 06:49:37
【问题描述】:
假设我在一个列表中有许多对象,我想从这些对象中创建 <table/> 中的 <tr/> 元素。我像这样创建了一个Ember.View:
App.TableRowItem = Ember.View.extend({
classNames: ['list-row'],
templateName: 'admin/things/tableRow',
showActionRow: function() {},
edit: function() {},
save: function() {},
delete: function() {}
});
现在,我想在 Handlebars 模板中为检索到的列表中的每个对象生成一个行项目,如下所示:
<table id="someTable">
<thead>
<tr><th>Head1</th><th>Head2</th><th>Head3</th></tr>
</thead>
<tbody>
{{# each thing in controller.things}}
{{log thing}}
{{view App.TableRowItem contentBinding="thing" id="thing.id"}}
{{/each}}
</tbody>
</table>
我希望它能够为每个 id 为 this.id 的元素创建一个 id 标记,但我收到以下错误消息:
Uncaught Error: assertion failed: Attempted to register a view with an id already in use: this.id
我做错了什么?如果我没有在{{view}} 助手中设置id,我会收到与上面相同的消息,但它抱怨id already in use: null...
更新:
由于我无法使用 idBinding 解决问题,我在 App.TableRowItem 视图中尝试了以下解决方案:
App.TableRowItem = Ember.View.extend({
tagName: 'tr',
templateName: 'admin/things/tableRow',
// this is new!
init: function() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ ) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
this.set('elementId', text);
}
});
现在我没有看到任何错误,但我看到模板中不仅有 <tr/> (admin/things/tableRow),而且还有一个 <div/> 元素,它似乎代表了我的 @ 987654338@没有任何内容!?
虽然调试 this.$() 给了我一个空的 <tr id="1fFxV" class="ember-view"></tr> 并且 tableRow 模板可以通过 this.$().closest('tr').next() 访问...
【问题讨论】: