Skip to content

Instantly share code, notes, and snippets.

@asciidisco
Created October 31, 2012 11:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asciidisco/3986542 to your computer and use it in GitHub Desktop.
Save asciidisco/3986542 to your computer and use it in GitHub Desktop.
PubSub with requirejs & backbone
// PubSub impl. with require.js & backbone.js
// events.js
define(['underscore', 'backbone'], function (_, Backbone) {
'use strict';
var events = {};
_.extend(events, Backbone.Events);
return events;
});
// filea.js
define(['events'], function (events) {
'use strict';
events.on('my:event', function (message) {
console.log('Received message: ', message);
});
});
// fileb.js
define(['filea', 'events'], function (filea, events) {
'use strict';
events.trigger('my:event', 'My Message');
});
@trodrigues
Copy link

Is there a specific reason as to why you're storing the instance in a closure scoped variable and then returning it if it's been created? Require.js should cache modules so you should only need to create it once and then you'd always get the same instance (I could be wrong and confusing it with the way node.js does things).

@asciidisco
Copy link
Author

@trodrigues Yap, thx.
You´re right, updated the gist based on your comment.
Require caches & that makes it even simpler :D

@trodrigues
Copy link

Awesome stuff. I'm actually doing some crazy stuff to reroute events from Backbone views to Backbone routers, by wrapping trigger and checking if the event is namespaced with views: or router: and then the specific view or router name. Maybe I should just go and have a global event emitter like this.

@asciidisco
Copy link
Author

I was in a similiar situation & wanted to have a much simpler approach.
Maybe not cleaner, but easier to understand for other devs in the team.

@trodrigues
Copy link

Agreed. This seems less prone to confusion. It's not that "weird behavior" you have to explain later.

@cobbweb
Copy link

cobbweb commented Nov 1, 2012

Seems fine to me, I normally have something like MyAppName.vent which I use for generic PubSub. The only issue with generic PubSub is that while it keeps your code decoupled; it's hard to document and maintain, and come up with a consistent naming convention. How do you normally name your events?

@asciidisco
Copy link
Author

I try to avoid globals as much as possible, so I don´t have a javascript MyAppName global in my current projects.

The application where I use this right now consists of a few modules/widgets, so i go with a naming scheme like:
shell MyWdget:Emitter:operation
For example: shell Comments:Comment:add, when a new comment has been added to the collection of the comments widget.

@asciidisco
Copy link
Author

Whoops, this javascriptand shell things shouldn´t be there, copy & pasten errors...

@mwmwmw
Copy link

mwmwmw commented Jun 26, 2014

This works extremely well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment