-
Notifications
You must be signed in to change notification settings - Fork 5
/
backbone-http.js
342 lines (284 loc) · 9.58 KB
/
backbone-http.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
backbone-http.js 0.7.2
Copyright (c) 2013-2015 Vidigami
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Source: https://github.com/vidigami/backbone-http
Dependencies: Backbone.js, Underscore.js, and BackboneORM.
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("backbone-orm"));
else if(typeof define === 'function' && define.amd)
define(["backbone-orm"], factory);
else if(typeof exports === 'object')
exports["BackboneHTTP"] = factory(require("backbone-orm"));
else
root["BackboneHTTP"] = factory(root["BackboneORM"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
/*
backbone-http.js 0.7.2
Copyright (c) 2013-2014 Vidigami
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Source: https://github.com/vidigami/backbone-http
Dependencies: Backbone.js and Underscore.js.
*/
var Backbone, BackboneHTTP, BackboneORM, _, key, publish, ref, ref1, value;
ref = BackboneORM = __webpack_require__(1), _ = ref._, Backbone = ref.Backbone;
module.exports = BackboneHTTP = __webpack_require__(2);
publish = {
configure: __webpack_require__(3),
sync: __webpack_require__(4),
_: _,
Backbone: Backbone
};
_.extend(BackboneHTTP, publish);
BackboneHTTP.modules = {
'backbone-orm': BackboneORM
};
ref1 = BackboneORM.modules;
for (key in ref1) {
value = ref1[key];
BackboneHTTP.modules[key] = value;
}
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
module.exports = {};
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1).configure;
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
/*
backbone-http.js 0.7.2
Copyright (c) 2013-2014 Vidigami
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Source: https://github.com/vidigami/backbone-http
Dependencies: Backbone.js and Underscore.js.
*/
var Backbone, BackboneORM, CAPABILITIES, HTTPCursor, HTTPSync, JSONUtils, Schema, URL, Utils, _, backboneSync, ref;
ref = BackboneORM = __webpack_require__(1), _ = ref._, Backbone = ref.Backbone, Utils = ref.Utils, JSONUtils = ref.JSONUtils, Schema = ref.Schema;
URL = BackboneORM.modules.url;
backboneSync = Backbone.sync;
HTTPCursor = __webpack_require__(5);
CAPABILITIES = {
embed: 'maybe',
json: 'maybe',
unique: 'maybe',
manual_ids: false,
dynamic: false,
self_reference: 'maybe'
};
HTTPSync = (function() {
function HTTPSync(model_type1, options) {
this.model_type = model_type1;
if (options == null) {
options = {};
}
this.model_type.model_name = Utils.findOrGenerateModelName(this.model_type);
if (!(this.url = _.result(new this.model_type, 'url'))) {
throw new Error("Missing url for model: " + this.model_type);
}
this.schema = new Schema(this.model_type, {
id: {
type: '_raw'
}
});
this.beforeSend = options.beforeSend;
this.event_emitter = _.extend({}, Backbone.Events);
}
HTTPSync.prototype.initialize = function(model) {
if (this.is_initialized) {
return;
}
this.is_initialized = true;
return this.schema.initialize();
};
HTTPSync.prototype.resetSchema = function(options, callback) {
return this.http('delete', null, {}, callback);
};
HTTPSync.prototype.cursor = function(query) {
if (query == null) {
query = {};
}
return new HTTPCursor(query, {
model_type: this.model_type,
sync: this
});
};
HTTPSync.prototype.destroy = function(query, callback) {
var ref1;
if (arguments.length === 1) {
ref1 = [{}, query], query = ref1[0], callback = ref1[1];
}
return this.http('delete', null, {
query: query
}, callback);
};
HTTPSync.prototype.http = function(method, model, options, callback) {
var url, url_parts;
url = model ? _.result(model, 'url') : this.url;
if (options.query && _.size(options.query)) {
url_parts = URL.parse(url, true);
_.extend(url_parts.query, JSONUtils.querify(options.query));
url = URL.format(url_parts);
}
return backboneSync(method, model || this.event_emitter, _.extend({
url: url,
beforeSend: this.beforeSend
}, options, {
success: function(res) {
return callback(null, JSONUtils.parse(res));
},
error: function(res) {
return callback(_.extend(new Error("Ajax failed with status " + res.status + " for " + method), {
status: res.status,
json: res.responseJSON,
text: res.responseText
}));
}
}));
};
return HTTPSync;
})();
module.exports = function(type, sync_options) {
var model_type, sync, sync_fn;
if (Utils.isCollection(new type())) {
model_type = Utils.configureCollectionModelType(type, module.exports, sync_options);
return type.prototype.sync = model_type.prototype.sync;
}
sync = new HTTPSync(type, sync_options);
type.prototype.sync = sync_fn = function(method, model, options) {
var url;
if (options == null) {
options = {};
}
sync.initialize();
if (method === 'createSync') {
return module.exports.apply(null, Array.prototype.slice.call(arguments, 1));
}
if (method === 'sync') {
return sync;
}
if (method === 'schema') {
return sync.schema;
}
if (method === 'isRemote') {
return true;
}
if (_.contains(['create', 'update', 'patch', 'delete', 'read'], method)) {
if (!(url = options.url || _.result(model, 'url'))) {
throw new Error('Missing url for model');
}
if (method === 'read') {
options = _.extend({
$one: !model.models
}, options);
}
sync.http(method, model, _.omit(options, 'error', 'success'), (function(_this) {
return function(err, res) {
if (err) {
return options.error(err);
} else {
return options.success(res);
}
};
})(this));
return;
}
if (sync[method]) {
return sync[method].apply(sync, Array.prototype.slice.call(arguments, 1));
} else {
return void 0;
}
};
Utils.configureModelType(type);
return BackboneORM.model_cache.configureSync(type, sync_fn);
};
module.exports.capabilities = function(url) {
return CAPABILITIES;
};
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
/*
backbone-http.js 0.7.2
Copyright (c) 2013-2014 Vidigami
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Source: https://github.com/vidigami/backbone-http
Dependencies: Backbone.js and Underscore.js.
*/
var Cursor, HTTPCursor, _, ref,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
ref = __webpack_require__(1), _ = ref._, Cursor = ref.Cursor;
module.exports = HTTPCursor = (function(superClass) {
extend(HTTPCursor, superClass);
function HTTPCursor() {
return HTTPCursor.__super__.constructor.apply(this, arguments);
}
HTTPCursor.prototype.toJSON = function(callback) {
var query;
if (this.hasCursorQuery('$zero')) {
return callback(null, this.hasCursorQuery('$one') ? null : []);
}
return this.sync.http('read', null, {
query: query = _.extend({}, this._find, this._cursor)
}, (function(_this) {
return function(err, res) {
if (query.$one && err && (err.status === 404)) {
return callback(null, null);
}
if (err) {
return callback(err);
}
return callback(null, _this.hasCursorQuery('$count') || _this.hasCursorQuery('$exists') ? res.result : res);
};
})(this));
};
return HTTPCursor;
})(Cursor);
/***/ }
/******/ ])
});
;