Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Marionette Event Aggregator : how to listen at the App level to ItemView Event

Tags:

marionette

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 !!!');
});
like image 991
joe cool Avatar asked Jul 05 '13 16:07

joe cool


People also ask

What is an itemview in marionette?

Class: ItemView - Marionette.js Documentation An ItemView is a view that represents a single item. That item may be a Backbone.Model or may be a Backbone.Collection. Whichever it is though, it will be treated as a single item. An ItemView is a view that represents a single item. That item may be a Backbone.Model or may be a Backbone.Collection.

What is Backbone Radio in marionette?

Backbone Radio. The Backbone Radio provides easy support for a number of messaging patterns for Backbone and Marionette. This is provided through two basic constructs: Radio takes these two constructs and adds the channel implementation - providing namespaces for events and requests.

What is the difference between backbone views and Marionette views?

Unlike Backbone Views, all Marionette views come with a powerful render method. In fact, the primary differences between the views are the differences in their render methods. It goes without saying that it is unwise to override the rendermethod of any Marionette view.

What is the difference between an itemview and a backbone?

That item may be a Backbone.Model or may be a Backbone.Collection. Whichever it is though, it will be treated as a single item. An ItemView is a view that represents a single item. That item may be a Backbone.Model or may be a Backbone.Collection. Whichever it is though, it will be treated as a single item.


2 Answers

If you want to listen for the event at the application level, you have to trigger it at the application level:

displayMessage: function () {
  myApp.trigger('display:message');
}

Assuming myApp is your Marionette application. Then, you simply listen for that event:

myApp.on('display:message', ...)

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

  • use listenTo as Andrew suggested (see an example here: https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/config/marionette/regions/dialog.js). In the example, the dialog region listens to a "dialog:close" event triggered on the view, and reacts to it
  • use a controller to listen to events on the view and react properly, as you can see here: https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L38 The controller reacts to a "contacts:filter" event that was triggered in the view, and reacts to it

Neither of these 2 solutions requires "polluting" the global event space.

like image 158
David Sulc Avatar answered Oct 20 '22 17:10

David Sulc


ok so I tried the listenTo solution, here is part of my code:

var regionManager = Backbone.Marionette.Region.extend({
    el: "#messages",
    onShow: function (view) {
        this.listenTo(view, "display:message", this.displayMessage);
    },
    displayMessage: function () {
        console.log('regionManager received display:message');
    }
});
App.addRegions({
    messageListRegion: regionManager

});
var MessageItemView = Backbone.Marionette.ItemView.extend({
    template: "#messagesTPL",
    tagName: 'tr',
    className: 'messageItem',
    events : {
        'click': 'displayMessage'
    },
    displayMessage: function (e) {
        e.preventDefault();
        e.stopPropagation();
        this.trigger('display:message');
    }
});

But when I click on a messageItemView, the regionManager does not execute the displayMessage callback set in the listenTo method.

like image 21
joe cool Avatar answered Oct 20 '22 16:10

joe cool