最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why are events not firing after the second render in Backbone.js? - Stack Overflow

programmeradmin0浏览0评论

I am creating an app in Backbone.js which has a parent and multiple child views. The child views contain links which they listen to and perform a function.

The parent stores a list of all of the children views. In the render function, after it is done computing its own html, it does the following:


$(this.el).html(html);
for (var i = 0; i < this.views.length; i++){
    $('.children', this.el).append(this.views[i].render().el);
}

ANSWER: The problem was that I was creating the link during the render. I.e. on the first render (which was called from the init) the event successfully binded to the link. However, since all following calls of render recreate the whole element, the new link did not have the handler bound to it. This was solved via @Tom Tu solution of adding this.delegateEvents() to the render

I am creating an app in Backbone.js which has a parent and multiple child views. The child views contain links which they listen to and perform a function.

The parent stores a list of all of the children views. In the render function, after it is done computing its own html, it does the following:


$(this.el).html(html);
for (var i = 0; i < this.views.length; i++){
    $('.children', this.el).append(this.views[i].render().el);
}

ANSWER: The problem was that I was creating the link during the render. I.e. on the first render (which was called from the init) the event successfully binded to the link. However, since all following calls of render recreate the whole element, the new link did not have the handler bound to it. This was solved via @Tom Tu solution of adding this.delegateEvents() to the render

Share Improve this question edited Nov 29, 2011 at 9:51 chacham15 asked Nov 29, 2011 at 8:41 chacham15chacham15 14.3k26 gold badges119 silver badges218 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

You are probably using jquery remove function somewhere to remove the subviews from the view - it automatically removes all the events bound to the element (this.el) - set in the events object. You can either use this.delegateEvents() method in render of the subviews after you render template to rebind the event delegates set in events object or use jquery detach method instead to remove elements from DOM without removing event bindings (link). The delegateEvents method is quite costly and thus i'd recommend the detach method for removing elements that you want to reuse if you are rendering long lists of subviews - irrelevant if it's just a couple of views.

Other possibility is that you've set the events object wrong - hard to tell from the amount of code provided, but i bet on the first one.

A really common challenge. For future finders of this question, here's a great article about view rendering:

You just need to make sure that delegateEvents is called to rebind the events on your subviews any time .html() runs. And since Backbone’s setElement calls delegateEvents already, a quick solution could look like this...

http://ianstormtaylor.com/rendering-views-in-backbonejs-isnt-always-simple/

发布评论

评论列表(0)

  1. 暂无评论