Skip to content

Commit

Permalink
invoke $ready in multiple level inheritance
Browse files Browse the repository at this point in the history
Asume Bar3 extends Bar2 extends Bar1 extends Foo. Foo defines $ready:

Bar3 -> Bar2 -> Bar1 -> Foo:$ready

Then $ready on Foo will be executed four times:

1- On Foo's level
2- On Bar1's level
3- On Bar2's level
4- On Bar3's level

This support makes sure grandparents get notified when their inherit classes are created
  • Loading branch information
tnhu committed Aug 14, 2013
1 parent 4e0c04f commit fa6bc9d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions jsface.ready.js
Expand Up @@ -14,12 +14,14 @@
readyFns = [],
readyCount = 0;

Class.plugins.$ready = function(clazz, parent, api) {
var r = api.$ready,
len = parent ? parent.length : 0,
count = len,
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];
Expand All @@ -34,10 +36,15 @@
}
}

// 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 (isFunction(r)) {
r.call(clazz, clazz, parent, api);
if ( !loop && isFunction(r)) {
r.call(clazz, clazz, parent, api); // invoke ready from current class
readyFns.push([ clazz, r ]);
readyCount++;
}
Expand Down

0 comments on commit fa6bc9d

Please sign in to comment.