Question

I'm kind of new to backbone and have a question. It seems like when an event in my view is fired, I lose the context to "this". How can I preserve this or get the original "this" for the model. Here is an example:

var MyListView = MyDBView.extend({
   initialize: function(options){

    },
    render: function() {
       //stuff here.  I can access this.options here

    },
  dialogResponseYes: function(e){
      //try to get this.options and options is undefined as this has changed to another element (a button)
   }

});

So, how do I get the original context of this?

Was it helpful?

Solution

if you are using events object to bind events to your view like here http://backbonejs.org/#View-delegateEvents everything should be ok, otherwise u can hardly bind them to your view using _.bindAll

initialize: function(options){
_.bindAll(this, "dialogResponseYes");
    },

or

var MyListView = MyDBView.extend({
   initialize: function(options){

    },
    events: {
        'click div': 'dialogResponseYes' //example
    },
    render: function() {


    },
    dialogResponseYes: function(e){

    }

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top