Created
December 10, 2009 11:47
-
-
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.
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
| /* | |
| * $.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