Skip to content

Instantly share code, notes, and snippets.

@vincentp
Created December 10, 2009 11:47
Show Gist options
  • Select an option

  • Save vincentp/253290 to your computer and use it in GitHub Desktop.

Select an option

Save vincentp/253290 to your computer and use it in GitHub Desktop.
jQuery.namespace.js : Allow you to store all your javascript in namespaces using jQuery.
/*
* $.namespace
* Allow you to store all your javascript in namespaces.
* Call the namespaces only when you need them and avoid useless Javascript DOM manipulation.
*
* 1. Create your namespaces
* $.namespace([
* 'views',
* 'views.home',
* 'session',
* 'session.login'
* 'session.signup'
* ]);
*
* 2. Create your javascript in the namespaces created above
* $.namespace.views.home = function(){
* // JS script used in my home page
* };
*
* 3. In your different html views, include the required namespace like that :
* <div class="jquery_namespace" name="views.home" />
*
* 4. At the end of your namespaces declaration, load the required javascript like that :
* $.namespace.load();
*/
(function($) {
$.namespace = function(path){
$.namespace = {};
$.each(path, function(key, path){
eval(['$.namespace', '.', path, '=', '{};'].join(''));
eval(['$.namespace', '.', path, '=', 'function(){ };'].join(''));
});
$.namespace.load = function(){
$('.jquery_namespace').livequery(function(){
var path = ['$.namespace', '.', $(this).attr('name')].join('');
eval([path, '(', path, ')', ';'].join(''));
});
};
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment