-
Notifications
You must be signed in to change notification settings - Fork 4
/
base.js
114 lines (99 loc) · 2.64 KB
/
base.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Wheel._Class.subclass('Wheel.Base', {
initialize: function(opts) {
this._preInit(opts);
this.init();
this._postInit();
},
_preInit: function(opts) {
this._uid = this._class.uid();
this.optionize(opts);
},
_postInit: function() {
this.listen();
},
optionize: function(opts) {
var normalOpts = {};
var opt;
for( opt in opts ) {
if ( typeof this._class.prototype[opt] == 'function' ) {
// is a property
this['_'+opt] = opts[opt];
} else {
this[opt] = opts[opt];
}
}
},
init: function() {
// overloaded by subclasses
},
listen: function() {
// yup, this too
},
publish: function(eventType, eventData) {
this._publisher || this._findPublisher();
this._publisher.trigger(eventType, eventData);
},
subscribe: function(eventName, callback, context) {
this._publisher || this._findPublisher();
this._publisher.on(eventName, callback, context || this);
},
_findPublisher: function() {
this._publisher = (this._class.App && this._class.App.app) || Wheel.Publisher;
if (!this._publisher) {
throw "Cannot find app or Wheel.Publisher. Please namespace your classes under an application class or assign a Wheel.Publisher";
}
}
}, {
uid: function() {
Wheel.Base._uid = Wheel.Base._uid || 0;
return ++ Wheel.Base._uid;
},
build: function() {
var klass = this;
function Class(args) {
return klass.apply(this, args);
}
Class.prototype = klass.prototype;
return new Class(arguments);
},
attrAccessor: function(prop) {
var propId = '_'+prop;
this.prototype[prop] = function(value){
if (value !== undefined) {
this[propId] = value;
if (self[propId] != value) {
this.trigger('change');
this.trigger('change:'+prop);
}
}
return this[propId];
};
},
subclass: function(name, iprops, cprops) {
var klass = this._subclass(name, iprops, cprops);
if (klass.properties && klass.properties.length) {
$.each(klass.properties, function(i, prop) {
if ( typeof klass.prototype[prop] !== 'function' ) {
klass.attrAccessor(prop);
}
});
}
if (Wheel.isString(name)) {
var path = name.split('.');
var length = path.length;
var i, base = window;
for (i = 0; i < length-1; i++) {
base = window[path[i]];
if (base && base._typeof === 'Wheel.App') {
klass.App = base;
break;
}
}
}
return klass;
}
});
Wheel.Base.mixin(Wheel.Mixins.Events);
Wheel.Class = function(x, y, z) {
return Wheel.Base.subclass(x, y, z);
};