4
votes

I want to implement a custom vent event aggregator instance with requirejs as explainer here

Looking at examples here and in the docs, I've seent that calls to vent.on and vent.trigger are mainly used in views. My pattern would then be:

define(['marionette', 'vent'], function (Marionette, vent) {
    return Marionette.ItemView.extend({
        initialize: function () {
           //bind
           this.listenTo(vent, 'mycustomevent', this.myMethod);
           //trigger
           vent.trigger('viewinit', ...);
        }
    });
});

Is this pattern correct (views are responsible for managing aggregator events) or should i use it on Models and Collections?

1

1 Answers

5
votes

The event aggregator really is just a pub/sub system for communication.

Regarding "what should go where", I'd suggest the following in most cases:

  • views trigger events (according to what the user has clicked, e.g.)
  • controllers listen for and react to events (deleting a model, e.g.)

Of course, there are many ways to use the event aggregator, but when dealing with views, the above method fits most use cases.

Using the event aggregator is also useful to manage routing events and remove duplication (see section "Implementing routing" here: http://samples.leanpub.com/marionette-gentle-introduction-sample.pdf )