-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.js
More file actions
117 lines (110 loc) · 2.53 KB
/
app.js
File metadata and controls
117 lines (110 loc) · 2.53 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var App = App || {};
/*
* CORE-SANDBOX-MODULE Pattern implementation
* see readme.md for references.
*/
/*
* @description Sandbox object, the API of this object is available to modules.
* see readme.md for reference.
*/
App.Sandbox = Class.extend({
initialize: function(core) {
this.publish = core.publish;
this.subscribe = core.subscribe;
this.dom = core.dom;
}
});
/*
* @description Core object, the API of this object is available to sandboxes.
* see readme.md for reference.
* @static
* @param {object} base: base library (jquery is used here).
*/
App.Core = function(_$) {
var moduleData = {},
cache = {},
_dom = {
find: function(selector) {
return _$(selector);
},
wrap: function(element) {
return _$(element);
}
},
_utilities = {
merge: _$.extend,
grep: _$.grep,
inArray: _$.inArray,
each: _$.each
};
return {
dom: _dom,
utilities: _utilities,
register: function(moduleId, creator, options) {
moduleData[moduleId] = {
creator: creator,
instance: null,
options: options || {}
};
},
/**
* Starts a single module
* @param {string} moduleId The module identifier
*/
start: function(moduleId) {
console.log("Starting " + moduleId);
moduleData[moduleId].instance = new moduleData[moduleId].creator(new App.Sandbox(this), moduleData[moduleId].options);
moduleData[moduleId].instance.create();
},
stop: function(moduleId) {
var data = moduleData[moduleId];
if (data.instance) {
data.instance.destroy();
data.instance = null;
}
},
startAll: function() {
for (var moduleId in moduleData) {
if (moduleData.hasOwnProperty(moduleId)) {
this.start(moduleId);
}
}
},
stopAll: function() {
for (var moduleId in moduleData) {
if (moduleData.hasOwnProperty(moduleId)) {
this.stop(moduleId);
}
}
},
publish: function(message, args) {
try {
var i;
for (i = 0; i < cache[message].length; i++) {
if (typeof args === "undefined") { args = []; }
if (!(args instanceof Array)) {
args = [args];
}
cache[message][i].apply(this, args);
};
} catch (err) {
console.log(err);
}
},
subscribe: function(message, callback) {
if (!cache[message]) {
cache[message] = [];
}
cache[message].push(callback);
return [message, callback];
},
unsubscribe: function(handle) {
var t = handle[0];
base.each(cache[t], function(idx) {
if (this == handle[1]) {
cache[t].splice(idx, 1);
}
});
}
};
} (jQuery);