Javascript 如何向主干中的集合添加多个模型
如何向主干中的集合添加多个模型Javascript 如何向主干中的集合添加多个模型,javascript,backbone.js,collections,models,Javascript,Backbone.js,Collections,Models,如何向主干中的集合添加多个模型 formatChannelIds: function() { _this = this; // var activeIds =''; _.filter(_this.modelChannelList.toJSON(), function(channelObj) { if (channelObj['isactive'] == true) { // activeIds =activeIds+','+
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);
}
我的集合在数组中显示空值 Fetch方法接受一个包含所有必要参数的对象
_this.modelChannelStats.fetch({
data: {
channel: channelObj['id']
},
processData: true,
success: function(model, response, options) {
_this.channelstatsCollection.push(model);
}
});
尝试使用这段代码 您正在fetch调用中传递两个对象。{data:{channel:channelObj['id']},processData:true}{success:function(model,response,options){{u this.channelstatsCollection.push(model);}所有的东西都应该包装在一个对象中进行提取。我在集合中得到一个模型,但不是所有的模型,只有最后一个在成功回调中记录您的集合,比如success:function(model,response,options){this.channelstatsCollection.push(model);console.log(_this.channelstatsCollection);}在每个日志中,您将获得一个添加到集合中的模型。