Backbone.js 视图 delegateEvents 未绑定(有时)
我正在使用 Backbone.js,有时 视图事件 无法正确绑定。
我可以使用 jQuery 中的 $(viewselector).data()
检查事件绑定情况。大多数时候有事件,有时没有!
有哪些我应该注意的已知事情可能会导致这种情况?
I'm using Backbone.js and sometimes the views events do not get bound correctly.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当视图初始化时,事件被委托给 this.el。因此,您需要:
对于最后一项,如果必须这样做,可以再次调用 delegateEvents 来重新委托您所看到的事件。
Events are delegated to this.el when the view is initialized. So you need to:
For the last item, if you have to do it, you can call delegateEvents once more to re-delegate the event on your view.
在这些场景中,我的方法是在每个具有事件的视图的渲染中添加
delegateEvents()
,如下所示:这非常适合专门动态创建的视图,即在每次单击或所以...
My approach in these scenarios is to add
delegateEvents()
in render of each view that has an event, like the following:This is perfect for views specially created dynamically i.e. views that are declared new under each click or so...