Backbone:收集问题

时间:2014-03-04 02:27:00

标签: javascript backbone.js

我正在尝试登录,然后获取项目列表。
从登录模型中,它会尝试reset项目列表。问题是当集合被重置时,集合UI没有捕获它。
登录模型的登录功能

     login: function() {
        this.save(
            {}, {
                success: function(resp) {
                    var list = [];
                    list[0] = resp.get("0");
                    list[1] = resp.get("1");
                    dashboardList.reset(list);
                }
            });
       }

DashboardList

var DashboardList = Backbone.Collection.extend({
    model: DashboardModel
});

var dashboardList= new DashboardList();

信息中心列表视图

var DashboardListView = Backbone.View.extend({
    initialize: function() {
        this.collection.on('add', this.addOne, this);
        this.collection.on('reset', this.addAll, this);
    },
    addOne: function(item) {
        var viewItem = new DashboardView({model: dashboardModel});
        this.$el.append(viewItem.render().el);
    },
    addAll: function() {
        this.collection.forEach(this.addOne, this);
    },
    render: function() {
        this.addAll();
    }
});

信息中心项目视图

var DashboardView = Backbone.View.extend({
    template:_.template('<div>'+
                        '<h3><%= campaignName %></h3>'+
                        '<span><%= orderedAndGoal %>, </span>'+
                        '<span><%= status %>, </span>'+
                        '<span><%= endDate %>, </span>'+
                        '</div>'),
    initialize: function() {
        this.model.on('change', this.render, this); 
    },
    render: function() {
        var attributes = this.model.toJSON();
        this.$el.html(this.template(attributes));
        this.$el.appendTo('.container');
    }
});

1 个答案:

答案 0 :(得分:0)

有两个问题:

  1. 服务器的响应与收集时的预期不符。当我改变一些静态数据时。它流过。

  2. 单个仪表板视图需要返回它的视图对象,以便可以将其渲染到集合中。

    render: function() { console.log('what happens here') var attributes = this.model.toJSON(); this.$el.html(this.template(attributes)); //this.$el.appendTo('.container'); return this; },

相关问题