Skip to content Skip to sidebar Skip to footer

Binding A Callback In Backbone.js And Underscore.js

I have the following code: initialize: function() { _.bindAll(this); var callBack = function(res) { window.item = new Item(res); this.render(); }; _

Solution 1:

_.bind returns a new function, so:

callBack = _.bind(callBack, this);

You can also use _.bindAll, but you have to call it after you define the function. Otherwise there are no functions at the time you call _.bindAll. Note that you have to use this.callBack = ... in that case, because otherwise this won't consist of any functions.

Using both _.bind and _.bindAll is superfluous.

Solution 2:

I usually write a 'load' method on my model which I giva a callback as parameter. Then I call this method from the render() method in the view and do whatever I need to do in the callback function (of course, that callback is triggered in the model after the data has been loaded).

Post a Comment for "Binding A Callback In Backbone.js And Underscore.js"