I'm trying to create a master view/controller for a Backbone project, and I'm having troubles accessing the views within the view properly. Here's the code:
var Controller = Backbone.View.extend({
initialize: function() {
this.characterView = new CharacterView({model: character});
this.encounterView = new EncounterView({collection: encounter});
this.characterView.$el.on('click', '.attack', this.charAttack);
},
charAttack: function() {
console.log(this.characterView);
},
render: function() {
this.encounterView.render();
this.characterView.render();
console.log(this.characterView.model.toJSON());
}
});
var controller = new Controller();
controller.render();
The this.characterView
in charAttack is undefined whereas in the render function, it uses the right object. I'm not sure why render can access this.characterView, but charAttack can't. Any help to understand this would be greatly appreciated.