Created
March 14, 2012 16:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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