-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
170 lines (143 loc) · 3.06 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
/** !
* model-sync
*
* @author Enrico Marino and Federico Spini
* @copyright 2013 Enrico Marino and Federico Spini
* @licence MIT
*/
/**
* Expose component.
*/
module.exports = function (model) {
/**
* Component dependencies.
*/
model.request = require('request');
/**
* root
*/
model.prototype.root = '';
/**
* primary_key
*/
model.prototype.primary_key = '_id';
/**
* id
* Get the model id.
*
* @return {String} the model id.
* @api public
*/
model.prototype.id = function () {
if (this._id) {
return this._id;
}
this._id = this.attributes[this.primary_key];
return this._id;
};
/**
* url
* Get the model url.
*
* @return {String} the model url.
* @api public
*/
model.prototype.url = function () {
if (this._url) {
return this._url;
}
var url = this.root;
var collection = this.collection;
var id = this.id();
if (collection) {
url += collection.url();
}
if (id) {
url += '/';
url += encodeURIComponent(id);
}
url = url.replace(/\/\/+/, '/');
this._url = url;
return url;
};
/**
* save
*
* @param {Function} callback
* @param {Object} err error
* @param {Object} res response
* @param {Object} context
* @api public
*/
model.prototype.save = function (callback, context) {
callback = callback || function () {};
var model = this;
var id = model.id();
var url = model.url();
var data = model.attributes;
if (id) {
model.update(callback, context);
return;
}
model.request
.post(url)
.send(data)
.end(function (res) {
if (res.ok) {
callback.call(context, null, res.body);
} else {
callback.call(context, res.text, null);
}
});
};
/**
* update
*
* @param {Function} callback
* @param {Object} err error
* @param {Object} res response
* @param {Object} context
* @api private
*/
model.prototype.update = function (callback, context) {
callback = callback || function () {};
var model = this;
var url = model.url();
var data = model.attributes;
model.request
.put(url)
.send(data)
.end(function (res) {
if (res.ok) {
callback.call(context, null, res.body);
} else {
callback.call(context, res.text, null);
}
});
};
/**
* fetch
*
* @param {Function} callback
* @param {Object} err error
* @param {Object} res response
* @param {Object} context
* @api public
*/
model.prototype.fetch = function (callback, context) {
callback = callback || function () {};
var model = this;
var url = model.url();
model.request
.get(url)
.end(function (res) {
if (res.ok) {
model.set(res.body);
callback.call(context, null, model);
} else {
callback.call(context, res.text, null);
}
});
};
return model;
}