Skip to content

Instantly share code, notes, and snippets.

@hernan
Forked from cowboy/ba-backbone-module.js
Created October 10, 2011 19:13
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 hernan/1276232 to your computer and use it in GitHub Desktop.
Save hernan/1276232 to your computer and use it in GitHub Desktop.
Idea for a Backbone module system (allowing modules to be accessed across multiple files, possibly loaded out of order). Inspired by https://gist.github.com/1202511
/*!
* Backbone Module Manager - v0.1pre - 9/9/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
function ModuleManager(fn) {
// Create an internal module cache.
this._modules = {};
// The initialization function.
this._init = fn;
}
// Access a module (inititalizing if necessary).
ModuleManager.prototype.get = function(name) {
return this._modules[name] || (this._modules[name] = this._init(name));
};
// An easy interface to accessing a module. Basically, sugar around how someone
// might implement "new ModuleManager(fn)" on their own.
ModuleManager.Module = function(init) {
// This is the function you'll use to access modules.
function fn(name) {
// Get all args but the first.
var args = Array.prototype.slice.call(arguments, 1);
// Get a new module for this name.
var module = fn.modules.get(name);
// If a function was passed,
if ( args.length ) {
// Call the passed function (the last argument) passing in the module,
// followed by any arguments passed in BETWEEN the module name and that
// function.
return args.pop().apply(this, [module].concat(args));
}
return module;
}
// Initialize ModuleManager as a property of the function.
fn.modules = new ModuleManager(init);
return fn;
};
// You could use this the basic way.
var modules = new ModuleManager(function(name) {
return {foo: 111};
});
// Modules are created when first accessed.
modules.get('module-one').bar = 222;
console.log(modules.get('module-one').foo); // logs: 111
console.log(modules.get('module-one').bar); // logs: 222
console.log(modules.get('module-two').foo); // logs: 111
console.log(modules.get('module-two').bar); // logs: undefined
// Or you can do it like the cool kids do it:
// Simulating Backbone.
var Backbone = {};
// Create a Backbone-specific module manager with sensible defaults.
Backbone.Module = ModuleManager.Module(function(name) {
return {Views: {}};
});
// Setup.
var test = 123;
// Again, modules are created when first accessed.
Backbone.Module('module-one').Views.test = 456;
// IIFEs work, but they're not always pretty.
(function(module, global, num) {
console.log(global.test); // logs: 123
console.log(module.Views.test); // logs: 456
console.log(num); // logs: 789
}(Backbone.Module('module-one'), this, 789));
// Perhaps this syntax is a little sweeter?
Backbone.Module('module-one', this, 789, function(module, global, num) {
console.log(global.test); // logs: 123
console.log(module.Views.test); // logs: 456
console.log(num); // logs: 789
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment