-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds-Core.js
43 lines (38 loc) · 1.49 KB
/
ds-Core.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
(function (global, Backbone, _, $) {
if (!global || !Backbone || !_ || !$) return;
var dependecyQueue = [];
function waitingForDependency (dependencies, callback, context) {
dependecyQueue.push([dependencies, callback, context]);
}
function checkForAvailableDependencies() {
var ds = global.ds, i = 0, q = dependecyQueue, l = q.length, dependencies, d, callback, context, j, k;
for (; i < l; i++) {
if (!q[i]) continue;
dependencies = q[i][0], callback = q[i][1], context = q[i][2];
// Check if all dependencies are available now
for (j=0, k=dependencies.length; d=dependencies[j], j<k; j++) {
if (ds[d] === undefined) { // Nope, at least this one is not
break; // Look no further
} else if (j === k - 1) {
// Ok, we've reached the end of the loop without getting thrown out
// that means: voilá all dependencies are available
delete q[i]; // We've done our bit, no need to call the callback again
callback.call(context);
}
}
}
}
global.ds && global.ds.demand && global.ds.supply || (global.ds = {
demand: function (dependencies, callback, context) {
_.isArray(dependencies) || (dependencies = [dependencies]);
if (dependencies.length && typeof dependencies[0] === "string" && typeof callback === "function") {
waitingForDependency(dependencies, callback, context);
checkForAvailableDependencies();
}
},
supply: function (obj) {
_.extend(global.ds, obj);
checkForAvailableDependencies();
}
});
})(this, Backbone, _, jQuery);