Skip to content

Instantly share code, notes, and snippets.

@mklabs
Created March 14, 2012 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklabs/2037864 to your computer and use it in GitHub Desktop.
Save mklabs/2037864 to your computer and use it in GitHub Desktop.
Backbone handy walk the application object to bridge event triggered to function calls
(function(exports) {
// change app to whatever.
var app = exports.app = Object.create(_.extend({}, Backbone.Events, {
// map over the backbone api to an EventEmitter like one
emit: Backbone.Events.trigger
}));
// top-level application namespaces
app.controllers = {};
app.model = {};
app.ui = {};
// handy walk the application object to bridge event triggered to function calls
//
// app.emit('ui:panel:change');
// // invoke the app.ui.panel.change method
//
app.on('all', function(ev) {
var parts = ev.split(':'),
args = Array.prototype.slice.call(arguments, 1);
if(parts.length < 2) return;
var memo = app;
parts.forEach(function(name) {
var ns = memo[name];
if(!ns) return;
// invoke!
if(typeof ns === 'function') return ns.apply(memo, args);
// or continue the walk through
memo = ns;
});
});
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment