-
-
Save petersendidit/250559 to your computer and use it in GitHub Desktop.
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
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