我正在学习以下教程: http ://arturadib.com/hello-backbonejs/docs/1.html
其中有这段代码:
(function($){
var ListView = Backbone.View.extend({
el: $('body'), // attaches `this.el` to an existing element.
initialize(): Automatically called upon instantiation. Where you make all types of bindings, excluding UI events, such as clicks, etc.
initialize: function(){
_.bindAll(this, 'render'); // fixes loss of context for 'this' within methods
this.render(); // not all views are self-rendering. This one is.
},
render: function(){
$(this.el).append("<ul> <li>hello world</li> </ul>");
}
});
var listView = new ListView();
})(jQuery);
在initialize方法中,为什么需要做bindAll。我对 bindAll 的理解是它允许在调用 render 时使用 this 的上下文。
由于我们调用 this.render() 不是上下文已经 this...为什么我们需要bindAll
?