/
barebone.js
74 lines (52 loc) · 1.42 KB
/
barebone.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
var _ = require(__dirname + '/vendor/underscore');
var extend = require(__dirname + '/lib/extend');
var Barebone = {};
Barebone.Model = function(attributes) {
attributes || (attributes = {});
this.events = {};
this.attributes = {};
this.set(this.defaults, {silent: true});
this.initialize(attributes);
}
_.extend(Barebone.Model.prototype, {
initialize: function() {},
set: function(attributes, options) {
options || (options = {});
var now = this.attributes;
for(var attr in attributes) {
var value = attributes[attr];
if (now[attr] != value) {
this.attributes[attr] = value;
if (!options['silent']) {
this.trigger('change:'+attr, this, value);
}
}
}
return this;
},
get: function(attribute) {
return this.attributes[attribute];
}
});
Barebone.Events = {
bind: function(event, callback) {
console.log()
this.events[event] || (this.events[event] = [])
this.events[event].push(callback);
},
trigger: function(event) {
var callbacks = this.events[event];
if(!callbacks) {
return this;
}
var args = Array.prototype.slice.call(arguments, 1);
for (i = 0, l = callbacks.length; i < l; i++) {
var callback = callbacks[i];
callback.apply(this, args);
}
return this;
}
}
_.extend(Barebone.Model.prototype, Barebone.Events);
Barebone.Model.extend = extend;
module.exports = Barebone;