irfan Munir
irfan Munir

Reputation: 151

Backbone.Marionette and Event Aggregator

Referring URL : http://davidsulc.com/blog/2012/04/22/a-simple-backbone-marionette-tutorial-part-2/

I am naive into backbone and event Aggregator. Can you please let me know the reason to use the following line of code.

this.model.addVote(); MyApp.vent.trigger("rank:down", this.model);

it seems some other possibility could be

this.model.addVote(); this.model(rankDown);

or the other way MyApp.vent.trigger("addVote", this.model

Kindly explain thanks.

Running Sample: http://jsfiddle.net/Irfanmunir/966pG/29/

Upvotes: 1

Views: 2281

Answers (1)

Derick Bailey
Derick Bailey

Reputation: 72858

Events in general are useful for decoupling objects from each other, while still allowing them to communicate. The event aggregator pattern (or pub/sub pattern) at an application level allows further decoupling by having a 3rd party in the mix: publisher, aggregator, subscriber. This way, neither the publisher nor subscriber have to know about each other. They each know about the event aggregator only.

I wrote up a small article on this a while back:

http://lostechies.com/derickbailey/2012/04/03/revisiting-the-backbone-event-aggregator-lessons-learned/

In this case, the events are used because the model needs to be manipulated in the context of the collection that it belongs to. Rather than going through the model to get to the collection (which it might not be directly assigned to... models are not required to be part of a collection), it's easier and more flexible to raise this event and have it handled somewhere more appropriate.

Upvotes: 3

Related Questions