why do bindAll in backbone.js views?

后端 未结 3 1687
面向向阳花
面向向阳花 2020-12-04 09:36

In backbone\'s todo demo the code has a few spots where _.bindAll(this,...) is used. Specifically it\'s used in the initialize function of both vie

3条回答
  •  粉色の甜心
    2020-12-04 09:46

    As of Backbone 0.5.2, it's no longer necessary to use _.bindAll(this...) in your views to set the context of the "bind" callback functions, as you can now pass a 3rd argument to bind() that will set the context (i.e. "this") of the callback.

    For example:

    var MyView = Backbone.View.extend({
      initialize: function(){
        this.model.bind('change', this.render, this);
      },
      render: function(){
        // "this" is correctly set to the instance of MyView
      }
    });
    

提交回复
热议问题