_.bindAll() explained

by GarethElms

HTML

<script src="http://documentcloud.github.com/underscore/"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.2/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js"></script>

JavaScript

var TestViewTwo = Backbone.View.extend({
    callback: function(callback) {
        callback();
    }
});

var testViewTwo = new TestViewTwo();

var TestView = Backbone.View.extend({
    initialize: function() {
        console.log( "Before binding func1 to this : " + this.func1);
        _.bindAll(this, 'func1');
        console.log( "After binding func1 to this : " + this.func1);
        console.log( "func1 is wrapped in a function that pushes this");
    },
    func1: function() {
        console.log(this);
    },
    func2: function() {
        console.log(this);
    },
    doCallback: function() {
        testViewTwo.callback(this.func1);
        testViewTwo.callback(this.func2);
    }
});

var testView = new TestView();

testView.doCallback();