Skip to content

Instantly share code, notes, and snippets.

@petersendidit
Forked from rmurphey/gist:250544
Created December 7, 2009 02:52
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 petersendidit/250559 to your computer and use it in GitHub Desktop.
Save petersendidit/250559 to your computer and use it in GitHub Desktop.
jQuery.config = {
namespaces : {
myNamespace : '/js/myNamespace/'
}
};
// this module would be in a separate file at /js/myNamepsace/MyModule.js
jQuery.module('myNamespace.MyModule', null, {
config:{
defaults : {
hello : 'goodbye',
world : 'world'
},
}
_init : function() {
this.element.addClass('module-ized');
},
myMethod : function() {
alert(this.options.hello);
console.log(this.element);
},
myOtherMethod : function() {
alert(this.options.world);
}
});
/*************************************************/
// this code would be separate
jQuery
// load the module from jQuery.config.namespaces.myNamespace + 'MyModule.js'
.loadModule('myNamespace.MyModule', function() {
// when the module is loaded (or if it's already available), do this stuff
// creating a new instance automatically runs the "_init" method,
// and makes this.options a merge of the default and provided options
var myModuleInstance = jQuery('#module').myNamespace.MyModule({
hello : 'hello'
});
// click on #foo will alert hello, log the node #module, and then alert world
jQuery('#foo').bind('click', {target: myModuleInstance, action: 'myMethod'});
// click on #bar will just alert world
jQuery('#bar').bind('click', {target: myModuleInstance, action: 'myOtherMethod'});
// will alert hello and log the node #module
myModuleInstance.myNamespace.MyModule('myMethod');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment