结合在Backbone.js的和Underscore.js回调 [英] Binding a callback in Backbone.js and Underscore.js
问题描述
我有以下的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
);
},
的意图是使()的$不用彷徨函数完成后,应调用。然而,即使有约束力的_.bind回调函数后,我仍然得到对象不起作用渲染在控制台中。我使用绑定错误在这里?
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?
推荐答案
_绑定
返回一个新的功能,所以:
_.bind
returns a new function, so:
callBack = _.bind(callBack, this);
您也可以使用 _。bindAll
,但你以后调用它的 的定义的功能。否则有你叫 _。bindAll
的时候没有的功能。请注意,您在这种情况下,使用 this.callBack = ...
,否则这个
将不包括任何功能。
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.
同时使用 _。结合
和 _。bindAll
是多余的。
这篇关于结合在Backbone.js的和Underscore.js回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!