未捕获的TypeError:在骨干中调用函数时,undefined不是函数

时间:2014-08-01 11:24:33

标签: javascript backbone.js backbone-views

我收到以下错误消息,

  

未捕获的TypeError:undefined不是函数

运行以下代码时,

render: function() {

    var cal_current_date = new Date();
    console.log("Dashboard Render");

    this.$el.html(this.template({
        dates: app.dates
    }));

    console.log("changing .calender-holder width");

    var width = $(".month-column").length * parseInt($(".calender").width()) / 6;
    $('.calender-holder').width(width);
    $('.month-column').width( width / $(".month-column").length);

    var month = cal_current_date.getMonth();

    if(month > 2) {
        var marginLeft = (month * 2) * parseInt( $('.month-column').width() );
        this.$el.find('.calender-holder').css("margin-left", "-" + marginLeft + "px" );
    }

    this.projectDashboardView = new app.ProjectDashboardView({
        collection: this.collection
    });

    return this;

},

app.ProjectDashboardView = Backbone.View.extend({

el: '.project-holder',

events: {

},

initialize: function() {
    console.log("ProjectDashboardView initialize");
    this.collection.on("reset", this.render);
    this.collection.on("add", this.addOne);

},

render: function() {
    console.log("ProjectDashboardView render");
    this.addAll(); <!---------- COMPLAINING ABOUT THIS LINE
},

addAll: function() {
    console.log("allAdd");
    this.collection.each( this.addOne );
},

addOne: function() {
    console.log("addOne");
    var view = new app.ProjectTimelineEntry({ model: model });
    this.$el.append( view.render().el );
}

});

我已经添加了问题所在的代码,就好像这是未定义的,或者定义不正确,如果我是console.log(这个)我只是得到传递给视图的集合,如果我没有得到的话该视图的所有功能也是如此?

1 个答案:

答案 0 :(得分:0)

解决此问题的简单方法是在视图的初始化中使用_bindAll

这是你使用它后初始化的样子

initialize: function() {
            _.bindAll(this,'render','addAll','addOne');
             console.log("ProjectDashboardView initialize");
             this.collection.on("reset", this.render);
             this.collection.on("add", this.addOne);
        }