Backbone.js 视图 delegateEvents 没有被绑定(有时) [英] Backbone.js views delegateEvents do not get bound (sometimes)
问题描述
我正在使用 Backbone.js,有时 views 事件 没有得到正确绑定.
I'm using Backbone.js and sometimes the views events do not get bound correctly.
我可以用jQuery中的$(viewselector).data()
检查事件绑定情况.大多数时候有事件,有时没有!
I can check the event binding situation with $(viewselector).data()
in jQuery. Most of the time there are events, sometimes there aren't!
是否有任何我应该注意的已知事情会导致这种情况?
Are there any known things I should watch out for that can cause this?
推荐答案
当视图初始化时,事件被委托给 this.el.所以你需要:
Events are delegated to this.el when the view is initialized. So you need to:
- 通过为构造函数提供el"选项来指定元素来创建视图
- 在您的视图中定义 el、tag、id、classname 以直接在页面上创建或查找您的元素.
- 将渲染的视图附加到视图的el"元素
- 确保在创建视图后不要替换el"元素
对于最后一项,如果您必须这样做,您可以再次调用 delegateEvents 以在您的视图上重新委托该事件.
For the last item, if you have to do it, you can call delegateEvents once more to re-delegate the event on your view.
这篇关于Backbone.js 视图 delegateEvents 没有被绑定(有时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!