Skip to content

Commit cce7a0d

Browse files
committed
added required
1 parent 4c05ae5 commit cce7a0d

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

jquery-class/jquery.class.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
/**
22
* JQuery Class
3-
* version 2.0.5
3+
* version 2.0.6
44
*/
55
$.Class = function(definition) {
66
var registerNameSpace = function(ns, ptr) {
77
var current = ns.shift();
8-
if (typeof ptr[current] === 'undefined') { ptr[current] = {}; }
8+
typeof ptr[current] === 'undefined' ? ptr[current] = {} : null;
99
return ns.length > 0 ? registerNameSpace(ns, ptr[current]) : ptr;
10-
}
11-
12-
var Class = function() {
13-
for (var key in definition) {
14-
if (/^init/.test(key) && typeof definition[key] === 'function') definition[key].apply(this, arguments);
10+
}, Class = function() {
11+
var initialize = function(def, self, args) {
12+
pointer = registerNameSpace(def.namespace.split('.'), window);
13+
$.extend(true, pointer[def.namespace.split('.').pop()], self);
14+
for (var key in def) (/^init/.test(key) && typeof def[key] === 'function') ? def[key].apply(self, args) : null;
15+
$.extend(true, pointer[def.namespace.split('.').pop()], self);
16+
window.loadedClass ? window.loadedClass.push('required.' + def.namespace) : window.loadedClass = ['required.' + def.namespace];
17+
$(document).trigger('required.' + def.namespace);
18+
};
19+
if (typeof definition.required === 'object') {
20+
var self = this, events = [];
21+
for(var x in definition.required) {
22+
events.push('required.' + definition.required[x]);
23+
}
24+
$(document).on(events.join(' '), {def: definition, args: arguments}, function(e) {
25+
if ($.grep(window.loadedClass, function(x) {return $.inArray(events)}).length === events.length) {
26+
initialize(e.data.def, self, e.data.args);
27+
}
28+
});
29+
} else {
30+
initialize(definition, this, arguments);
1531
}
1632
};
17-
1833
Class.prototype = definition;
1934
Class.prototype.constructor = Class;
20-
2135
if (definition.consts && typeof definition.consts === 'object') {
2236
$.each(definition.consts, function (constant, value) {
2337
Class.prototype[constant] = $.proxy(function (cons) { return cons; }, this, value);
2438
});
2539
}
26-
27-
var pointer = registerNameSpace(definition.namespace.split('.'), window);
28-
29-
$.extend(pointer[definition.namespace.split('.').pop()], definition);
30-
return $.extend(pointer[definition.namespace.split('.').pop()], new Class());
31-
};
32-
40+
new Class();
41+
};

0 commit comments

Comments
 (0)