我在我的模型中有一个方法“getSegmentGroup”来创建一个 json,该 json 具有来自通过 fetch 获取的数据的有限字段(它具有所有记录和字段)。
我的模型方法如下所示
getSegmentGroups: function(customerIds, column1Name, column2Name){
var segmentTableJson = [];
this.getSegment = new CustomerCollection();
this.getSegment.fetch({
success: function(response) {
var fetchData = response.toJSON();
for(var i in customerIds){
customerId = customerIds[i];
for(var j in fetchData){
if(fetchData[j].custId == customerId){
column_1 = fetchData[j][column1Name];
column_2 = fetchData[j][column2Name];
}else{
continue;
}
}
var item = {
"customerId" : customerId,
"column1" : column_1,
"column2" : column_2
};
segmentTableJson.push(item);
}
console.log(JSON.stringify(segmentTableJson));
var jsonTableFormat = JSON.stringify(segmentTableJson);
}
});
}
输入 customerIds 是一个 id 数组,如下所示
[7, 8]
结果 JSON 是
[{"customerId":7,"column1":"raju.allen1888@gmail.com","column2":24},{"customerId":8,"column1":"raju","column2":34}]
现在我需要从模型中传递 json“jsonTableFormat”来查看以在表中显示 json。
我如何传递给 segment_table_view 以显示表格
var segment_table_view = Backbone.View.extend({
el: $('#segment'),
initialize: function() {
this.model.bind("add", this.render, this);
},
//this creates new rows in the table for each model in the collection
render: function() {
_.each(this.model.models, function(data) {
this.$el.append(new segment_view({
model: data
}).render().el);
}, this);
return this;
}
});
//a (row) view to render each employee
var segment_view = Backbone.View.extend({
tagName: "tr",
template: _.template($("#segment-table").html()),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
由于我是骨干新手,我无法弄清楚。