I have the following code:
initialize: function() {
_.bindAll(this);
var callBack = function(res) {
window.item = new Item(res);
this.render();
};
_.bind(callBack, this);
$.get('/item/parse', {
uri: decodeURIComponent($.urlParam('uri')),
title: decodeURIComponent($.urlParam('title'))
},
callBack
);
},
The intention is that render() should be called after the $.get function finishes. However, even after binding the callback function with _.bind, I still get “Object has no function render” in the console. Am I using bind incorrectly here?
_.bindreturns a new function, so: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 usethis.callBack = ...in that case, because otherwisethiswon’t consist of any functions.Using both
_.bindand_.bindAllis superfluous.