Skip to content

Tag Archives: Javascript

关于backbone _.bindAll

_.bindAll(this, ‘render’);
this.model.bind(’change’, this.render);

以上这两行代码主要为了当model改变时, view执行render, 那_.bindAll有什么用呢?
看这里:http://stackoverflow.com/questions/6079055/why-do-bindall-in-backbone-js-views
其中有一段是这样的:

Without _.bindAll( this, ‘render’ ) when model changes this in render will be pointing to the model, not to the view, so we won’t have neither this.el nor this.$ or any other view’s properties available.

当model被change时, this指向model, 但是我们要执行render, 那么就需要_.bindAll(this, ‘render’), 使model也有这个render的方法?
请达人解答?
*
新版本这样就可以
this.model.bind(’change’, this.render, this);