【问题标题】:CollectionView fails to render with empty collectionCollectionView 无法使用空集合呈现
【发布时间】:2013-05-18 14:29:16
【问题描述】:

为什么我的 Marionette CollectionView 不能处理空集合?

我正在尝试在 CompositeView 中呈现 CollectionView(它是项目列表中每个项目中的任务列表)。一切正常,直到我将一个空集合传递给 CollectionView。集合 is 允许为空;一个项目很容易没有分配任何任务。

Chrome 显示的错误是

Uncaught TypeError: Object [object Object] has no method 'on'

传入的项目集合一个有效的Backbone.CollectionTasksCollection的过滤方法返回的任务列表也是,这就是为什么我可以'不知道为什么会显示上面的错误。

Marionette 的 CollectionView 可以接受一个空集合(它有一个emptyView 属性),那么为什么传入一个时会报错呢?我认为它正在尝试将事件绑定到应该“正常工作”的集合 - 只有模型不存在。

我的任务集合如下所示:

var TasksCollection = Backbone.Collection.extend({
    model: TaskModel,
    byProject: function(projectId) {
        var matches = this.filter(function(task) {
            return task.get('projectId') == projectId;
        }, this);

        return new TasksCollection (matches);
    },
    complete: function(state) {
        return new TasksCollection (this.where({ complete: state }));
    }
});

我的观点是这样的:

// Single task item
var TasksListView = Marionette.CollectionView.extend({
    tagName: 'ul',
    className: 'tasks nav nav-stacked nav-pills',
    itemView: Minuteboy.views.TaskItem,
    emptyView: Templates['partials/tasks-empty']
});

// Project item (should probably be a Layout or CompositeView)
var DashboardProject = Marionette.ItemView.extend({
    tagName: 'article',
    template: Templates['partials/dashboard-project'],
    initialize: function() {
        this.on('render', function() {
            var tasks = AllProjectTasks.byProject(this.model.get('id')).complete(false);

            this.$el.find('.tasks-wrapper').html(new TasksListView({ 
                collection: tasks
            }).render().el);
        });
    }
});

// Containing view with page title and container for projects list
var DashboardPage = Marionette.CompositeView.extend({
    template: Templates['pages/dashboard'],
    itemView: DashboardProject,
    itemViewContainer: '#content'
});

最后,我像这样实例化页面视图:

var page = new DashboardPage({
    collection: // A valid Backbone.Collection
});

【问题讨论】:

    标签: events marionette collectionview


    【解决方案1】:

    这是我的一个错误。我没有为我的 CollectionView 的emptyView 属性使用某种视图对象,而是使用了一个模板(在这种情况下为把手)。我的工作 CollectionView 看起来像这样:

    var EmptyList = Marionette.ItemView.extend({
        tagName: 'li',
        template: Templates['partials/tasks-empty']
    });
    
    Minuteboy.views.TaskList = Marionette.CollectionView.extend({
        tagName: 'ul',
        className: 'tasks nav nav-stacked nav-pills',
        itemView: Minuteboy.views.TaskItem,
        emptyView: EmptyList
    });
    

    注意itemView 行:

    itemView: Minuteboy.views.TaskItem
    

    这曾经是

    itemView: Templates['partials/tasks-empty']
    

    不好

    【讨论】:

      猜你喜欢
      • 2013-10-06
      • 2020-11-09
      • 2013-08-08
      • 1970-01-01
      • 2014-11-18
      • 2020-06-26
      • 2015-01-31
      • 2011-04-21
      • 2020-10-16
      相关资源
      最近更新 更多