Saurabh Kumar
Saurabh Kumar

Reputation: 16651

Backbone events not working as expected

i have a view

    app.View.FriendRequestButtonView = Backbone.View.extend({

        initialize: function() {
          this.set(a, b, c);
          app.on('foo', this.reject, this);
          app.on('boo', this.accept, this);
    },

    reject: function(){
          this.set(false, false, false);
    },

    accept: function(){
          this.set(true, false, false);
    },

    set: function (a, b, c){
        if(!a && b && c){
            this.events = {
               "click .cancel": "dosomething"
            };
            this.render();
        }
         }
         ........
  } 

When i call normally the view the event "click .cancel": "dosomething" works but when i trigger it via foo or boo the event is not working. What i am doing wrong?

Upvotes: 0

Views: 40

Answers (1)

jgillich
jgillich

Reputation: 76169

A view's events are bound during initialization; setting them later won't work. Call delegateEvents to bind them manually:

 If an events hash is not passed directly, uses this.events as the source. 

Which means the following should work:

this.events = {
    "click .cancel": "dosomething"
};
this.delegateEvents();

this.render();

Upvotes: 1

Related Questions