0
App.Order = {
    oForm : $('#order-form'),

    expertMode :  false,

    expertModeData : null,

    init : function() {
            $('#toggle-expert-mode').on('click', function() {
                this.switchMode();
            });
    }
}

How do i bind this inside the click event handler. Also is this the right way to add an event listener, i mean inside the init method?

Doing App.Order.switchMode() does not make sense as what happens if i change the object name?

Thanks

  • @FelixKling I think that the question you answered uses a different design pattern.. but helps understand, thanks – user3058010 Dec 02 '13 at 15:56
  • I didn't use any design pattern. I tried to come with the simplest use case possible (maybe I didn't succeed). The point is that it's the same problem and solved by the answer given there. – Felix Kling Dec 02 '13 at 15:57

1 Answers1

2

How do i bind this inside the click event handler.

Literally as you said. You bind(this):

App.Order = {
    // ...
    init : function() {
        $('#toggle-expert-mode').on('click', function() {
            this.switchMode();
        }.bind(this));
    }
}
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • Thank you. I was trying `this.switchMode().bind(this)` – user3058010 Dec 02 '13 at 15:52
  • BTW now when the `this` refers to the object how to i get a reference to the original `this` `#toggle-expert-mode` in this case? Currently i have to do `e.target || e.srcElement` – user3058010 Dec 03 '13 at 12:05