forked from tnhu/jsface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsface.ready.js
50 lines (44 loc) · 1.44 KB
/
jsface.ready.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
/*
* JSFace Object Oriented Programming Library - Ready plugin
* https://github.com/tnhu/jsface
*
* Copyright (c) 2009-2012 Tan Nhu
* Licensed under MIT license (https://github.com/tnhu/jsface/blob/master/LICENSE.txt)
*/
(function(context) {
var jsface = context.jsface || require("./jsface"),
Class = jsface.Class,
functionOrNil = jsface.functionOrNil,
readyFns = [],
readyCount = 0;
Class.plugins.$ready = function invoke(clazz, parent, api, loop) {
var r = api.$ready,
len = parent ? parent.length : 0,
count = len,
_super = len && parent[0].$super,
pa, i, entry;
// find and invoke $ready from parent(s)
while (len--) {
for (i = 0; i < readyCount; i++) {
entry = readyFns[i];
pa = parent[len];
if (pa === entry[0]) {
entry[1].call(pa, clazz, parent, api);
count--;
}
if ( !count) { break; }
}
}
// call $ready from grandparent(s), if any
if (_super) {
invoke(clazz, [ _super ], api, true);
}
// in an environment where there are a lot of class creating/removing (rarely)
// this implementation might cause a leak (saving pointers to clazz and $ready)
if ( !loop && functionOrNil(r)) {
r.call(clazz, clazz, parent, api); // invoke ready from current class
readyFns.push([ clazz, r ]);
readyCount++;
}
};
})(this);