-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathstatic.js
201 lines (177 loc) · 3.55 KB
/
static.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
/**
* Module dependencies.
*/
try {
var Collection = require('collection');
} catch (e) {
var Collection = require('component-collection');
}
var request = require('superagent');
var noop = function(){};
/**
* Expose request for configuration
*/
exports.request = request;
/**
* Construct a url to the given `path`.
*
* Example:
*
* User.url('add')
* // => "/users/add"
*
* @param {String} path
* @return {String}
* @api public
*/
exports.url = function(path){
var url = this._base;
if (0 == arguments.length) return url;
return url + '/' + path;
};
/**
* Set base path for urls.
* Note this is defaulted to '/' + modelName.toLowerCase() + 's'
*
* Example:
*
* User.route('/api/u')
*
* @param {String} path
* @return {Function} self
* @api public
*/
exports.route = function(path){
this._base = path;
return this;
}
/**
* Add custom http headers to all requests.
*
* Example:
*
* User.headers({
* 'X-CSRF-Token': 'some token',
* 'X-API-Token': 'api token
* });
*
* @param {String|Object} header(s)
* @param {String} value
* @return {Function} self
* @api public
*/
exports.headers = function(headers){
for(var i in headers){
this._headers[i] = headers[i];
}
return this;
};
/**
* Add validation `fn()`.
*
* @param {Function} fn
* @return {Function} self
* @api public
*/
exports.validate = function(fn){
this.validators.push(fn);
return this;
};
/**
* Use the given plugin `fn()`.
*
* @param {Function} fn
* @return {Function} self
* @api public
*/
exports.use = function(fn){
fn(this);
return this;
};
/**
* Define attr with the given `name` and `options`.
*
* @param {String} name
* @param {Object} options
* @return {Function} self
* @api public
*/
exports.attr = function(name, options){
this.attrs[name] = options || {};
// implied pk
if ('_id' == name || 'id' == name) {
this.attrs[name].primaryKey = true;
this.primaryKey = name;
}
// getter / setter method
this.prototype[name] = function(val){
if (0 == arguments.length) return this.attrs[name];
var prev = this.attrs[name];
this.dirty[name] = val;
this.attrs[name] = val;
this.model.emit('change', this, name, val, prev);
this.model.emit('change ' + name, this, val, prev);
this.emit('change', name, val, prev);
this.emit('change ' + name, val, prev);
return this;
};
return this;
};
/**
* Remove all and invoke `fn(err)`.
*
* @param {Function} [fn]
* @api public
*/
exports.destroyAll = function(fn){
fn = fn || noop;
var self = this;
var url = this.url('');
this.request
.del(url)
.set(this._headers)
.end(function(err, res){
if (err) return fn(err, null, res);
fn(null, [], res);
});
};
/**
* Get all and invoke `fn(err, array)`.
*
* @param {Function} fn
* @api public
*/
exports.all = function(fn){
var self = this;
var url = this.url('');
this.request
.get(url)
.set(this._headers)
.end(function(err, res){
if (err) return fn(err, null, res);
var col = new Collection;
for (var i = 0, len = res.body.length; i < len; ++i) {
col.push(new self(res.body[i]));
}
fn(null, col, res);
});
};
/**
* Get `id` and invoke `fn(err, model)`.
*
* @param {Mixed} id
* @param {Function} fn
* @api public
*/
exports.get = function(id, fn){
var self = this;
var url = this.url(id);
this.request
.get(url)
.set(this._headers)
.end(function(err, res){
if (err) return fn(err, null, res);
var model = new self(res.body);
fn(null, model, res);
});
};