_.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);

Screen Shot 2011-10-14 at 1.55.50 PM.png

Screen Shot 2011-10-14 at 2.06.54 PM.png

Screen Shot 2011-10-14 at 2.18.03 PM.png

Screen Shot 2011-10-14 at 3.31.51 PM.png

Screen Shot 2011-10-14 at 3.33.11 PM.png

Screen Shot 2011-10-17 at 5.20.27 PM.png

Screen Shot 2011-10-18 at 3.37.23 PM.png

转载请注明:韦旭红的点点滴滴 » 关于backbone _.bindAll