I created a simple backbone book library project, it has 1 model, view, collection. When I fetch the data from collection I'll process the JSON data and set the processed data into a variable in collection to show it in Google charts. How to listen to that processed variable change in Backbone ?
Here is my code
Model
var BookModel= Backbone.Model.extend({
defaults:{
id:'',
name: '',
author: ''
}
});
Collection
var BookCollection= Backbone.Collection.extend({
model: BookModel,
initialize: function(){
this.fetchData();
},
fetchData: function(){
this.url= "/get/all_books";
this.fetch({
success: success_callback,
error: error_callback
})
},
success_callback: function(that, data){
var chart_data=[];
//Process logic goes here. chart_data has the data i need
// I need to listen this.chart_data value change in view
this.chart_data= chart_data;
},
error_callback: function(){
}
});
View
var BookView= Backbone.View.extend({
initialize: function(){
this.collection= new BookCollection();
}
drawChart: function(){
//Google Chart Goes here, when ever the chart_data variable changes
}
});
Please correct me if any of my code conventions are not good. Thank you