I'm new to Marionette and can't get my head around events...
I have an ItemView which trigger an event, and I would like to receive this event at the application level, but when my app listen for this event, nothing happens...
If the Event Aggregator is in Application and ItemView, why this code doesn't work ? :
var MessageItemView = Backbone.Marionette.ItemView.extend({
template: "#messagesTPL",
tagName: 'tr',
className: 'messageItem',
events : {
'click': 'displayMessage'
},
displayMessage: function () {
this.trigger('display:message');
}
});
App.on('display:message', function () {
console.log('display message !!!');
});
If you want to listen for the event at the application level, you have to trigger it at the application level:
Assuming
myApp
is your Marionette application. Then, you simply listen for that event:It's the only way to implement what you asked for: "I have an ItemView which trigger an event, and I would like to receive this event at the application level". Depending on your situation, the better way is to either
Neither of these 2 solutions requires "polluting" the global event space.
ok so I tried the listenTo solution, here is part of my code:
But when I click on a messageItemView, the regionManager does not execute the displayMessage callback set in the listenTo method.