i have a method "getSegmentGroup" in my MODEL to create a json with limited fields from data feteched through fetch(which has all records and fields).
My model method looks like the following
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);
}
});
}
the input customerIds is an array of ids like the following
[7, 8]
Resulting JSON is
[{"customerId":7,"column1":"raju.allen1888@gmail.com","column2":24},{"customerId":8,"column1":"raju","column2":34}]
Now i need to pass the json "jsonTableFormat" from the model to view to display the json in table.
how do i pass to a segment_table_view to display a table
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;
}
});
Since i'm new to backbone i couldn't figure it out.