-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (171 loc) · 4.07 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* model
* Model model
*
* @copyright 2013 Enrico Marino and Federico Spini
* @license MIT
*/
/**
* Expose component
*/
module.exports = function () {
/**
* Component dependencies
*/
var Emitter = require('emitter');
/**
* @constructor Model
* @description Create a model.
* @param {Object} options options
* @param {Object} [options.attributes] attributes
* @return {Model} a model
*/
function model (attributes, options) {
if (!(this instanceof model)) {
return new model(attributes, options);
}
Emitter.call(this);
attributes = attributes || {};
options = options || {};
this.collection = options.collection;
this.attributes = {};
this.set_all(attributes, options);
}
/**
* use
* use plugin
*
* @param {Function} fn plugin
* @return {model} this dor chaining
* @api public
*/
model.use = function (fn) {
fn(this);
return this;
};
/**
* Inherit from `Emitter`
*/
model.prototype = Object.create(Emitter.prototype);
model.prototype.constructor = model;
/**
* get
* Get the attribute value.
*
* @param {String} key attribute name
* @return {Mixed} the attribute value
* @api public
*/
model.prototype.get = function (key) {
return this.attributes[key];
};
/**
* set
* Set the attribute value.
*
* @param {String} key attribute name
* @param {Mixed} value attribute value
* @param {Object} options options
* @param {Boolean} [options.silent]
* @return {model} this for chaining
* @api public
*/
model.prototype.set = function (key, value, options) {
if (key == null) {
return this;
}
if (typeof key === 'object') {
return this.set_all(key, value);
}
var attributes = this.attributes;
var silent = options && options.silent;
var previous = attributes[key];
var created = !(key in attributes);
var changed = created || previous !== value;
if (changed) {
attributes[key] = value;
}
if (changed && !silent) {
this.emit('change:' + key, this, value, previous);
this.emit('change', this);
}
return this;
};
/**
* set_all
* Set the attribute values.
*
* @param {Object} values attribute values
* @param {Object} options options
* @param {Boolean} [options.silent]
* @return {model} this for chaining
* @api public
*/
model.prototype.set_all = function (values, options) {
if (values == null) {
return this;
}
var silent = options && options.silent;
var attributes = this.attributes;
var key;
var value;
var previous;
var changed;
var created;
var emitted;
for (key in values) {
previous = attributes[key];
value = values[key];
created = !(key in attributes);
changed = created || previous !== value;
if (changed) {
attributes[key] = value;
}
if (changed && !silent) {
this.emit('change:' + key, this, value, previous);
emitted = true;
}
}
if (emitted) {
this.emit('change', this);
}
return this;
};
/**
* del
* Delete attribute `key`.
*
* @param {String} key key
* @param {Object} options options
* @param {Boolean} [options.silent]
* @return {model} this for chaining.
* @api public
*/
model.prototype.del = function (key, options) {
var attributes = this.attributes;
var silent = options && options.silent;
var value = attributes[key];
var present = value != null;
if (present) {
delete attributes[key];
}
if (present && !silent) {
this.emit('delete:' + key, this, value);
}
return this;
};
/**
* has
* Return `true` if attributes contains `key`, `false` otherwise.
*
* @param {String} key key
* @return {Boolean}
* `true` if attributes contains `key`,
* `false` otherwise.
* @api public
*/
model.prototype.has = function (key) {
return this.attributes[key] != null;
};
return model;
}