Created
December 7, 2009 02:05
-
-
Save rmurphey/250544 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.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