Search code examples
jquerymodel-view-controllerbackbone.jsmarionette

Marionette ItemView events - bind multiple functions to single event


Is it possible to bind multiple functions to single event in a Marionette ItemView
eg.

events: { 
"click #div1": "func1 func2" 
}

This doesn't work at all in ItemView. However, its supposed to work fine in CollectionView: https://github.com/marionettejs/backbone.marionette/issues/355.

I also tried

events: { 
"click #div1": "func1",
"click #div1": "func2" 
}

But this results in a call only to func2!


Solution

  • Your events is an object, and Backbone iterates over its keys and since you can't have duplicate keys you can't declare two handlers on the same event.

    You can instead use a single event-handler that again calls the functions you want to invoke.

    events: {
        'click #div1': 'onDiv1Click'
    },
    
    onDiv1Click: function() {
        this.func1();
        this.func2();
    }