-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathunderoop.js
More file actions
97 lines (83 loc) · 2.59 KB
/
underoop.js
File metadata and controls
97 lines (83 loc) · 2.59 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
(function() {
var root = this;
root._sel = {};
function Class() {};
function Module() {};
_.mixin({
// create grouping of functions. Can be used with classes
// to combine functionality.
Module: function(obj) {
var m = new Module;
m._module = true;
_(obj).each(function(v, k) {
m[k] = v;
});
m.toString = function() {
return ["<Module: ", (obj.name || _uniqueId("UnnamedModule")), ">"].join("");
};
return m;
},
// check if an object is a Module
isModule: function(x) {
return x._module === true;
},
// check if an object is using a module
includes: function(x, module) {
return _(module).isModule() && _(x.includes).indexOf(module) != -1;
},
// creates a class. does not support inheritance because inheritance
// and AJAX do not work well together. Use Modules instead.
Class: function(obj) {
obj = obj || {};
// set the name, or default to unique UnnamedClass
// created an array of any included modules
var modules = obj.includes || [],
name = obj.name = (obj.name || _.uniqueId("UnnamedClass")),
methodMaps = _(modules).map(function(x) {
x = _(x).clone(); delete x.name; delete x.toString; delete x._module; return x;
});
obj = _.reduce(methodMaps.concat(obj), function(memo, m) {
return _.extend(memo, m);
}, {});
obj = _(obj).extend({
toString: function() {
return ["<Class: ", name, ">"].join("");
}
});
var klass = function() {
if(_.isFunction(this.initialize)) {
if(_.isFunction(this.initializeBefore)) {
this.initializeBefore();
}
this.initialize.apply(this, arguments);
if(_.isFunction(this.initializeAfter)) {
this.initializeAfter();
}
return this;
} else {
if(console && console.warn) console.warn(name + " does not define an initialize method");
return this;
}
};
klass._name = name;
klass._class = true;
klass._modules = klass._modules = modules;
klass.prototype = new Class;
_(obj).each(function(v, k) {
klass.prototype[k] = v;
if(_.isFunction(v) && _.isUndefined(_sel[k])) {
_sel[k] = function() {
var args = arguments;
return function(x) {
return x[k].apply(x, args);
};
};
}
});
return klass;
},
isClass: function(x) {
return x._class === true;
}
});
})();