Backbone中的Underscore的.defer()导致错误:无法调用未定义的方法'apply'

时间:2013-09-12 18:01:59

标签: javascript jquery backbone.js underscore.js

我在同一个Backbone View中的两个地方使用defer()来运行两个不同的功能。但是,两者都会导致以下错误:

两个函数调用位于渲染函数中:

loadReferralCollection: function(collection) {
        console.log("hello")
},

loadRemixedCollection: function(collection) {
        console.log("hello")
},

render: function() {
        var self = this;
        if ( this.options.params !== undefined && this.options.params.referral !== undefined ){
              _( self.loadReferralCollection(self.model) ).defer();
        } else if ( this.options.params !== undefined && this.options.params.remix !== undefined ) {
              _( self.loadRemixedCollection(self.model) ).defer();
        }
}

完整错误:

Uncaught TypeError: Cannot call method 'apply' of undefined underscore.js?body=1:621
(anonymous function)

1 个答案:

答案 0 :(得分:2)

defer()想要推迟函数调用。所以你必须传递一个函数:

或者:

_( function() { self.loadRemixedCollection(self.model); } ).defer();

或者

_( self.loadRemixedCollection ).defer(self.model);

好吧,或者

_.defer( function() { self.loadRemixedCollection(self.model); } )
_.defer( self.loadRemixedCollection, self.model )