Backbone trigger two methods in one event

Naor picture Naor · Nov 13, 2012 · Viewed 12.9k times · Source

I am using Backbone and I have a view with events defined:

    ....
    events: {
        'click .search-button': 'setModelTerm',
        'change .source-select': 'setModelSourceId',
        'change .source-select': 'activateSource'
    },
    ....

I would like to trigger two methods when the event change .source-select fires. The problem is that the last entry in the event object overrides the preceding entry.
How can I trigger two methods in one event?
(I am trying to prevent writing another method that calls those two methods)

Answer

nikoshr picture nikoshr · Nov 13, 2012

You can pass a wrapper function in your hash of events to call your two methods.

From http://backbonejs.org/#View-delegateEvents

Events are written in the format {"event selector": "callback"}. The callback may be either the name of a method on the view, or a direct function body.

Try

events: {
    'click .search-button': 'setModelTerm',
    'change .source-select': function(e) {
        this.setModelSourceId(e);
        this.activateSource(e);
    }
},