Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fire an event from one view to another in backbone

Tags:

I have a main view and inside that main view I have another view that gets created when I click a button. Is there a way to listen for a custom event on the parent view for an event that is fired from the child view. I tried to do it through the el property using jQuery trigger but that didn't quite work.

like image 581
Chapsterj Avatar asked Oct 20 '11 23:10

Chapsterj


1 Answers

Ya, no problem... you'll want to use the "Event Aggregator" pattern. It's 1 line of code in Backbone:

var eventAgg = _.extend({}, Backbone.Events);

Now can you trigger / bind to events from this object, everywhere in your app, and have the different parts of your app communicate with each other in a decoupled manner.

I use this a LOT!

I also blogged more about it here: http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

like image 56
Derick Bailey Avatar answered Oct 24 '22 03:10

Derick Bailey