-
Notifications
You must be signed in to change notification settings - Fork 27
/
backbone-pouch.js
250 lines (219 loc) · 6.95 KB
/
backbone-pouch.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
/*
* backbone-pouch
* http://jo.github.io/backbone-pouch/
*
* Copyright (c) 2013 Johannes J. Schmidt
* Licensed under the MIT license.
*/
(function(root) {
'use strict';
var BackbonePouch;
if (typeof exports === 'object') {
BackbonePouch = exports;
} else {
BackbonePouch = root.BackbonePouch = {};
}
// Require Underscore, if we're on the server, and it's not already present.
var _ = root._;
if (!_ && (typeof require === 'function')) {
_ = require('underscore');
}
var methodMap = {
'create': 'post',
'update': 'put',
'patch': 'put',
'delete': 'remove'
};
BackbonePouch.defaults = {
fetch: 'allDocs',
listen: false,
options: {
post: {},
put: {},
get: {},
remove: {},
allDocs: {},
query: {},
spatial: {},
changes: {
continuous: true
}
}
};
// inspired from https://github.com/Raynos/xtend
function extend() {
var target = {};
for (var i = 0; i < arguments.length; i++) {
var source = arguments[i];
if (typeof source !== 'object') {
continue;
}
for (var name in source) {
if (source[name] && target[name] && typeof source[name] === 'object' && typeof target[name] === 'object' && name !== 'db') {
target[name] = extend(target[name] || {}, source[name]);
} else {
target[name] = source[name];
}
}
}
return target;
}
// backbone-pouch sync adapter
BackbonePouch.sync = function(defaults) {
defaults = defaults || {};
defaults = extend(BackbonePouch.defaults, defaults);
var adapter = function(method, model, options) {
options = options || {};
options = extend(defaults, model && model.pouch || {}, options);
// This is to get the options (especially options.db)
// by calling model.sync() without arguments.
if (typeof method !== 'string') {
return options;
}
// ensure we have a pouch db adapter
if (!options.db) {
throw new Error('A "db" property must be specified');
}
function callback(err, response) {
if (err) {
return options.error && options.error(err);
}
if (method === 'create' || method === 'update' || method === 'patch') {
response = {
_id: response.id,
_rev: response.rev
};
}
if (method === 'delete') {
response = {};
}
if (method === 'read') {
if (options.listen && (model instanceof Backbone.Collection)) {
// TODO:
// * implement for model
// * allow overwriding of since.
_.result(options, 'db').info(function(err, info) {
// get changes since info.update_seq
_.result(options, 'db').changes(_.extend({}, options.options.changes, {
since: info.update_seq,
onChange: function(change) {
var todo = model.get(change.id);
if (change.deleted) {
if (todo) {
todo.destroy();
}
} else {
if (todo) {
todo.set(change.doc);
} else {
model.add(change.doc);
}
}
// call original onChange if present
if (typeof options.options.changes.onChange === 'function') {
options.options.changes.onChange(change);
}
}
}));
});
}
}
return options.success && options.success(response);
}
model.trigger('request', model, _.result(options, 'db'), options);
if (method === 'read') {
// get single model
if (model.id) {
return _.result(options, 'db').get(model.id, options.options.get, callback);
}
// query view or spatial index
if (options.fetch === 'query' || options.fetch === 'spatial') {
if (!options.options[options.fetch].fun) {
throw new Error('A "' + options.fetch + '.fun" object must be specified');
}
return _.result(options, 'db')[options.fetch](
options.options[options.fetch].fun,
options.options[options.fetch]
).then( function(resp) {
callback(null, resp);
}).catch(function(err) {
callback(err);
});
}
// allDocs or spatial query
_.result(options, 'db')[options.fetch](options.options[options.fetch], callback);
} else {
_.result(options, 'db')[methodMap[method]](model.toJSON(), options.options[methodMap[method]], callback);
}
return options;
};
adapter.defaults = defaults;
return adapter;
};
BackbonePouch.attachments = function(defaults) {
defaults = defaults || {};
function getPouch(model) {
if (model.pouch && model.pouch.db) {
return _.result(model.pouch, 'db');
}
if (model.collection && model.collection.pouch && model.collection.pouch.db) {
return _.result(model.collection.pouch, 'db');
}
if (defaults.db) {
return _.result(defaults, 'db');
}
var options = model.sync();
if (options.db) {
return _.result(options, 'db');
}
// TODO: ask sync adapter
throw new Error('A "db" property must be specified');
}
return {
attachments: function(filter) {
var atts = this.get('_attachments') || {};
if (filter) {
return _.filter(_.keys(atts), function(key) {
if (typeof filter === 'function') {
return filter(key, atts[key]);
}
return atts[key].content_type.match(filter);
});
}
return _.keys(atts);
},
attachment: function(name, done) {
// TODO: first look at the _attachments stub,
// maybe there the data is already there
var db = getPouch(this);
return db.getAttachment(this.id, name, done);
},
attach: function(blob, name, type, done) {
if (typeof name === 'function') {
done = name;
name = undefined;
type = undefined;
}
if (typeof type === 'function') {
done = type;
type = undefined;
}
name = name || blob.filename;
type = type || blob.type;
var db = getPouch(this);
var that = this;
return db.putAttachment(this.id, name, this.get('_rev'), blob, type, function(err, response) {
if (!err && response.rev) {
var atts = that.get('_attachments') || {};
atts[name] = {
content_type: type,
stub: true
};
that.set({ _id: response.id, _rev: response.rev, _attachments: atts }, { silent: true });
}
done(err, response);
});
}
};
};
}(this));