【问题标题】:Backbone: Trouble with CollectionsBackbone:集合的问题
【发布时间】:2014-03-04 02:27:00
【问题描述】:

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

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

仪表板列表

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');
    }
});

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    有两个问题:

    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; },

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 2012-12-02
      • 2023-03-06
      • 1970-01-01
      • 2012-11-23
      • 2011-11-25
      • 2012-02-11
      相关资源
      最近更新 更多