Backbone.js:Subclassed View中的属性未定义

时间:2012-02-10 02:40:58

标签: backbone.js

我想在Web应用中显示几个模型列表,所以我创建了一个泛型类,如下所示:

App.ModelListView = Backbone.View.extend({

    initialize: function() {
        this.collection.bind('reset', this.addAll, this);
        this.collection.bind('add', this.addOne, this);
        this.collection.bind('remove', this.removeOne, this);
    },

    addAll: function () {
        this.removeAll();
        this.collection.each(this.addOne, this);
    },

    ...

});

对于每种模型类型,我将App.ModelListView子类化为:

App.ProjectListView = App.ModelListView.extend({

    el: $("#project-list"),

    collection: App.Projects,

    listItemView: App.ProjectListItemView

});

当我使用

实例化App.ProjectListView视图时
App.ProjectMainListView = new App.ProjectListView();

我发现当App.ModelListView的{​​{1}}函数执行时,'this.collection'是未定义的。为什么不使用我在子类中定义的initialize的值?我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我猜你在定义你的课程后正在设置App.Projects。尝试将视图实例的创建更改为以下::

App.ProjectMainListView = new App.ProjectListView({
    collection: App.Projects
});

在您定义视图时,我敢打赌您还没有设置App.Projects