Reputation: 352
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?
Upvotes: 4
Views: 2494
Reputation: 25994
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:
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 )
Upvotes: 5