3 ms·
Lets Talk; An EventBus in Backbone.js
- shinuza 15y agoI've being using this pattern since day one with Backbone.js. If you want to avoid spaghetti code you need to have a central mediator that will take care of the communication between your modules. It's also easier to debug if all your events business happens in one place.
- pdelgallego 15y agoA central event emitter is common practice in client software. It is also known as the event aggregator pattern. A nice description by Martin Fowler[1] [1] http://martinfowler.com/eaaDev/EventAggregator.html http://martinfowler.com/eaaDev/EventAggregator.html
- yiran 15y agoCheck out this post of an event aggregation implementation in Backbone: http://devlicio.us/blogs/mike_nichols/archive/2011/08/14/backbone-event-aggregator-extended.aspx http://devlicio.us/blogs/mike_nichols/archive/2011/08/14/bac...
- Lapsa 15y agoNB: Use with caution Some pitfalls: - easy to create zombie objects - easy to break sub-view re-usability - gl hf if you have managed to end up with x-references
- troygoode 15y agoI'm using a statechart library (Stativus[1]) in a similar capacity, but with... states. [1] https://github.com/etgryphon/stativus https://github.com/etgryphon/stativus
- HerroRygar 15y agoI also had this exact problem. I posted this question on StackOverflow about it, and the accepted answer led me to the solution I use now, which is identical to the one described in the article: http://stackoverflow.com/questions/9405148/how-can-i-bubble-up-events-in-a-backbone-view-hierarchy http://stackoverflow.com/questions/9405148/how-can-i-bubble-... ...I always just do something along the lines of window.MyAppNamespace.EventProxy = _.extend({}, Backbone.Events); The nice thing about this tactic is that it loosely couples interactions between your views. A coworker who also uses Backbone.js prefers passing references between your views to each other. Although this makes the code easier to read in some cases, since it's more obvious what the flow of control is, it tightly couples the components.
- swampthing 15y agoThe Backbone documentation actually mentions this too (last sentence of the introductory paragraph here: http://documentcloud.github.com/backbone/#Events http://documentcloud.github.com/backbone/#Events).
- HerroRygar 15y agoThat's interesting...I can't believe I never noticed that.
- elchief 15y agowow, backbone is catching up to what Sencha ExtJS had five years ago.
- deleted 15y ago[deleted]
- gordonslondon 15y agoNote that Backbone.Collection also extends Backbone.Events so you can use your collection has a global mediator. Generally a collection is shared across multiple views.