【问题标题】:How to add multiple models to collection in Backbone如何在 Backbone 中将多个模型添加到集合中
【发布时间】:2015-05-19 11:33:25
【问题描述】:

如何在 Backbone 中将多个模型添加到集合中

    formatChannelIds: function() {

    _this = this;

    // var activeIds ='';


    _.filter(_this.modelChannelList.toJSON(), function(channelObj) {
      if (channelObj['isactive'] == true) {
        //   activeIds =activeIds+','+channelObj['id'];
        console.log(channelObj['id']);
        _this.modelChannelStats.fetch({
          data: {
            channel: channelObj['id']
          },
          processData: true
        }, {
          success: function(model, response, options) {



            _this.channelstatsCollection.push(model);



          }

        });

      }

    });

    console.log(_this.channelstatsCollection);
    console.log(_this.modelChannelStats);


  }

我的集合在数组中显示一个空值。

【问题讨论】:

    标签: javascript backbone.js collections models


    【解决方案1】:

    Fetch 方法接受一个包含所有必要参数的对象。

    _this.modelChannelStats.fetch({
             data: {
               channel: channelObj['id']
             },
             processData: true,
             success: function(model, response, options) {
               _this.channelstatsCollection.push(model);
    
            }
    
        });
    

    尝试使用这段代码。

    【讨论】:

    • 您在 fetch 调用中传递了两个传递的两个对象。 {数据:{通道:channelObj['id']},processData:true}{成功:函数(模型,响应,选项){_this.channelstatsCollection.push(模型); } } 所有东西都应该包装在一个对象中以便获取。
    • 我收集了一个模型,但不是所有模型都只有最后一个
    • 尝试在成功回调中记录您的收藏,例如成功:function(model, response, options) { _this.channelstatsCollection.push(model); console.log(_this.channelstatsCollection); } 在每个日志中,您都会将一个模型添加到您的集合中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多