Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created December 7, 2009 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rmurphey/250544 to your computer and use it in GitHub Desktop.
Save rmurphey/250544 to your computer and use it in GitHub Desktop.
jQuery.namespaces : {
myNamespace : '/js/myNamespace/'
};
// this module would be in a separate file at /js/myNamepsace/MyModule.js
$.provide('myNamespace.MyModule');
jQuery.module('myNamespace.MyModule', null, {
defaults : {
hello : 'goodbye',
world : 'world'
},
init : function(config) {
this.config = jQuery.extend(this.defaults, config);
this.node = this.config.node.addClass('module-ized');
},
myMethod : function() {
alert(this.config.hello);
console.log(this.node);
},
myOtherMethod : function() {
alert(this.config.world);
}
});
/*************************************************/
// this code would be separate
jQuery
// load the module from jQuery.config.namespaces.myNamespace + 'MyModule.js'
.require('myNamespace.MyModule')
// when the module is loaded (or if it's already available), do this stuff
.done(function() {
// creating a new instance automatically runs the "init" method,
// which overrides the defaults with the config object
var myModuleInstance = new myNamespace.MyModule({
hello : 'hello',
node : jQuery('#module')
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment