-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathproto.js
288 lines (254 loc) · 5 KB
/
proto.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Module dependencies.
*/
try {
var Emitter = require('emitter');
var each = require('each');
} catch (e) {
var Emitter = require('component-emitter');
var each = require('component-each');
}
var request = require('superagent');
var noop = function(){};
/**
* Mixin emitter.
*/
Emitter(exports);
/**
* Expose request for configuration
*/
exports.request = request;
/**
* Register an error `msg` on `attr`.
*
* @param {String} attr
* @param {String} msg
* @return {Object} self
* @api public
*/
exports.error = function(attr, msg){
this.errors.push({
attr: attr,
message: msg
});
return this;
};
/**
* Check if this model is new.
*
* @return {Boolean}
* @api public
*/
exports.isNew = function(){
var key = this.model.primaryKey;
return ! this.has(key);
};
/**
* Get / set the primary key.
*
* @param {Mixed} val
* @return {Mixed}
* @api public
*/
exports.primary = function(val){
var key = this.model.primaryKey;
if (0 == arguments.length) return this[key]();
return this[key](val);
};
/**
* Validate the model and return a boolean.
*
* Example:
*
* user.isValid()
* // => false
*
* user.errors
* // => [{ attr: ..., message: ... }]
*
* @return {Boolean}
* @api public
*/
exports.isValid = function(){
this.validate();
return 0 == this.errors.length;
};
/**
* Return `false` or an object
* containing the "dirty" attributes.
*
* Optionally check for a specific `attr`.
*
* @param {String} [attr]
* @return {Object|Boolean}
* @api public
*/
exports.changed = function(attr){
var dirty = this.dirty;
if (Object.keys(dirty).length) {
if (attr) return !! dirty[attr];
return dirty;
}
return false;
};
/**
* Perform validations.
*
* @api private
*/
exports.validate = function(){
var self = this;
var fns = this.model.validators;
this.errors = [];
each(fns, function(fn){ fn(self) });
};
/**
* Destroy the model and mark it as `.destroyed`
* and invoke `fn(err)`.
*
* Events:
*
* - `destroying` before deletion
* - `destroy` on deletion
*
* @param {Function} [fn]
* @api public
*/
exports.destroy = function(fn){
fn = fn || noop;
if (this.isNew()) return fn(new Error('not saved'));
var self = this;
var url = this.url();
this.model.emit('destroying', this);
this.emit('destroying');
this.request
.del(url)
.set(this.model._headers)
.end(function(err, res){
if (err) return fn(err, res);
self.destroyed = true;
self.model.emit('destroy', self, res);
self.emit('destroy');
fn(null, res);
});
};
/**
* Save and invoke `fn(err)`.
*
* Events:
*
* - `saving` pre-update or save, after validation
* - `save` on updates and saves
*
* @param {Function} [fn]
* @api public
*/
exports.save = function(fn){
if (!this.isNew()) return this.update(fn);
var self = this;
var url = this.model.url();
var key = this.model.primaryKey;
fn = fn || noop;
if (!this.isValid()) return fn(new Error('validation failed'));
this.model.emit('saving', this);
this.emit('saving');
this.request
.post(url)
.set(this.model._headers)
.send(self)
.end(function(err, res){
if (err) return fn(err, res);
if (res.body) self.primary(res.body[key]);
self.dirty = {};
self.model.emit('save', self, res);
self.emit('save');
fn(null, res);
});
};
/**
* Update and invoke `fn(err)`.
*
* @param {Function} [fn]
* @api private
*/
exports.update = function(fn){
var self = this;
var url = this.url();
fn = fn || noop;
if (!this.isValid()) return fn(new Error('validation failed'));
this.model.emit('saving', this);
this.emit('saving');
this.request
.put(url)
.set(this.model._headers)
.send(self)
.end(function(err, res){
if (err) return fn(err, res);
self.dirty = {};
self.model.emit('save', self, res);
self.emit('save');
fn(null, res);
});
};
/**
* Return a url for `path` relative to this model.
*
* Example:
*
* var user = new User({ id: 5 });
* user.url('edit');
* // => "/users/5/edit"
*
* @param {String} path
* @return {String}
* @api public
*/
exports.url = function(path){
var model = this.model;
var url = model._base;
var id = this.primary();
if (0 == arguments.length) return url + '/' + id;
return url + '/' + id + '/' + path;
};
/**
* Set multiple `attrs`.
*
* @param {Object} attrs
* @return {Object} self
* @api public
*/
exports.set = function(attrs){
for (var key in attrs) {
this[key](attrs[key]);
}
return this;
};
/**
* Get `attr` value.
*
* @param {String} attr
* @return {Mixed}
* @api public
*/
exports.get = function(attr){
return this.attrs[attr];
};
/**
* Check if `attr` is present (not `null` or `undefined`).
*
* @param {String} attr
* @return {Boolean}
* @api public
*/
exports.has = function(attr){
return null != this.attrs[attr];
};
/**
* Return the JSON representation of the model.
*
* @return {Object}
* @api public
*/
exports.toJSON = function(){
return this.attrs;
};