I have an issue to access my collection on a view’s method, in fact, it works well from the initialize() method, but i created another one (drawVisualization()), and i get an undefined error when i try to access this.collection, it’s probably a stupid question but i didn’t find any solution, i tried to use _.bind on the initialize method but doesn’t seem works in this case, here is the code:
App.Views.account = Backbone.View.extend({
className: 'account',
el: $('#account-container'),
initialize: function(){
console.log(this.collection.toJSON()); //Works fine !
this.template = _.template($('#account-template').html());
_.bind(this.drawVisualization, this); //Seems to be useless
},
render: function(){
//Some code...
return this;
},
drawVisualization: function(){
console.log(this.collection.toJSON()); //Fail because of undefined collection !
}
Thanks for your help !
I don’t have an idea why it doesn’t work, but try using underscore’s
bindAll.For me just dumping
bindAllin the beginning of each viewinitializeis a good way of avoiding this kind of problems.