-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoduleExec.js
74 lines (63 loc) · 2.47 KB
/
moduleExec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* To use <body data-module='users' data-action='show'>
*
* Only a single module can be initialized per page, if that page requires other modules
* then there should be a "page" module that initializes all of the modules for that page,
* and that page module is what would be initialized via this method.
*
* Based heavily on the work by Paul Irish and Jason Garber
* - http://www.paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/
* - http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution
*
*/
;(function (baseNamespace, moduleNamespace){
var DEFAULT_MODULE_NAME = 'common'; // this should init everything that inits every time
// setup defaults if namespaces aren't passed in or are empty
if (!baseNamespace) {
baseNamespace = window.app || {};
baseNamespace.util = baseNamespace.util || {};
}
if (!moduleNamespace) {
moduleNamespace = baseNamespace.util;
}
/**
* util.init, when passed a single argument, defaults to calling an 'init' function
* (should it exist). Otherwise, util.init will execute appNamespace.module.action().
*
*/
function init() {
var body = document.body,
module = body.getAttribute('data-module'),
action = body.getAttribute('data-action');
exec(DEFAULT_MODULE_NAME);
exec(module);
exec(module, action);
}
/**
* util.exec accepts a module and action, and executes that method
*
* @param {string} module the module name
* @param {string} action If the action is omitted, the init() method will be executed
*
*/
function exec(module, action) {
var ns = baseNamespace;
action = (action === undefined) ? 'init' : action;
if (module !== '' && ns[module] && typeof ns[module][action] == 'function') {
ns[module][action]();
}
}
// add this module last and self-execute
init();
// could also tell init to run on Document Ready via jQuery if jQuery is available
// The reason for using this would be to avoid having to make sure this module is concatenated
// at the end of all of the JavaScript files
// $(init);
moduleNamespace.moduleExec = {
init: init
};
// Pass in the base namespace and where to attach the module as part of the IIFE
// Example does not have it because there is no surrounding app/namespace
// i.e.
// })(app, app.util);
})();