【发布时间】:2013-02-15 02:53:03
【问题描述】:
这是我对“tr”单行的看法。我想单击名称单元格并弹出该单元格的视图。我无法触发事件..
我错过了什么吗?谢谢!
所以这个问题由 gumballhead 解决,我遇到的问题是需要有一个与 ItemRowView 关联的 tagName。然后在渲染函数中,我需要做 self.$el.html(this.template(model));
觉得分享给大家会有所帮助..
ItemRowView = Backbone.View.extend({
initialize : function() {
},
template : _.template($('#item-row-template').html()),
render : function() {
var self=this;
var model = this.model.toJSON();
self.$el = this.template(model);
return self.$el;
},
events : {
"click td .item-name" : "viewOneItem"
//Even if I change it to "click td":"viewOneItem", still not firing
},
viewOneItem : function() {
console.log("click");
}
});
收藏视图:
ItemsView = Backbone.View.extend({
initialize : function() {
},
tagName : "tbody",
render : function() {
var self = this;
this.collection.each(function(i) {
var itemRowView = new ItemRowView({
model : i
});
self.$el.append(itemRowView.render());
});
return self.$el;
}
});
应用视图: AppView = Backbone.View.extend({ this.items = new Items();
this.items.fetch();
this.itemsView = new ItemsView({collection:this.items});
$('#items-tbody').html(itemsView.render());
});
对于模板:
<script type="text/template" id="item-row-template">
<tr>
<td class="item-name">{{name}}</td>
<td>{{description}}</td>
</tr>
</script>
<table>
<thead>
<tr>
<th>name</th>
</tr>
</thead>
<tbody id="items-tbody">
</tbody>
</table>
【问题讨论】:
-
try
"click 'td .item-name'"Backbone 通过按空格分隔来绑定事件我相信......它可能与您的选择器有问题。啊,委托事件(这些是)使用不同的拆分器。 ` var delegateEventSplitter = /^(\S+)\s*(.*)$/;` -
@Cory:这行不通,
events解析器不会知道单引号的含义。 -
有种不喜欢的感觉