将事件委托给 Backbone.js 中的子视图
我们都知道做这样的事情是不好的:
<ul>
<li>Item</li>
<li>Item</li>
... 500 more list items
</ul>
然后...
$("ul li").bind("click", function() { ... });
我一直在浏览很多 Backbone 示例/指南,以下似乎是基于模型集合渲染项目列表的标准方法。
var ListView = Backbone.View.extend() {
tagName: 'ul',
render: function() {
this.collection.each(function(item) {
var view = new ListItemView({model: item});
$(this.el).append(view.render().el);
});
return this;
}
});
列表项视图:
var ListItemView = Backbone.View.extend() {
tagName: 'li',
events: {
'click' : 'log'
}
log : function() {
console.log(this.model.get("title"));
}
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
如果我没记错的话,用包含 500 个模型的集合实例化 listView 会产生 500 个点击事件,每一行一个。这很糟糕吧?
我知道 Backbone 内置了命名空间事件的事件委托:
events : {
'click li' : 'log'
}
我想我可以将其放入 ListView 中,它只会为整个列表创建一个单击事件,但随后我将无法访问与单击的列表项。
主干开发人员使用什么模式来解决这个典型问题?
We all know doing something like this is bad:
<ul>
<li>Item</li>
<li>Item</li>
... 500 more list items
</ul>
and then...
$("ul li").bind("click", function() { ... });
I've been looking through a lot of Backbone examples / guides and the following seems to be a standard approach to rendering a list with items, based from a collection of models.
var ListView = Backbone.View.extend() {
tagName: 'ul',
render: function() {
this.collection.each(function(item) {
var view = new ListItemView({model: item});
$(this.el).append(view.render().el);
});
return this;
}
});
A list item view:
var ListItemView = Backbone.View.extend() {
tagName: 'li',
events: {
'click' : 'log'
}
log : function() {
console.log(this.model.get("title"));
}
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
If I'm not mistaken, instantiating the listView with a collection with 500 models, gives me 500 click events, one for each row. This is bad right?
I know Backbone has built in event delegation for namespaced events:
events : {
'click li' : 'log'
}
I suppose I could put this in my ListView, and it would only create one click event for the entire list, but then I wouldn't be able access to model data corresponding to the clicked list item.
What patterns do backbone developers use to solve this typical problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Derick Bailey 写了一篇关于这个困境的详细博客文章,您可以在这里查看:http://lostechies.com/derickbailey/2011/10/11/backbone-js-getting-the-model-for-a-clicked-element/
Derick Bailey wrote a detailed blog post about this dilemma, you can check it out here: http://lostechies.com/derickbailey/2011/10/11/backbone-js-getting-the-model-for-a-clicked-element/
您可以将实例与元素关联起来,如下所示:
然后:
You can associate the instance with an element like so:
Then:
从父视图中跟踪子视图。然后,在添加子视图时,将其添加到哈希中,并将 cid 添加到子视图的 el 中。这种方式有一个指向子视图的指针,并且可以对其模型执行操作等...
我没有测试下面的确切代码,所以这可能在一两个地方是错误的,但我已经测试了这个一般原则。我还省略了 listitemview 代码。
Keep track of the subviews from the parent view. Then when adding the subview add it to the hash as well as add the cid to the el of the subview. This way have a pointer to the subview and could perform operations on its model etc...
I have not tested this exact code below so THIS may be wrong in a place or two but I have tested this general principle. I have also omitted the listitemview code.