-
Notifications
You must be signed in to change notification settings - Fork 0
/
jeroenjs.js
116 lines (92 loc) · 2.74 KB
/
jeroenjs.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
115
116
//
// Simple implementation of the model-view-controller pattern for JavaScript applications.
// Depends on underscore (http://underscorejs.org) v1.4.4
//
//
// Event bus for registering and triggering events
//
var Events = {
on: function(name, callback) {
this._events || (this._events = {});
var events = this._events[name] || (this._events[name] = []);
events.push({ callback: callback });
},
trigger: function(name, args) {
if (!this._events || !this._events[name]) return this;
var events = this._events[name];
_.each(events, function(event) {
event.callback(args);
});
}
};
//
// Model to hold domain values
//
var Model = function(attributes) {
this._attributes = {};
this.set(attributes || {});
this.initialize.apply(this);
}
_.extend(Model.prototype, Events, {
initialize: function() {
// Overwrite to add functionality
},
get: function(name) {
return this._attributes[name];
},
set: function(attributes) {
if (!attributes) return this;
var changed = false;
var model = this;
_.each(_.keys(attributes), function(name) {
var curValue = model._attributes[name];
var newValue = attributes[name];
if (curValue !== newValue) {
model._attributes[name] = newValue;
model.trigger('change:' + name, newValue);
changed = true;
}
});
if (changed) {
this.trigger('change');
}
}
});
//
// Controller used to integrate between view and model
//
var Controller = function(attributes) {
this.initialize.apply(this, attributes);
}
_.extend(Controller.prototype, Events, {
initialize: function(attributes) {
// Overwrite to add functionality
}
});
//
// Allows simple inheritance of base classes
//
extend = function(protoProps, staticProps) {
var parent = this;
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your extend definition), or defaulted by
// us to simply call the parent's constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function() { return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from parent, without calling parent's constructor function.
var Surrogate = function() { this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
// Add prototype properties (instance properties) to the subclass, if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
// Set a convenience property in case the parent's prototype is needed later.
child.__super__ = parent.prototype;
return child;
}
Model.extend = Controller.extend = extend;