@converse/skeletor 0.0.3 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -7
- package/dist/skeletor.js +508 -1048
- package/dist/skeletor.js.map +1 -1
- package/dist/skeletor.min.js +4 -93
- package/dist/skeletor.min.js.map +1 -1
- package/package.json +6 -7
- package/src/collection.js +1 -1
- package/src/drivers/indexeddb-getitems.js~ +1116 -0
- package/src/drivers/indexeddb.js~ +1115 -0
- package/src/storage.js +11 -8
- package/src/storage.js~ +13 -9
- package/CHANGES.md +0 -26
- package/dist/skeletor.js~ +0 -2291
- package/src/.events.js.swp +0 -0
- package/src/.helpers.js.swp +0 -0
- package/src/.history.js.swp +0 -0
- package/src/.model.js.swp +0 -0
- package/src/.overview.js.swp +0 -0
- package/src/.router.js.swp +0 -0
- package/src/.view.js.swp +0 -0
- package/src/collection.js~ +0 -737
- package/src/drivers/.sessionStorage.js.swp +0 -0
- package/src/drivers/sessionStorage.js~ +0 -316
- package/src/element-overview.js~ +0 -4
- package/src/element.js~ +0 -190
- package/src/events.js~ +0 -334
- package/src/helpers.js~ +0 -151
- package/src/history.js~ +0 -301
- package/src/htmlview.js~ +0 -14
- package/src/model.js~ +0 -439
- package/src/orderedlist.js~ +0 -88
- package/src/overview.js~ +0 -111
- package/src/router.js~ +0 -119
- package/src/vdomview.js~ +0 -15
- package/src/view.js~ +0 -283
package/dist/skeletor.js~
DELETED
|
@@ -1,2291 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('lodash-es/create'), require('lodash-es/extend'), require('lodash-es/has'), require('lodash-es/result'), require('lodash-es/uniqueId'), require('lodash-es/isEmpty'), require('lodash-es/clone'), require('lodash-es/defaults'), require('lodash-es/defer'), require('lodash-es/escape'), require('lodash-es/invert'), require('lodash-es/isElement'), require('lodash-es/isEqual'), require('lodash-es/isFunction'), require('lodash-es/iteratee'), require('lodash-es/omit'), require('lodash-es/pick'), require('lodash-es/countBy'), require('lodash-es/difference'), require('lodash-es/every'), require('lodash-es/findIndex'), require('lodash-es/findLastIndex'), require('lodash-es/groupBy'), require('lodash-es/indexOf'), require('lodash-es/isString'), require('lodash-es/keyBy'), require('lodash-es/lastIndexOf'), require('lodash-es/some'), require('lodash-es/sortBy'), require('lodash-es/isRegExp'), require('lit-html')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['lodash-es/create', 'lodash-es/extend', 'lodash-es/has', 'lodash-es/result', 'lodash-es/uniqueId', 'lodash-es/isEmpty', 'lodash-es/clone', 'lodash-es/defaults', 'lodash-es/defer', 'lodash-es/escape', 'lodash-es/invert', 'lodash-es/isElement', 'lodash-es/isEqual', 'lodash-es/isFunction', 'lodash-es/iteratee', 'lodash-es/omit', 'lodash-es/pick', 'lodash-es/countBy', 'lodash-es/difference', 'lodash-es/every', 'lodash-es/findIndex', 'lodash-es/findLastIndex', 'lodash-es/groupBy', 'lodash-es/indexOf', 'lodash-es/isString', 'lodash-es/keyBy', 'lodash-es/lastIndexOf', 'lodash-es/some', 'lodash-es/sortBy', 'lodash-es/isRegExp', 'lit-html'], factory) :
|
|
4
|
-
(global = global || self, global.skeletor = factory(global.create, global.extend, global.has, global.result, global.uniqueId, global.isEmpty, global.clone, global.defaults, global.defer, global.escape, global.invert, global.isElement, global.isEqual, global.isFunction, global.iteratee, global.omit, global.pick, global.countBy, global.difference, global.every, global.findIndex, global.findLastIndex, global.groupBy, global.indexOf$1, global.isString, global.keyBy, global.lastIndexOf, global.some, global.sortBy, global.isRegExp, global.render));
|
|
5
|
-
}(this, (function (create, extend, has, result, uniqueId, isEmpty, clone, defaults, defer, escape, invert, isElement, isEqual, isFunction, iteratee, omit, pick, countBy, difference, every, findIndex, findLastIndex, groupBy, indexOf$1, isString, keyBy, lastIndexOf, some, sortBy, isRegExp, render) { 'use strict';
|
|
6
|
-
|
|
7
|
-
debugger;
|
|
8
|
-
create = create && Object.prototype.hasOwnProperty.call(create, 'default') ? create['default'] : create;
|
|
9
|
-
extend = extend && Object.prototype.hasOwnProperty.call(extend, 'default') ? extend['default'] : extend;
|
|
10
|
-
has = has && Object.prototype.hasOwnProperty.call(has, 'default') ? has['default'] : has;
|
|
11
|
-
result = result && Object.prototype.hasOwnProperty.call(result, 'default') ? result['default'] : result;
|
|
12
|
-
uniqueId = uniqueId && Object.prototype.hasOwnProperty.call(uniqueId, 'default') ? uniqueId['default'] : uniqueId;
|
|
13
|
-
isEmpty = isEmpty && Object.prototype.hasOwnProperty.call(isEmpty, 'default') ? isEmpty['default'] : isEmpty;
|
|
14
|
-
clone = clone && Object.prototype.hasOwnProperty.call(clone, 'default') ? clone['default'] : clone;
|
|
15
|
-
defaults = defaults && Object.prototype.hasOwnProperty.call(defaults, 'default') ? defaults['default'] : defaults;
|
|
16
|
-
defer = defer && Object.prototype.hasOwnProperty.call(defer, 'default') ? defer['default'] : defer;
|
|
17
|
-
escape = escape && Object.prototype.hasOwnProperty.call(escape, 'default') ? escape['default'] : escape;
|
|
18
|
-
invert = invert && Object.prototype.hasOwnProperty.call(invert, 'default') ? invert['default'] : invert;
|
|
19
|
-
isElement = isElement && Object.prototype.hasOwnProperty.call(isElement, 'default') ? isElement['default'] : isElement;
|
|
20
|
-
isEqual = isEqual && Object.prototype.hasOwnProperty.call(isEqual, 'default') ? isEqual['default'] : isEqual;
|
|
21
|
-
isFunction = isFunction && Object.prototype.hasOwnProperty.call(isFunction, 'default') ? isFunction['default'] : isFunction;
|
|
22
|
-
iteratee = iteratee && Object.prototype.hasOwnProperty.call(iteratee, 'default') ? iteratee['default'] : iteratee;
|
|
23
|
-
omit = omit && Object.prototype.hasOwnProperty.call(omit, 'default') ? omit['default'] : omit;
|
|
24
|
-
pick = pick && Object.prototype.hasOwnProperty.call(pick, 'default') ? pick['default'] : pick;
|
|
25
|
-
countBy = countBy && Object.prototype.hasOwnProperty.call(countBy, 'default') ? countBy['default'] : countBy;
|
|
26
|
-
difference = difference && Object.prototype.hasOwnProperty.call(difference, 'default') ? difference['default'] : difference;
|
|
27
|
-
every = every && Object.prototype.hasOwnProperty.call(every, 'default') ? every['default'] : every;
|
|
28
|
-
findIndex = findIndex && Object.prototype.hasOwnProperty.call(findIndex, 'default') ? findIndex['default'] : findIndex;
|
|
29
|
-
findLastIndex = findLastIndex && Object.prototype.hasOwnProperty.call(findLastIndex, 'default') ? findLastIndex['default'] : findLastIndex;
|
|
30
|
-
groupBy = groupBy && Object.prototype.hasOwnProperty.call(groupBy, 'default') ? groupBy['default'] : groupBy;
|
|
31
|
-
indexOf$1 = indexOf$1 && Object.prototype.hasOwnProperty.call(indexOf$1, 'default') ? indexOf$1['default'] : indexOf$1;
|
|
32
|
-
isString = isString && Object.prototype.hasOwnProperty.call(isString, 'default') ? isString['default'] : isString;
|
|
33
|
-
keyBy = keyBy && Object.prototype.hasOwnProperty.call(keyBy, 'default') ? keyBy['default'] : keyBy;
|
|
34
|
-
lastIndexOf = lastIndexOf && Object.prototype.hasOwnProperty.call(lastIndexOf, 'default') ? lastIndexOf['default'] : lastIndexOf;
|
|
35
|
-
some = some && Object.prototype.hasOwnProperty.call(some, 'default') ? some['default'] : some;
|
|
36
|
-
sortBy = sortBy && Object.prototype.hasOwnProperty.call(sortBy, 'default') ? sortBy['default'] : sortBy;
|
|
37
|
-
isRegExp = isRegExp && Object.prototype.hasOwnProperty.call(isRegExp, 'default') ? isRegExp['default'] : isRegExp;
|
|
38
|
-
render = render && Object.prototype.hasOwnProperty.call(render, 'default') ? render['default'] : render;
|
|
39
|
-
|
|
40
|
-
// (c) 2010-2019 Jeremy Ashkenas and DocumentCloud
|
|
41
|
-
|
|
42
|
-
// Helpers
|
|
43
|
-
// -------
|
|
44
|
-
|
|
45
|
-
// Helper function to correctly set up the prototype chain for subclasses.
|
|
46
|
-
// Similar to `goog.inherits`, but uses a hash of prototype properties and
|
|
47
|
-
// class properties to be extended.
|
|
48
|
-
//
|
|
49
|
-
function inherits(protoProps, staticProps) {
|
|
50
|
-
const parent = this;
|
|
51
|
-
let child;
|
|
52
|
-
|
|
53
|
-
// The constructor function for the new subclass is either defined by you
|
|
54
|
-
// (the "constructor" property in your `extend` definition), or defaulted
|
|
55
|
-
// by us to simply call the parent constructor.
|
|
56
|
-
if (protoProps && has(protoProps, 'constructor')) {
|
|
57
|
-
child = protoProps.constructor;
|
|
58
|
-
} else {
|
|
59
|
-
child = function(){ return parent.apply(this, arguments); };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Add static properties to the constructor function, if supplied.
|
|
63
|
-
extend(child, parent, staticProps);
|
|
64
|
-
|
|
65
|
-
// Set the prototype chain to inherit from `parent`, without calling
|
|
66
|
-
// `parent`'s constructor function and add the prototype properties.
|
|
67
|
-
child.prototype = create(parent.prototype, protoProps);
|
|
68
|
-
child.prototype.constructor = child;
|
|
69
|
-
|
|
70
|
-
// Set a convenience property in case the parent's prototype is needed
|
|
71
|
-
// later.
|
|
72
|
-
child.__super__ = parent.prototype;
|
|
73
|
-
|
|
74
|
-
return child;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
function getResolveablePromise () {
|
|
79
|
-
const wrapper = {
|
|
80
|
-
isResolved: false,
|
|
81
|
-
isPending: true,
|
|
82
|
-
isRejected: false
|
|
83
|
-
};
|
|
84
|
-
const promise = new Promise((resolve, reject) => {
|
|
85
|
-
wrapper.resolve = resolve;
|
|
86
|
-
wrapper.reject = reject;
|
|
87
|
-
});
|
|
88
|
-
Object.assign(promise, wrapper);
|
|
89
|
-
promise.then(
|
|
90
|
-
function (v) {
|
|
91
|
-
promise.isResolved = true;
|
|
92
|
-
promise.isPending = false;
|
|
93
|
-
promise.isRejected = false;
|
|
94
|
-
return v;
|
|
95
|
-
},
|
|
96
|
-
function (e) {
|
|
97
|
-
promise.isResolved = false;
|
|
98
|
-
promise.isPending = false;
|
|
99
|
-
promise.isRejected = true;
|
|
100
|
-
throw (e);
|
|
101
|
-
}
|
|
102
|
-
);
|
|
103
|
-
return promise;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// Throw an error when a URL is needed, and none is supplied.
|
|
108
|
-
function urlError() {
|
|
109
|
-
throw new Error('A "url" property or function must be specified');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Wrap an optional error callback with a fallback error event.
|
|
113
|
-
function wrapError(model, options) {
|
|
114
|
-
const error = options.error;
|
|
115
|
-
options.error = function(resp) {
|
|
116
|
-
if (error) error.call(options.context, model, resp, options);
|
|
117
|
-
model.trigger('error', model, resp, options);
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Map from CRUD to HTTP for our default `sync` implementation.
|
|
122
|
-
const methodMap = {
|
|
123
|
-
create: 'POST',
|
|
124
|
-
update: 'PUT',
|
|
125
|
-
patch: 'PATCH',
|
|
126
|
-
delete: 'DELETE',
|
|
127
|
-
read: 'GET'
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
function getSyncMethod(model) {
|
|
131
|
-
const store = result(model, 'browserStorage') || result(model.collection, 'browserStorage');
|
|
132
|
-
return store ? store.sync() : sync;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// sync
|
|
136
|
-
// ----
|
|
137
|
-
|
|
138
|
-
// Override this function to change the manner in which Backbone persists
|
|
139
|
-
// models to the server. You will be passed the type of request, and the
|
|
140
|
-
// model in question. By default, makes a RESTful Ajax request
|
|
141
|
-
// to the model's `url()`. Some possible customizations could be:
|
|
142
|
-
//
|
|
143
|
-
// * Use `setTimeout` to batch rapid-fire updates into a single request.
|
|
144
|
-
// * Send up the models as XML instead of JSON.
|
|
145
|
-
// * Persist models via WebSockets instead of Ajax.
|
|
146
|
-
//
|
|
147
|
-
function sync(method, model, options={}) {
|
|
148
|
-
const type = methodMap[method];
|
|
149
|
-
|
|
150
|
-
// Default JSON-request options.
|
|
151
|
-
const params = {type: type, dataType: 'json'};
|
|
152
|
-
|
|
153
|
-
// Ensure that we have a URL.
|
|
154
|
-
if (!options.url) {
|
|
155
|
-
params.url = result(model, 'url') || urlError();
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Ensure that we have the appropriate request data.
|
|
159
|
-
if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
|
|
160
|
-
params.contentType = 'application/json';
|
|
161
|
-
params.data = JSON.stringify(options.attrs || model.toJSON(options));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Don't process data on a non-GET request.
|
|
165
|
-
if (params.type !== 'GET') {
|
|
166
|
-
params.processData = false;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Pass along `textStatus` and `errorThrown` from jQuery.
|
|
170
|
-
const error = options.error;
|
|
171
|
-
options.error = function(xhr, textStatus, errorThrown) {
|
|
172
|
-
options.textStatus = textStatus;
|
|
173
|
-
options.errorThrown = errorThrown;
|
|
174
|
-
if (error) error.call(options.context, xhr, textStatus, errorThrown);
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
// Make the request, allowing the user to override any Ajax options.
|
|
178
|
-
const xhr = options.xhr = ajax(extend(params, options));
|
|
179
|
-
model.trigger('request', model, xhr, options);
|
|
180
|
-
return xhr;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function ajax() {
|
|
184
|
-
return fetch.apply(this, arguments);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Backbone.js 1.4.0
|
|
188
|
-
|
|
189
|
-
const Events = {};
|
|
190
|
-
|
|
191
|
-
// Regular expression used to split event strings.
|
|
192
|
-
const eventSplitter = /\s+/;
|
|
193
|
-
|
|
194
|
-
// A private global variable to share between listeners and listenees.
|
|
195
|
-
let _listening;
|
|
196
|
-
|
|
197
|
-
// Iterates over the standard `event, callback` (as well as the fancy multiple
|
|
198
|
-
// space-separated events `"change blur", callback` and jQuery-style event
|
|
199
|
-
// maps `{event: callback}`).
|
|
200
|
-
const eventsApi = function(iteratee, events, name, callback, opts) {
|
|
201
|
-
let i = 0, names;
|
|
202
|
-
if (name && typeof name === 'object') {
|
|
203
|
-
// Handle event maps.
|
|
204
|
-
if (callback !== undefined && 'context' in opts && opts.context === undefined) opts.context = callback;
|
|
205
|
-
for (names = Object.keys(name); i < names.length ; i++) {
|
|
206
|
-
events = eventsApi(iteratee, events, names[i], name[names[i]], opts);
|
|
207
|
-
}
|
|
208
|
-
} else if (name && eventSplitter.test(name)) {
|
|
209
|
-
// Handle space-separated event names by delegating them individually.
|
|
210
|
-
for (names = name.split(eventSplitter); i < names.length; i++) {
|
|
211
|
-
events = iteratee(events, names[i], callback, opts);
|
|
212
|
-
}
|
|
213
|
-
} else {
|
|
214
|
-
// Finally, standard events.
|
|
215
|
-
events = iteratee(events, name, callback, opts);
|
|
216
|
-
}
|
|
217
|
-
return events;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
// Bind an event to a `callback` function. Passing `"all"` will bind
|
|
221
|
-
// the callback to all events fired.
|
|
222
|
-
Events.on = function(name, callback, context) {
|
|
223
|
-
this._events = eventsApi(onApi, this._events || {}, name, callback, {
|
|
224
|
-
context: context,
|
|
225
|
-
ctx: this,
|
|
226
|
-
listening: _listening
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
if (_listening) {
|
|
230
|
-
const listeners = this._listeners || (this._listeners = {});
|
|
231
|
-
listeners[_listening.id] = _listening;
|
|
232
|
-
// Allow the listening to use a counter, instead of tracking
|
|
233
|
-
// callbacks for library interop
|
|
234
|
-
_listening.interop = false;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return this;
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
// Inversion-of-control versions of `on`. Tell *this* object to listen to
|
|
241
|
-
// an event in another object... keeping track of what it's listening to
|
|
242
|
-
// for easier unbinding later.
|
|
243
|
-
Events.listenTo = function(obj, name, callback) {
|
|
244
|
-
if (!obj) return this;
|
|
245
|
-
const id = obj._listenId || (obj._listenId = uniqueId('l'));
|
|
246
|
-
const listeningTo = this._listeningTo || (this._listeningTo = {});
|
|
247
|
-
let listening = _listening = listeningTo[id];
|
|
248
|
-
|
|
249
|
-
// This object is not listening to any other events on `obj` yet.
|
|
250
|
-
// Setup the necessary references to track the listening callbacks.
|
|
251
|
-
if (!listening) {
|
|
252
|
-
this._listenId || (this._listenId = uniqueId('l'));
|
|
253
|
-
listening = _listening = listeningTo[id] = new Listening(this, obj);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Bind callbacks on obj.
|
|
257
|
-
const error = tryCatchOn(obj, name, callback, this);
|
|
258
|
-
_listening = undefined;
|
|
259
|
-
|
|
260
|
-
if (error) throw error;
|
|
261
|
-
// If the target obj is not Backbone.Events, track events manually.
|
|
262
|
-
if (listening.interop) listening.on(name, callback);
|
|
263
|
-
|
|
264
|
-
return this;
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
// The reducing API that adds a callback to the `events` object.
|
|
268
|
-
const onApi = function(events, name, callback, options) {
|
|
269
|
-
if (callback) {
|
|
270
|
-
const handlers = events[name] || (events[name] = []);
|
|
271
|
-
const context = options.context, ctx = options.ctx, listening = options.listening;
|
|
272
|
-
if (listening) listening.count++;
|
|
273
|
-
|
|
274
|
-
handlers.push({callback: callback, context: context, ctx: context || ctx, listening: listening});
|
|
275
|
-
}
|
|
276
|
-
return events;
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
// An try-catch guarded #on function, to prevent poisoning the global
|
|
280
|
-
// `_listening` variable.
|
|
281
|
-
const tryCatchOn = function(obj, name, callback, context) {
|
|
282
|
-
try {
|
|
283
|
-
obj.on(name, callback, context);
|
|
284
|
-
} catch (e) {
|
|
285
|
-
return e;
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
// Remove one or many callbacks. If `context` is null, removes all
|
|
290
|
-
// callbacks with that function. If `callback` is null, removes all
|
|
291
|
-
// callbacks for the event. If `name` is null, removes all bound
|
|
292
|
-
// callbacks for all events.
|
|
293
|
-
Events.off = function(name, callback, context) {
|
|
294
|
-
if (!this._events) return this;
|
|
295
|
-
this._events = eventsApi(offApi, this._events, name, callback, {
|
|
296
|
-
context: context,
|
|
297
|
-
listeners: this._listeners
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
return this;
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
// Tell this object to stop listening to either specific events ... or
|
|
304
|
-
// to every object it's currently listening to.
|
|
305
|
-
Events.stopListening = function(obj, name, callback) {
|
|
306
|
-
const listeningTo = this._listeningTo;
|
|
307
|
-
if (!listeningTo) return this;
|
|
308
|
-
|
|
309
|
-
const ids = obj ? [obj._listenId] : Object.keys(listeningTo);
|
|
310
|
-
for (let i = 0; i < ids.length; i++) {
|
|
311
|
-
const listening = listeningTo[ids[i]];
|
|
312
|
-
|
|
313
|
-
// If listening doesn't exist, this object is not currently
|
|
314
|
-
// listening to obj. Break out early.
|
|
315
|
-
if (!listening) break;
|
|
316
|
-
|
|
317
|
-
listening.obj.off(name, callback, this);
|
|
318
|
-
if (listening.interop) listening.off(name, callback);
|
|
319
|
-
}
|
|
320
|
-
if (isEmpty(listeningTo)) this._listeningTo = undefined;
|
|
321
|
-
|
|
322
|
-
return this;
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
// The reducing API that removes a callback from the `events` object.
|
|
326
|
-
const offApi = function(events, name, callback, options) {
|
|
327
|
-
if (!events) return;
|
|
328
|
-
|
|
329
|
-
const context = options.context, listeners = options.listeners;
|
|
330
|
-
let i = 0, names;
|
|
331
|
-
|
|
332
|
-
// Delete all event listeners and "drop" events.
|
|
333
|
-
if (!name && !context && !callback) {
|
|
334
|
-
for (names = Object.keys(listeners); i < names.length; i++) {
|
|
335
|
-
listeners[names[i]].cleanup();
|
|
336
|
-
}
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
names = name ? [name] : Object.keys(events);
|
|
341
|
-
for (; i < names.length; i++) {
|
|
342
|
-
name = names[i];
|
|
343
|
-
const handlers = events[name];
|
|
344
|
-
|
|
345
|
-
// Bail out if there are no events stored.
|
|
346
|
-
if (!handlers) {
|
|
347
|
-
break;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// Find any remaining events.
|
|
351
|
-
const remaining = [];
|
|
352
|
-
for (let j = 0; j < handlers.length; j++) {
|
|
353
|
-
const handler = handlers[j];
|
|
354
|
-
if (
|
|
355
|
-
callback && callback !== handler.callback &&
|
|
356
|
-
callback !== handler.callback._callback ||
|
|
357
|
-
context && context !== handler.context
|
|
358
|
-
) {
|
|
359
|
-
remaining.push(handler);
|
|
360
|
-
} else {
|
|
361
|
-
const listening = handler.listening;
|
|
362
|
-
if (listening) listening.off(name, callback);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
// Replace events if there are any remaining. Otherwise, clean up.
|
|
367
|
-
if (remaining.length) {
|
|
368
|
-
events[name] = remaining;
|
|
369
|
-
} else {
|
|
370
|
-
delete events[name];
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return events;
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
// Bind an event to only be triggered a single time. After the first time
|
|
378
|
-
// the callback is invoked, its listener will be removed. If multiple events
|
|
379
|
-
// are passed in using the space-separated syntax, the handler will fire
|
|
380
|
-
// once for each event, not once for a combination of all events.
|
|
381
|
-
Events.once = function(name, callback, context) {
|
|
382
|
-
// Map the event into a `{event: once}` object.
|
|
383
|
-
const events = eventsApi(onceMap, {}, name, callback, this.off.bind(this));
|
|
384
|
-
if (typeof name === 'string' && (context === null || context === undefined)) callback = undefined;
|
|
385
|
-
return this.on(events, callback, context);
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
// Inversion-of-control versions of `once`.
|
|
389
|
-
Events.listenToOnce = function(obj, name, callback) {
|
|
390
|
-
// Map the event into a `{event: once}` object.
|
|
391
|
-
const events = eventsApi(onceMap, {}, name, callback, this.stopListening.bind(this, obj));
|
|
392
|
-
return this.listenTo(obj, events);
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
// Reduces the event callbacks into a map of `{event: onceWrapper}`.
|
|
396
|
-
// `offer` unbinds the `onceWrapper` after it has been called.
|
|
397
|
-
const onceMap = function(map, name, callback, offer) {
|
|
398
|
-
if (callback) {
|
|
399
|
-
const once = map[name] = once(function() {
|
|
400
|
-
offer(name, once);
|
|
401
|
-
callback.apply(this, arguments);
|
|
402
|
-
});
|
|
403
|
-
once._callback = callback;
|
|
404
|
-
}
|
|
405
|
-
return map;
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
// Trigger one or many events, firing all bound callbacks. Callbacks are
|
|
409
|
-
// passed the same arguments as `trigger` is, apart from the event name
|
|
410
|
-
// (unless you're listening on `"all"`, which will cause your callback to
|
|
411
|
-
// receive the true name of the event as the first argument).
|
|
412
|
-
Events.trigger = function(name) {
|
|
413
|
-
if (!this._events) return this;
|
|
414
|
-
|
|
415
|
-
const length = Math.max(0, arguments.length - 1);
|
|
416
|
-
const args = Array(length);
|
|
417
|
-
for (let i = 0; i < length; i++) args[i] = arguments[i + 1];
|
|
418
|
-
|
|
419
|
-
eventsApi(triggerApi, this._events, name, undefined, args);
|
|
420
|
-
return this;
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
// Handles triggering the appropriate event callbacks.
|
|
424
|
-
const triggerApi = function(objEvents, name, callback, args) {
|
|
425
|
-
if (objEvents) {
|
|
426
|
-
const events = objEvents[name];
|
|
427
|
-
let allEvents = objEvents.all;
|
|
428
|
-
if (events && allEvents) allEvents = allEvents.slice();
|
|
429
|
-
if (events) triggerEvents(events, args);
|
|
430
|
-
if (allEvents) triggerEvents(allEvents, [name].concat(args));
|
|
431
|
-
}
|
|
432
|
-
return objEvents;
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
// A difficult-to-believe, but optimized internal dispatch function for
|
|
436
|
-
// triggering events. Tries to keep the usual cases speedy (most internal
|
|
437
|
-
// Backbone events have 3 arguments).
|
|
438
|
-
const triggerEvents = function(events, args) {
|
|
439
|
-
let ev, i = -1;
|
|
440
|
-
const l = events.length,
|
|
441
|
-
a1 = args[0],
|
|
442
|
-
a2 = args[1],
|
|
443
|
-
a3 = args[2];
|
|
444
|
-
switch (args.length) {
|
|
445
|
-
case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return;
|
|
446
|
-
case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return;
|
|
447
|
-
case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return;
|
|
448
|
-
case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return;
|
|
449
|
-
default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); return;
|
|
450
|
-
}
|
|
451
|
-
};
|
|
452
|
-
|
|
453
|
-
// A listening class that tracks and cleans up memory bindings
|
|
454
|
-
// when all callbacks have been offed.
|
|
455
|
-
const Listening = function(listener, obj) {
|
|
456
|
-
this.id = listener._listenId;
|
|
457
|
-
this.listener = listener;
|
|
458
|
-
this.obj = obj;
|
|
459
|
-
this.interop = true;
|
|
460
|
-
this.count = 0;
|
|
461
|
-
this._events = undefined;
|
|
462
|
-
};
|
|
463
|
-
|
|
464
|
-
Listening.prototype.on = Events.on;
|
|
465
|
-
|
|
466
|
-
// Offs a callback (or several).
|
|
467
|
-
// Uses an optimized counter if the listenee uses Backbone.Events.
|
|
468
|
-
// Otherwise, falls back to manual tracking to support events
|
|
469
|
-
// library interop.
|
|
470
|
-
Listening.prototype.off = function(name, callback) {
|
|
471
|
-
let cleanup;
|
|
472
|
-
if (this.interop) {
|
|
473
|
-
this._events = eventsApi(offApi, this._events, name, callback, {
|
|
474
|
-
context: undefined,
|
|
475
|
-
listeners: undefined
|
|
476
|
-
});
|
|
477
|
-
cleanup = !this._events;
|
|
478
|
-
} else {
|
|
479
|
-
this.count--;
|
|
480
|
-
cleanup = this.count === 0;
|
|
481
|
-
}
|
|
482
|
-
if (cleanup) this.cleanup();
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
// Cleans up memory bindings between the listener and the listenee.
|
|
486
|
-
Listening.prototype.cleanup = function() {
|
|
487
|
-
delete this.listener._listeningTo[this.obj._listenId];
|
|
488
|
-
if (!this.interop) delete this.obj._listeners[this.id];
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
// Aliases for backwards compatibility.
|
|
492
|
-
Events.bind = Events.on;
|
|
493
|
-
Events.unbind = Events.off;
|
|
494
|
-
|
|
495
|
-
// Backbone.js 1.4.0
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
const Model = function(attributes, options) {
|
|
499
|
-
let attrs = attributes || {};
|
|
500
|
-
options || (options = {});
|
|
501
|
-
this.preinitialize.apply(this, arguments);
|
|
502
|
-
this.cid = uniqueId(this.cidPrefix);
|
|
503
|
-
this.attributes = {};
|
|
504
|
-
if (options.collection) this.collection = options.collection;
|
|
505
|
-
if (options.parse) attrs = this.parse(attrs, options) || {};
|
|
506
|
-
const default_attrs = result(this, 'defaults');
|
|
507
|
-
attrs = defaults(extend({}, default_attrs, attrs), default_attrs);
|
|
508
|
-
this.set(attrs, options);
|
|
509
|
-
this.changed = {};
|
|
510
|
-
this.initialize.apply(this, arguments);
|
|
511
|
-
};
|
|
512
|
-
|
|
513
|
-
Model.extend = inherits;
|
|
514
|
-
|
|
515
|
-
// Attach all inheritable methods to the Model prototype.
|
|
516
|
-
Object.assign(Model.prototype, Events, {
|
|
517
|
-
|
|
518
|
-
// A hash of attributes whose current and previous value differ.
|
|
519
|
-
changed: null,
|
|
520
|
-
|
|
521
|
-
// The value returned during the last failed validation.
|
|
522
|
-
validationError: null,
|
|
523
|
-
|
|
524
|
-
// The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
|
525
|
-
// CouchDB users may want to set this to `"_id"`.
|
|
526
|
-
idAttribute: 'id',
|
|
527
|
-
|
|
528
|
-
// The prefix is used to create the client id which is used to identify models locally.
|
|
529
|
-
// You may want to override this if you're experiencing name clashes with model ids.
|
|
530
|
-
cidPrefix: 'c',
|
|
531
|
-
|
|
532
|
-
// preinitialize is an empty function by default. You can override it with a function
|
|
533
|
-
// or object. preinitialize will run before any instantiation logic is run in the Model.
|
|
534
|
-
preinitialize: function(){},
|
|
535
|
-
|
|
536
|
-
// Initialize is an empty function by default. Override it with your own
|
|
537
|
-
// initialization logic.
|
|
538
|
-
initialize: function(){},
|
|
539
|
-
|
|
540
|
-
// Return a copy of the model's `attributes` object.
|
|
541
|
-
toJSON: function(options) {
|
|
542
|
-
return clone(this.attributes);
|
|
543
|
-
},
|
|
544
|
-
|
|
545
|
-
// Proxy `Backbone.sync` by default -- but override this if you need
|
|
546
|
-
// custom syncing semantics for *this* particular model.
|
|
547
|
-
sync: function(method, model, options) {
|
|
548
|
-
return getSyncMethod(this)(method, model, options);
|
|
549
|
-
},
|
|
550
|
-
|
|
551
|
-
// Get the value of an attribute.
|
|
552
|
-
get: function(attr) {
|
|
553
|
-
return this.attributes[attr];
|
|
554
|
-
},
|
|
555
|
-
|
|
556
|
-
keys: function() {
|
|
557
|
-
return Object.keys(this.attributes);
|
|
558
|
-
},
|
|
559
|
-
|
|
560
|
-
values: function() {
|
|
561
|
-
return Object.values(this.attributes);
|
|
562
|
-
},
|
|
563
|
-
|
|
564
|
-
pairs: function() {
|
|
565
|
-
return this.entries();
|
|
566
|
-
},
|
|
567
|
-
|
|
568
|
-
entries: function() {
|
|
569
|
-
return Object.entries(this.attributes);
|
|
570
|
-
},
|
|
571
|
-
|
|
572
|
-
invert: function() {
|
|
573
|
-
return invert(this.attributes);
|
|
574
|
-
},
|
|
575
|
-
|
|
576
|
-
pick: function(...args) {
|
|
577
|
-
if (args.length === 1 && Array.isArray(args[0])) {
|
|
578
|
-
args = args[0];
|
|
579
|
-
}
|
|
580
|
-
return pick(this.attributes, args);
|
|
581
|
-
},
|
|
582
|
-
|
|
583
|
-
omit: function(...args) {
|
|
584
|
-
if (args.length === 1 && Array.isArray(args[0])) {
|
|
585
|
-
args = args[0];
|
|
586
|
-
}
|
|
587
|
-
return omit(this.attributes, args);
|
|
588
|
-
},
|
|
589
|
-
|
|
590
|
-
isEmpty: function() {
|
|
591
|
-
return isEmpty(this.attributes);
|
|
592
|
-
},
|
|
593
|
-
|
|
594
|
-
// Get the HTML-escaped value of an attribute.
|
|
595
|
-
escape: function(attr) {
|
|
596
|
-
return escape(this.get(attr));
|
|
597
|
-
},
|
|
598
|
-
|
|
599
|
-
// Returns `true` if the attribute contains a value that is not null
|
|
600
|
-
// or undefined.
|
|
601
|
-
has: function(attr) {
|
|
602
|
-
return this.get(attr) != null;
|
|
603
|
-
},
|
|
604
|
-
|
|
605
|
-
// Special-cased proxy to lodash's `matches` method.
|
|
606
|
-
matches: function(attrs) {
|
|
607
|
-
return !!iteratee(attrs, this)(this.attributes);
|
|
608
|
-
},
|
|
609
|
-
|
|
610
|
-
// Set a hash of model attributes on the object, firing `"change"`. This is
|
|
611
|
-
// the core primitive operation of a model, updating the data and notifying
|
|
612
|
-
// anyone who needs to know about the change in state. The heart of the beast.
|
|
613
|
-
set: function(key, val, options) {
|
|
614
|
-
if (key == null) return this;
|
|
615
|
-
|
|
616
|
-
// Handle both `"key", value` and `{key: value}` -style arguments.
|
|
617
|
-
let attrs;
|
|
618
|
-
if (typeof key === 'object') {
|
|
619
|
-
attrs = key;
|
|
620
|
-
options = val;
|
|
621
|
-
} else {
|
|
622
|
-
(attrs = {})[key] = val;
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
options || (options = {});
|
|
626
|
-
|
|
627
|
-
// Run validation.
|
|
628
|
-
if (!this._validate(attrs, options)) return false;
|
|
629
|
-
|
|
630
|
-
// Extract attributes and options.
|
|
631
|
-
const unset = options.unset;
|
|
632
|
-
const silent = options.silent;
|
|
633
|
-
const changes = [];
|
|
634
|
-
const changing = this._changing;
|
|
635
|
-
this._changing = true;
|
|
636
|
-
|
|
637
|
-
if (!changing) {
|
|
638
|
-
this._previousAttributes = clone(this.attributes);
|
|
639
|
-
this.changed = {};
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
const current = this.attributes;
|
|
643
|
-
const changed = this.changed;
|
|
644
|
-
const prev = this._previousAttributes;
|
|
645
|
-
|
|
646
|
-
// For each `set` attribute, update or delete the current value.
|
|
647
|
-
for (const attr in attrs) {
|
|
648
|
-
val = attrs[attr];
|
|
649
|
-
if (!isEqual(current[attr], val)) changes.push(attr);
|
|
650
|
-
if (!isEqual(prev[attr], val)) {
|
|
651
|
-
changed[attr] = val;
|
|
652
|
-
} else {
|
|
653
|
-
delete changed[attr];
|
|
654
|
-
}
|
|
655
|
-
unset ? delete current[attr] : current[attr] = val;
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
// Update the `id`.
|
|
659
|
-
if (this.idAttribute in attrs) this.id = this.get(this.idAttribute);
|
|
660
|
-
|
|
661
|
-
// Trigger all relevant attribute changes.
|
|
662
|
-
if (!silent) {
|
|
663
|
-
if (changes.length) this._pending = options;
|
|
664
|
-
for (let i = 0; i < changes.length; i++) {
|
|
665
|
-
this.trigger('change:' + changes[i], this, current[changes[i]], options);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
// You might be wondering why there's a `while` loop here. Changes can
|
|
670
|
-
// be recursively nested within `"change"` events.
|
|
671
|
-
if (changing) return this;
|
|
672
|
-
if (!silent) {
|
|
673
|
-
while (this._pending) {
|
|
674
|
-
options = this._pending;
|
|
675
|
-
this._pending = false;
|
|
676
|
-
this.trigger('change', this, options);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
this._pending = false;
|
|
680
|
-
this._changing = false;
|
|
681
|
-
return this;
|
|
682
|
-
},
|
|
683
|
-
|
|
684
|
-
// Remove an attribute from the model, firing `"change"`. `unset` is a noop
|
|
685
|
-
// if the attribute doesn't exist.
|
|
686
|
-
unset: function(attr, options) {
|
|
687
|
-
return this.set(attr, undefined, extend({}, options, {unset: true}));
|
|
688
|
-
},
|
|
689
|
-
|
|
690
|
-
// Clear all attributes on the model, firing `"change"`.
|
|
691
|
-
clear: function(options) {
|
|
692
|
-
const attrs = {};
|
|
693
|
-
for (const key in this.attributes) attrs[key] = undefined;
|
|
694
|
-
return this.set(attrs, extend({}, options, {unset: true}));
|
|
695
|
-
},
|
|
696
|
-
|
|
697
|
-
// Determine if the model has changed since the last `"change"` event.
|
|
698
|
-
// If you specify an attribute name, determine if that attribute has changed.
|
|
699
|
-
hasChanged: function(attr) {
|
|
700
|
-
if (attr == null) return !isEmpty(this.changed);
|
|
701
|
-
return has(this.changed, attr);
|
|
702
|
-
},
|
|
703
|
-
|
|
704
|
-
// Return an object containing all the attributes that have changed, or
|
|
705
|
-
// false if there are no changed attributes. Useful for determining what
|
|
706
|
-
// parts of a view need to be updated and/or what attributes need to be
|
|
707
|
-
// persisted to the server. Unset attributes will be set to undefined.
|
|
708
|
-
// You can also pass an attributes object to diff against the model,
|
|
709
|
-
// determining if there *would be* a change.
|
|
710
|
-
changedAttributes: function(diff) {
|
|
711
|
-
if (!diff) return this.hasChanged() ? clone(this.changed) : false;
|
|
712
|
-
const old = this._changing ? this._previousAttributes : this.attributes;
|
|
713
|
-
const changed = {};
|
|
714
|
-
let hasChanged;
|
|
715
|
-
for (const attr in diff) {
|
|
716
|
-
const val = diff[attr];
|
|
717
|
-
if (isEqual(old[attr], val)) continue;
|
|
718
|
-
changed[attr] = val;
|
|
719
|
-
hasChanged = true;
|
|
720
|
-
}
|
|
721
|
-
return hasChanged ? changed : false;
|
|
722
|
-
},
|
|
723
|
-
|
|
724
|
-
// Get the previous value of an attribute, recorded at the time the last
|
|
725
|
-
// `"change"` event was fired.
|
|
726
|
-
previous: function(attr) {
|
|
727
|
-
if (attr == null || !this._previousAttributes) return null;
|
|
728
|
-
return this._previousAttributes[attr];
|
|
729
|
-
},
|
|
730
|
-
|
|
731
|
-
// Get all of the attributes of the model at the time of the previous
|
|
732
|
-
// `"change"` event.
|
|
733
|
-
previousAttributes: function() {
|
|
734
|
-
return clone(this._previousAttributes);
|
|
735
|
-
},
|
|
736
|
-
|
|
737
|
-
// Fetch the model from the server, merging the response with the model's
|
|
738
|
-
// local attributes. Any changed attributes will trigger a "change" event.
|
|
739
|
-
fetch: function(options) {
|
|
740
|
-
options = extend({parse: true}, options);
|
|
741
|
-
const model = this;
|
|
742
|
-
const success = options.success;
|
|
743
|
-
options.success = function(resp) {
|
|
744
|
-
const serverAttrs = options.parse ? model.parse(resp, options) : resp;
|
|
745
|
-
if (!model.set(serverAttrs, options)) return false;
|
|
746
|
-
if (success) success.call(options.context, model, resp, options);
|
|
747
|
-
model.trigger('sync', model, resp, options);
|
|
748
|
-
};
|
|
749
|
-
wrapError(this, options);
|
|
750
|
-
return this.sync('read', this, options);
|
|
751
|
-
},
|
|
752
|
-
|
|
753
|
-
// Set a hash of model attributes, and sync the model to the server.
|
|
754
|
-
// If the server returns an attributes hash that differs, the model's
|
|
755
|
-
// state will be `set` again.
|
|
756
|
-
save: function(key, val, options) {
|
|
757
|
-
// Handle both `"key", value` and `{key: value}` -style arguments.
|
|
758
|
-
let attrs;
|
|
759
|
-
if (key == null || typeof key === 'object') {
|
|
760
|
-
attrs = key;
|
|
761
|
-
options = val;
|
|
762
|
-
} else {
|
|
763
|
-
(attrs = {})[key] = val;
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
options = extend({validate: true, parse: true}, options);
|
|
767
|
-
const wait = options.wait;
|
|
768
|
-
const return_promise = options.promise;
|
|
769
|
-
const promise = return_promise && getResolveablePromise();
|
|
770
|
-
|
|
771
|
-
// If we're not waiting and attributes exist, save acts as
|
|
772
|
-
// `set(attr).save(null, opts)` with validation. Otherwise, check if
|
|
773
|
-
// the model will be valid when the attributes, if any, are set.
|
|
774
|
-
if (attrs && !wait) {
|
|
775
|
-
if (!this.set(attrs, options)) return false;
|
|
776
|
-
} else if (!this._validate(attrs, options)) {
|
|
777
|
-
return false;
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
// After a successful server-side save, the client is (optionally)
|
|
781
|
-
// updated with the server-side state.
|
|
782
|
-
const model = this;
|
|
783
|
-
const success = options.success;
|
|
784
|
-
const attributes = this.attributes;
|
|
785
|
-
options.success = function(resp) {
|
|
786
|
-
// Ensure attributes are restored during synchronous saves.
|
|
787
|
-
model.attributes = attributes;
|
|
788
|
-
let serverAttrs = options.parse ? model.parse(resp, options) : resp;
|
|
789
|
-
if (wait) serverAttrs = extend({}, attrs, serverAttrs);
|
|
790
|
-
if (serverAttrs && !model.set(serverAttrs, options)) return false;
|
|
791
|
-
if (success) success.call(options.context, model, resp, options);
|
|
792
|
-
model.trigger('sync', model, resp, options);
|
|
793
|
-
if (return_promise) promise.resolve();
|
|
794
|
-
};
|
|
795
|
-
wrapError(this, options);
|
|
796
|
-
|
|
797
|
-
// Set temporary attributes if `{wait: true}` to properly find new ids.
|
|
798
|
-
if (attrs && wait) this.attributes = extend({}, attributes, attrs);
|
|
799
|
-
|
|
800
|
-
const method = this.isNew() ? 'create' : options.patch ? 'patch' : 'update';
|
|
801
|
-
if (method === 'patch' && !options.attrs) options.attrs = attrs;
|
|
802
|
-
const xhr = this.sync(method, this, options);
|
|
803
|
-
|
|
804
|
-
// Restore attributes.
|
|
805
|
-
this.attributes = attributes;
|
|
806
|
-
|
|
807
|
-
if (return_promise) {
|
|
808
|
-
return promise;
|
|
809
|
-
} else {
|
|
810
|
-
return xhr;
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
},
|
|
814
|
-
|
|
815
|
-
// Destroy this model on the server if it was already persisted.
|
|
816
|
-
// Optimistically removes the model from its collection, if it has one.
|
|
817
|
-
// If `wait: true` is passed, waits for the server to respond before removal.
|
|
818
|
-
destroy: function(options) {
|
|
819
|
-
options = options ? clone(options) : {};
|
|
820
|
-
const model = this;
|
|
821
|
-
const success = options.success;
|
|
822
|
-
const wait = options.wait;
|
|
823
|
-
|
|
824
|
-
const destroy = function() {
|
|
825
|
-
model.stopListening();
|
|
826
|
-
model.trigger('destroy', model, model.collection, options);
|
|
827
|
-
};
|
|
828
|
-
|
|
829
|
-
options.success = function(resp) {
|
|
830
|
-
if (wait) destroy();
|
|
831
|
-
if (success) success.call(options.context, model, resp, options);
|
|
832
|
-
if (!model.isNew()) model.trigger('sync', model, resp, options);
|
|
833
|
-
};
|
|
834
|
-
|
|
835
|
-
let xhr = false;
|
|
836
|
-
if (this.isNew()) {
|
|
837
|
-
defer(options.success);
|
|
838
|
-
} else {
|
|
839
|
-
wrapError(this, options);
|
|
840
|
-
xhr = this.sync('delete', this, options);
|
|
841
|
-
}
|
|
842
|
-
if (!wait) destroy();
|
|
843
|
-
return xhr;
|
|
844
|
-
},
|
|
845
|
-
|
|
846
|
-
// Default URL for the model's representation on the server -- if you're
|
|
847
|
-
// using Backbone's restful methods, override this to change the endpoint
|
|
848
|
-
// that will be called.
|
|
849
|
-
url: function() {
|
|
850
|
-
const base =
|
|
851
|
-
result(this, 'urlRoot') ||
|
|
852
|
-
result(this.collection, 'url') ||
|
|
853
|
-
urlError();
|
|
854
|
-
if (this.isNew()) return base;
|
|
855
|
-
const id = this.get(this.idAttribute);
|
|
856
|
-
return base.replace(/[^\/]$/, '$&/') + encodeURIComponent(id);
|
|
857
|
-
},
|
|
858
|
-
|
|
859
|
-
// **parse** converts a response into the hash of attributes to be `set` on
|
|
860
|
-
// the model. The default implementation is just to pass the response along.
|
|
861
|
-
parse: function(resp, options) {
|
|
862
|
-
return resp;
|
|
863
|
-
},
|
|
864
|
-
|
|
865
|
-
// Create a new model with identical attributes to this one.
|
|
866
|
-
clone: function() {
|
|
867
|
-
return new this.constructor(this.attributes);
|
|
868
|
-
},
|
|
869
|
-
|
|
870
|
-
// A model is new if it has never been saved to the server, and lacks an id.
|
|
871
|
-
isNew: function() {
|
|
872
|
-
return !this.has(this.idAttribute);
|
|
873
|
-
},
|
|
874
|
-
|
|
875
|
-
// Check if the model is currently in a valid state.
|
|
876
|
-
isValid: function(options) {
|
|
877
|
-
return this._validate({}, extend({}, options, {validate: true}));
|
|
878
|
-
},
|
|
879
|
-
|
|
880
|
-
// Run validation against the next complete set of model attributes,
|
|
881
|
-
// returning `true` if all is well. Otherwise, fire an `"invalid"` event.
|
|
882
|
-
_validate: function(attrs, options) {
|
|
883
|
-
if (!options.validate || !this.validate) return true;
|
|
884
|
-
attrs = extend({}, this.attributes, attrs);
|
|
885
|
-
const error = this.validationError = this.validate(attrs, options) || null;
|
|
886
|
-
if (!error) return true;
|
|
887
|
-
this.trigger('invalid', this, error, extend(options, {validationError: error}));
|
|
888
|
-
return false;
|
|
889
|
-
}
|
|
890
|
-
});
|
|
891
|
-
|
|
892
|
-
// Backbone.js 1.4.0
|
|
893
|
-
|
|
894
|
-
const slice = Array.prototype.slice;
|
|
895
|
-
|
|
896
|
-
// Create a new **Collection**, perhaps to contain a specific type of `model`.
|
|
897
|
-
// If a `comparator` is specified, the Collection will maintain
|
|
898
|
-
// its models in sort order, as they're added and removed.
|
|
899
|
-
const Collection = function(models, options) {
|
|
900
|
-
options || (options = {});
|
|
901
|
-
this.preinitialize.apply(this, arguments);
|
|
902
|
-
if (options.model) this.model = options.model;
|
|
903
|
-
if (options.comparator !== undefined) this.comparator = options.comparator;
|
|
904
|
-
this._reset();
|
|
905
|
-
this.initialize.apply(this, arguments);
|
|
906
|
-
if (models) this.reset(models, extend({silent: true}, options));
|
|
907
|
-
};
|
|
908
|
-
|
|
909
|
-
Collection.extend = inherits;
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
// Default options for `Collection#set`.
|
|
913
|
-
const setOptions = {add: true, remove: true, merge: true};
|
|
914
|
-
const addOptions = {add: true, remove: false};
|
|
915
|
-
|
|
916
|
-
// Splices `insert` into `array` at index `at`.
|
|
917
|
-
const splice = function(array, insert, at) {
|
|
918
|
-
at = Math.min(Math.max(at, 0), array.length);
|
|
919
|
-
const tail = Array(array.length - at);
|
|
920
|
-
const length = insert.length;
|
|
921
|
-
let i;
|
|
922
|
-
for (i = 0; i < tail.length; i++) tail[i] = array[i + at];
|
|
923
|
-
for (i = 0; i < length; i++) array[i + at] = insert[i];
|
|
924
|
-
for (i = 0; i < tail.length; i++) array[i + length + at] = tail[i];
|
|
925
|
-
};
|
|
926
|
-
|
|
927
|
-
// Define the Collection's inheritable methods.
|
|
928
|
-
Object.assign(Collection.prototype, Events, {
|
|
929
|
-
|
|
930
|
-
// The default model for a collection is just a **Backbone.Model**.
|
|
931
|
-
// This should be overridden in most cases.
|
|
932
|
-
model: Model,
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
// preinitialize is an empty function by default. You can override it with a function
|
|
936
|
-
// or object. preinitialize will run before any instantiation logic is run in the Collection.
|
|
937
|
-
preinitialize: function(){},
|
|
938
|
-
|
|
939
|
-
// Initialize is an empty function by default. Override it with your own
|
|
940
|
-
// initialization logic.
|
|
941
|
-
initialize: function(){},
|
|
942
|
-
|
|
943
|
-
// The JSON representation of a Collection is an array of the
|
|
944
|
-
// models' attributes.
|
|
945
|
-
toJSON: function(options) {
|
|
946
|
-
return this.map(function(model) { return model.toJSON(options); });
|
|
947
|
-
},
|
|
948
|
-
|
|
949
|
-
// Proxy `Backbone.sync` by default.
|
|
950
|
-
sync: function(method, model, options) {
|
|
951
|
-
return getSyncMethod(this)(method, model, options);
|
|
952
|
-
},
|
|
953
|
-
|
|
954
|
-
// Add a model, or list of models to the set. `models` may be Backbone
|
|
955
|
-
// Models or raw JavaScript objects to be converted to Models, or any
|
|
956
|
-
// combination of the two.
|
|
957
|
-
add: function(models, options) {
|
|
958
|
-
return this.set(models, extend({merge: false}, options, addOptions));
|
|
959
|
-
},
|
|
960
|
-
|
|
961
|
-
// Remove a model, or a list of models from the set.
|
|
962
|
-
remove: function(models, options) {
|
|
963
|
-
options = extend({}, options);
|
|
964
|
-
const singular = !Array.isArray(models);
|
|
965
|
-
models = singular ? [models] : models.slice();
|
|
966
|
-
const removed = this._removeModels(models, options);
|
|
967
|
-
if (!options.silent && removed.length) {
|
|
968
|
-
options.changes = {added: [], merged: [], removed: removed};
|
|
969
|
-
this.trigger('update', this, options);
|
|
970
|
-
}
|
|
971
|
-
return singular ? removed[0] : removed;
|
|
972
|
-
},
|
|
973
|
-
|
|
974
|
-
// Update a collection by `set`-ing a new list of models, adding new ones,
|
|
975
|
-
// removing models that are no longer present, and merging models that
|
|
976
|
-
// already exist in the collection, as necessary. Similar to **Model#set**,
|
|
977
|
-
// the core operation for updating the data contained by the collection.
|
|
978
|
-
set: function(models, options) {
|
|
979
|
-
if (models == null) return;
|
|
980
|
-
|
|
981
|
-
options = extend({}, setOptions, options);
|
|
982
|
-
if (options.parse && !this._isModel(models)) {
|
|
983
|
-
models = this.parse(models, options) || [];
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
const singular = !Array.isArray(models);
|
|
987
|
-
models = singular ? [models] : models.slice();
|
|
988
|
-
|
|
989
|
-
let at = options.at;
|
|
990
|
-
if (at != null) at = +at;
|
|
991
|
-
if (at > this.length) at = this.length;
|
|
992
|
-
if (at < 0) at += this.length + 1;
|
|
993
|
-
|
|
994
|
-
const set = [];
|
|
995
|
-
const toAdd = [];
|
|
996
|
-
const toMerge = [];
|
|
997
|
-
const toRemove = [];
|
|
998
|
-
const modelMap = {};
|
|
999
|
-
|
|
1000
|
-
const add = options.add;
|
|
1001
|
-
const merge = options.merge;
|
|
1002
|
-
const remove = options.remove;
|
|
1003
|
-
|
|
1004
|
-
let sort = false;
|
|
1005
|
-
const sortable = this.comparator && at == null && options.sort !== false;
|
|
1006
|
-
const sortAttr = isString(this.comparator) ? this.comparator : null;
|
|
1007
|
-
|
|
1008
|
-
// Turn bare objects into model references, and prevent invalid models
|
|
1009
|
-
// from being added.
|
|
1010
|
-
let model, i;
|
|
1011
|
-
for (i = 0; i < models.length; i++) {
|
|
1012
|
-
model = models[i];
|
|
1013
|
-
|
|
1014
|
-
// If a duplicate is found, prevent it from being added and
|
|
1015
|
-
// optionally merge it into the existing model.
|
|
1016
|
-
const existing = this.get(model);
|
|
1017
|
-
if (existing) {
|
|
1018
|
-
if (merge && model !== existing) {
|
|
1019
|
-
let attrs = this._isModel(model) ? model.attributes : model;
|
|
1020
|
-
if (options.parse) attrs = existing.parse(attrs, options);
|
|
1021
|
-
existing.set(attrs, options);
|
|
1022
|
-
toMerge.push(existing);
|
|
1023
|
-
if (sortable && !sort) sort = existing.hasChanged(sortAttr);
|
|
1024
|
-
}
|
|
1025
|
-
if (!modelMap[existing.cid]) {
|
|
1026
|
-
modelMap[existing.cid] = true;
|
|
1027
|
-
set.push(existing);
|
|
1028
|
-
}
|
|
1029
|
-
models[i] = existing;
|
|
1030
|
-
|
|
1031
|
-
// If this is a new, valid model, push it to the `toAdd` list.
|
|
1032
|
-
} else if (add) {
|
|
1033
|
-
model = models[i] = this._prepareModel(model, options);
|
|
1034
|
-
if (model) {
|
|
1035
|
-
toAdd.push(model);
|
|
1036
|
-
this._addReference(model, options);
|
|
1037
|
-
modelMap[model.cid] = true;
|
|
1038
|
-
set.push(model);
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
// Remove stale models.
|
|
1044
|
-
if (remove) {
|
|
1045
|
-
for (i = 0; i < this.length; i++) {
|
|
1046
|
-
model = this.models[i];
|
|
1047
|
-
if (!modelMap[model.cid]) toRemove.push(model);
|
|
1048
|
-
}
|
|
1049
|
-
if (toRemove.length) this._removeModels(toRemove, options);
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
// See if sorting is needed, update `length` and splice in new models.
|
|
1053
|
-
let orderChanged = false;
|
|
1054
|
-
const replace = !sortable && add && remove;
|
|
1055
|
-
if (set.length && replace) {
|
|
1056
|
-
orderChanged = this.length !== set.length || some(this.models, (m, index) => m !== set[index]);
|
|
1057
|
-
this.models.length = 0;
|
|
1058
|
-
splice(this.models, set, 0);
|
|
1059
|
-
this.length = this.models.length;
|
|
1060
|
-
} else if (toAdd.length) {
|
|
1061
|
-
if (sortable) sort = true;
|
|
1062
|
-
splice(this.models, toAdd, at == null ? this.length : at);
|
|
1063
|
-
this.length = this.models.length;
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
// Silently sort the collection if appropriate.
|
|
1067
|
-
if (sort) this.sort({silent: true});
|
|
1068
|
-
|
|
1069
|
-
// Unless silenced, it's time to fire all appropriate add/sort/update events.
|
|
1070
|
-
if (!options.silent) {
|
|
1071
|
-
for (i = 0; i < toAdd.length; i++) {
|
|
1072
|
-
if (at != null) options.index = at + i;
|
|
1073
|
-
model = toAdd[i];
|
|
1074
|
-
model.trigger('add', model, this, options);
|
|
1075
|
-
}
|
|
1076
|
-
if (sort || orderChanged) this.trigger('sort', this, options);
|
|
1077
|
-
if (toAdd.length || toRemove.length || toMerge.length) {
|
|
1078
|
-
options.changes = {
|
|
1079
|
-
added: toAdd,
|
|
1080
|
-
removed: toRemove,
|
|
1081
|
-
merged: toMerge
|
|
1082
|
-
};
|
|
1083
|
-
this.trigger('update', this, options);
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
|
|
1087
|
-
// Return the added (or merged) model (or models).
|
|
1088
|
-
return singular ? models[0] : models;
|
|
1089
|
-
},
|
|
1090
|
-
|
|
1091
|
-
clearStore: async function(options={}) {
|
|
1092
|
-
await Promise.all(Array.from(this.models).map(m => {
|
|
1093
|
-
return new Promise(
|
|
1094
|
-
resolve => {
|
|
1095
|
-
m.destroy(Object.assign(options, {
|
|
1096
|
-
'success': resolve,
|
|
1097
|
-
'error': (m, e) => { console.error(e); resolve(); }
|
|
1098
|
-
}));
|
|
1099
|
-
}
|
|
1100
|
-
);
|
|
1101
|
-
}));
|
|
1102
|
-
await this.browserStorage.clear();
|
|
1103
|
-
this.reset();
|
|
1104
|
-
},
|
|
1105
|
-
|
|
1106
|
-
// When you have more items than you want to add or remove individually,
|
|
1107
|
-
// you can reset the entire set with a new list of models, without firing
|
|
1108
|
-
// any granular `add` or `remove` events. Fires `reset` when finished.
|
|
1109
|
-
// Useful for bulk operations and optimizations.
|
|
1110
|
-
reset: function(models, options) {
|
|
1111
|
-
options = options ? clone(options) : {};
|
|
1112
|
-
for (let i = 0; i < this.models.length; i++) {
|
|
1113
|
-
this._removeReference(this.models[i], options);
|
|
1114
|
-
}
|
|
1115
|
-
options.previousModels = this.models;
|
|
1116
|
-
this._reset();
|
|
1117
|
-
models = this.add(models, extend({silent: true}, options));
|
|
1118
|
-
if (!options.silent) this.trigger('reset', this, options);
|
|
1119
|
-
return models;
|
|
1120
|
-
},
|
|
1121
|
-
|
|
1122
|
-
// Add a model to the end of the collection.
|
|
1123
|
-
push: function(model, options) {
|
|
1124
|
-
return this.add(model, extend({at: this.length}, options));
|
|
1125
|
-
},
|
|
1126
|
-
|
|
1127
|
-
// Remove a model from the end of the collection.
|
|
1128
|
-
pop: function(options) {
|
|
1129
|
-
const model = this.at(this.length - 1);
|
|
1130
|
-
return this.remove(model, options);
|
|
1131
|
-
},
|
|
1132
|
-
|
|
1133
|
-
// Add a model to the beginning of the collection.
|
|
1134
|
-
unshift: function(model, options) {
|
|
1135
|
-
return this.add(model, extend({at: 0}, options));
|
|
1136
|
-
},
|
|
1137
|
-
|
|
1138
|
-
// Remove a model from the beginning of the collection.
|
|
1139
|
-
shift: function(options) {
|
|
1140
|
-
const model = this.at(0);
|
|
1141
|
-
return this.remove(model, options);
|
|
1142
|
-
},
|
|
1143
|
-
|
|
1144
|
-
// Slice out a sub-array of models from the collection.
|
|
1145
|
-
slice: function() {
|
|
1146
|
-
return slice.apply(this.models, arguments);
|
|
1147
|
-
},
|
|
1148
|
-
|
|
1149
|
-
filter: function(callback, thisArg) {
|
|
1150
|
-
return this.models.filter(
|
|
1151
|
-
isFunction(callback) ? callback : m => m.matches(callback),
|
|
1152
|
-
thisArg
|
|
1153
|
-
);
|
|
1154
|
-
},
|
|
1155
|
-
|
|
1156
|
-
every: function(pred) {
|
|
1157
|
-
return every(this.models.map(m => m.attributes), pred);
|
|
1158
|
-
},
|
|
1159
|
-
|
|
1160
|
-
difference: function(values) {
|
|
1161
|
-
return difference(this.models, values);
|
|
1162
|
-
},
|
|
1163
|
-
|
|
1164
|
-
max: function() {
|
|
1165
|
-
return Math.max.apply(Math, this.models);
|
|
1166
|
-
},
|
|
1167
|
-
|
|
1168
|
-
min: function() {
|
|
1169
|
-
return Math.min.apply(Math, this.models);
|
|
1170
|
-
},
|
|
1171
|
-
|
|
1172
|
-
drop: function(n=1) {
|
|
1173
|
-
return this.models.slice(n);
|
|
1174
|
-
},
|
|
1175
|
-
|
|
1176
|
-
some: function(pred) {
|
|
1177
|
-
return some(this.models.map(m => m.attributes), pred);
|
|
1178
|
-
},
|
|
1179
|
-
|
|
1180
|
-
sortBy: function(iteratee) {
|
|
1181
|
-
return sortBy(
|
|
1182
|
-
this.models,
|
|
1183
|
-
isFunction(iteratee) ? iteratee : m => isString(iteratee) ? m.get(iteratee) : m.matches(iteratee),
|
|
1184
|
-
);
|
|
1185
|
-
},
|
|
1186
|
-
|
|
1187
|
-
isEmpty: function() {
|
|
1188
|
-
return isEmpty(this.models);
|
|
1189
|
-
},
|
|
1190
|
-
|
|
1191
|
-
keyBy: function(iteratee) {
|
|
1192
|
-
return keyBy(this.models, iteratee);
|
|
1193
|
-
},
|
|
1194
|
-
|
|
1195
|
-
each: function(callback, thisArg) {
|
|
1196
|
-
return this.forEach(callback, thisArg);
|
|
1197
|
-
},
|
|
1198
|
-
|
|
1199
|
-
forEach: function(callback, thisArg) {
|
|
1200
|
-
return this.models.forEach(callback, thisArg);
|
|
1201
|
-
},
|
|
1202
|
-
|
|
1203
|
-
includes: function(item) {
|
|
1204
|
-
return this.models.includes(item);
|
|
1205
|
-
},
|
|
1206
|
-
|
|
1207
|
-
size: function() {
|
|
1208
|
-
return this.models.length;
|
|
1209
|
-
},
|
|
1210
|
-
|
|
1211
|
-
countBy: function(f) {
|
|
1212
|
-
return countBy(
|
|
1213
|
-
this.models,
|
|
1214
|
-
isFunction(f) ? f : m => isString(f) ? m.get(f) : m.matches(f),
|
|
1215
|
-
);
|
|
1216
|
-
},
|
|
1217
|
-
|
|
1218
|
-
groupBy: function(pred) {
|
|
1219
|
-
return groupBy(
|
|
1220
|
-
this.models,
|
|
1221
|
-
isFunction(pred) ? pred : m => isString(pred) ? m.get(pred) : m.matches(pred),
|
|
1222
|
-
);
|
|
1223
|
-
},
|
|
1224
|
-
|
|
1225
|
-
indexOf: function(fromIndex) {
|
|
1226
|
-
return indexOf$1(this.models, fromIndex);
|
|
1227
|
-
},
|
|
1228
|
-
|
|
1229
|
-
findLastIndex: function(pred, fromIndex) {
|
|
1230
|
-
return findLastIndex(
|
|
1231
|
-
this.models,
|
|
1232
|
-
isFunction(pred) ? pred : m => isString(pred) ? m.get(pred) : m.matches(pred),
|
|
1233
|
-
fromIndex
|
|
1234
|
-
);
|
|
1235
|
-
},
|
|
1236
|
-
|
|
1237
|
-
lastIndexOf: function(fromIndex) {
|
|
1238
|
-
return lastIndexOf(this.models, fromIndex);
|
|
1239
|
-
},
|
|
1240
|
-
|
|
1241
|
-
findIndex: function(pred) {
|
|
1242
|
-
return findIndex(
|
|
1243
|
-
this.models,
|
|
1244
|
-
isFunction(pred) ? pred : m => isString(pred) ? m.get(pred) : m.matches(pred),
|
|
1245
|
-
);
|
|
1246
|
-
},
|
|
1247
|
-
|
|
1248
|
-
last: function() {
|
|
1249
|
-
const length = this.models == null ? 0 : this.models.length;
|
|
1250
|
-
return length ? this.models[length - 1] : undefined;
|
|
1251
|
-
},
|
|
1252
|
-
|
|
1253
|
-
head: function() {
|
|
1254
|
-
return this.models[0];
|
|
1255
|
-
},
|
|
1256
|
-
|
|
1257
|
-
first: function() {
|
|
1258
|
-
return this.head();
|
|
1259
|
-
},
|
|
1260
|
-
|
|
1261
|
-
map: function(cb, thisArg) {
|
|
1262
|
-
return this.models.map(
|
|
1263
|
-
isFunction(cb) ? cb : m => isString(cb) ? m.get(cb) : m.matches(cb),
|
|
1264
|
-
thisArg
|
|
1265
|
-
);
|
|
1266
|
-
},
|
|
1267
|
-
|
|
1268
|
-
reduce: function(callback, initialValue) {
|
|
1269
|
-
return this.models.reduce(callback, initialValue || this.models[0]);
|
|
1270
|
-
},
|
|
1271
|
-
|
|
1272
|
-
reduceRight: function(callback, initialValue) {
|
|
1273
|
-
return this.models.reduceRight(callback, initialValue || this.models[0]);
|
|
1274
|
-
},
|
|
1275
|
-
|
|
1276
|
-
toArray: function() {
|
|
1277
|
-
return Array.from(this.models);
|
|
1278
|
-
},
|
|
1279
|
-
|
|
1280
|
-
// Get a model from the set by id, cid, model object with id or cid
|
|
1281
|
-
// properties, or an attributes object that is transformed through modelId.
|
|
1282
|
-
get: function(obj) {
|
|
1283
|
-
if (obj == null) return undefined;
|
|
1284
|
-
return this._byId[obj] ||
|
|
1285
|
-
this._byId[this.modelId(this._isModel(obj) ? obj.attributes : obj)] ||
|
|
1286
|
-
obj.cid && this._byId[obj.cid];
|
|
1287
|
-
},
|
|
1288
|
-
|
|
1289
|
-
// Returns `true` if the model is in the collection.
|
|
1290
|
-
has: function(obj) {
|
|
1291
|
-
return this.get(obj) != null;
|
|
1292
|
-
},
|
|
1293
|
-
|
|
1294
|
-
// Get the model at the given index.
|
|
1295
|
-
at: function(index) {
|
|
1296
|
-
if (index < 0) index += this.length;
|
|
1297
|
-
return this.models[index];
|
|
1298
|
-
},
|
|
1299
|
-
|
|
1300
|
-
// Return models with matching attributes. Useful for simple cases of
|
|
1301
|
-
// `filter`.
|
|
1302
|
-
where: function(attrs, first) {
|
|
1303
|
-
return this[first ? 'find' : 'filter'](attrs);
|
|
1304
|
-
},
|
|
1305
|
-
|
|
1306
|
-
// Return the first model with matching attributes. Useful for simple cases
|
|
1307
|
-
// of `find`.
|
|
1308
|
-
findWhere: function(attrs) {
|
|
1309
|
-
return this.where(attrs, true);
|
|
1310
|
-
},
|
|
1311
|
-
|
|
1312
|
-
find: function(predicate, fromIndex) {
|
|
1313
|
-
const pred = isFunction(predicate) ? predicate : m => m.matches(predicate);
|
|
1314
|
-
return this.models.find(pred, fromIndex);
|
|
1315
|
-
},
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
// Force the collection to re-sort itself. You don't need to call this under
|
|
1319
|
-
// normal circumstances, as the set will maintain sort order as each item
|
|
1320
|
-
// is added.
|
|
1321
|
-
sort: function(options) {
|
|
1322
|
-
let comparator = this.comparator;
|
|
1323
|
-
if (!comparator) throw new Error('Cannot sort a set without a comparator');
|
|
1324
|
-
options || (options = {});
|
|
1325
|
-
|
|
1326
|
-
const length = comparator.length;
|
|
1327
|
-
if (isFunction(comparator)) comparator = comparator.bind(this);
|
|
1328
|
-
|
|
1329
|
-
// Run sort based on type of `comparator`.
|
|
1330
|
-
if (length === 1 || isString(comparator)) {
|
|
1331
|
-
this.models = this.sortBy(comparator);
|
|
1332
|
-
} else {
|
|
1333
|
-
this.models.sort(comparator);
|
|
1334
|
-
}
|
|
1335
|
-
if (!options.silent) this.trigger('sort', this, options);
|
|
1336
|
-
return this;
|
|
1337
|
-
},
|
|
1338
|
-
|
|
1339
|
-
// Pluck an attribute from each model in the collection.
|
|
1340
|
-
pluck: function(attr) {
|
|
1341
|
-
return this.map(attr + '');
|
|
1342
|
-
},
|
|
1343
|
-
|
|
1344
|
-
// Fetch the default set of models for this collection, resetting the
|
|
1345
|
-
// collection when they arrive. If `reset: true` is passed, the response
|
|
1346
|
-
// data will be passed through the `reset` method instead of `set`.
|
|
1347
|
-
fetch: function(options) {
|
|
1348
|
-
options = extend({parse: true}, options);
|
|
1349
|
-
const success = options.success;
|
|
1350
|
-
const collection = this;
|
|
1351
|
-
const promise = options.promise && getResolveablePromise();
|
|
1352
|
-
options.success = function(resp) {
|
|
1353
|
-
const method = options.reset ? 'reset' : 'set';
|
|
1354
|
-
collection[method](resp, options);
|
|
1355
|
-
if (success) success.call(options.context, collection, resp, options);
|
|
1356
|
-
promise && promise.resolve();
|
|
1357
|
-
collection.trigger('sync', collection, resp, options);
|
|
1358
|
-
};
|
|
1359
|
-
wrapError(this, options);
|
|
1360
|
-
return promise ? promise : this.sync('read', this, options);
|
|
1361
|
-
},
|
|
1362
|
-
|
|
1363
|
-
// Create a new instance of a model in this collection. Add the model to the
|
|
1364
|
-
// collection immediately, unless `wait: true` is passed, in which case we
|
|
1365
|
-
// wait for the server to agree.
|
|
1366
|
-
create: function(model, options) {
|
|
1367
|
-
options = options ? clone(options) : {};
|
|
1368
|
-
const wait = options.wait;
|
|
1369
|
-
const return_promise = options.promise;
|
|
1370
|
-
const promise = return_promise && getResolveablePromise();
|
|
1371
|
-
|
|
1372
|
-
model = this._prepareModel(model, options);
|
|
1373
|
-
if (!model) return false;
|
|
1374
|
-
if (!wait) this.add(model, options);
|
|
1375
|
-
const collection = this;
|
|
1376
|
-
const success = options.success;
|
|
1377
|
-
options.success = function(m, resp, callbackOpts) {
|
|
1378
|
-
if (wait) {
|
|
1379
|
-
collection.add(m, callbackOpts);
|
|
1380
|
-
}
|
|
1381
|
-
if (success) {
|
|
1382
|
-
success.call(callbackOpts.context, m, resp, callbackOpts);
|
|
1383
|
-
}
|
|
1384
|
-
if (return_promise) {
|
|
1385
|
-
promise.resolve(m);
|
|
1386
|
-
}
|
|
1387
|
-
};
|
|
1388
|
-
model.save(null, options);
|
|
1389
|
-
if (return_promise) {
|
|
1390
|
-
return promise;
|
|
1391
|
-
} else {
|
|
1392
|
-
return model;
|
|
1393
|
-
}
|
|
1394
|
-
},
|
|
1395
|
-
|
|
1396
|
-
// **parse** converts a response into a list of models to be added to the
|
|
1397
|
-
// collection. The default implementation is just to pass it through.
|
|
1398
|
-
parse: function(resp, options) {
|
|
1399
|
-
return resp;
|
|
1400
|
-
},
|
|
1401
|
-
|
|
1402
|
-
// Create a new collection with an identical list of models as this one.
|
|
1403
|
-
clone: function() {
|
|
1404
|
-
return new this.constructor(this.models, {
|
|
1405
|
-
model: this.model,
|
|
1406
|
-
comparator: this.comparator
|
|
1407
|
-
});
|
|
1408
|
-
},
|
|
1409
|
-
|
|
1410
|
-
// Define how to uniquely identify models in the collection.
|
|
1411
|
-
modelId: function(attrs) {
|
|
1412
|
-
return attrs[this.model.prototype.idAttribute || 'id'];
|
|
1413
|
-
},
|
|
1414
|
-
|
|
1415
|
-
// Get an iterator of all models in this collection.
|
|
1416
|
-
values: function() {
|
|
1417
|
-
return new CollectionIterator(this, ITERATOR_VALUES);
|
|
1418
|
-
},
|
|
1419
|
-
|
|
1420
|
-
// Get an iterator of all model IDs in this collection.
|
|
1421
|
-
keys: function() {
|
|
1422
|
-
return new CollectionIterator(this, ITERATOR_KEYS);
|
|
1423
|
-
},
|
|
1424
|
-
|
|
1425
|
-
// Get an iterator of all [ID, model] tuples in this collection.
|
|
1426
|
-
entries: function() {
|
|
1427
|
-
return new CollectionIterator(this, ITERATOR_KEYSVALUES);
|
|
1428
|
-
},
|
|
1429
|
-
|
|
1430
|
-
// Private method to reset all internal state. Called when the collection
|
|
1431
|
-
// is first initialized or reset.
|
|
1432
|
-
_reset: function() {
|
|
1433
|
-
this.length = 0;
|
|
1434
|
-
this.models = [];
|
|
1435
|
-
this._byId = {};
|
|
1436
|
-
},
|
|
1437
|
-
|
|
1438
|
-
// Prepare a hash of attributes (or other model) to be added to this
|
|
1439
|
-
// collection.
|
|
1440
|
-
_prepareModel: function(attrs, options) {
|
|
1441
|
-
if (this._isModel(attrs)) {
|
|
1442
|
-
if (!attrs.collection) attrs.collection = this;
|
|
1443
|
-
return attrs;
|
|
1444
|
-
}
|
|
1445
|
-
options = options ? clone(options) : {};
|
|
1446
|
-
options.collection = this;
|
|
1447
|
-
const model = new this.model(attrs, options);
|
|
1448
|
-
if (!model.validationError) return model;
|
|
1449
|
-
this.trigger('invalid', this, model.validationError, options);
|
|
1450
|
-
return false;
|
|
1451
|
-
},
|
|
1452
|
-
|
|
1453
|
-
// Internal method called by both remove and set.
|
|
1454
|
-
_removeModels: function(models, options) {
|
|
1455
|
-
const removed = [];
|
|
1456
|
-
for (let i = 0; i < models.length; i++) {
|
|
1457
|
-
const model = this.get(models[i]);
|
|
1458
|
-
if (!model) continue;
|
|
1459
|
-
|
|
1460
|
-
const index = this.indexOf(model);
|
|
1461
|
-
this.models.splice(index, 1);
|
|
1462
|
-
this.length--;
|
|
1463
|
-
|
|
1464
|
-
// Remove references before triggering 'remove' event to prevent an
|
|
1465
|
-
// infinite loop. #3693
|
|
1466
|
-
delete this._byId[model.cid];
|
|
1467
|
-
const id = this.modelId(model.attributes);
|
|
1468
|
-
if (id != null) delete this._byId[id];
|
|
1469
|
-
|
|
1470
|
-
if (!options.silent) {
|
|
1471
|
-
options.index = index;
|
|
1472
|
-
model.trigger('remove', model, this, options);
|
|
1473
|
-
}
|
|
1474
|
-
|
|
1475
|
-
removed.push(model);
|
|
1476
|
-
this._removeReference(model, options);
|
|
1477
|
-
}
|
|
1478
|
-
return removed;
|
|
1479
|
-
},
|
|
1480
|
-
|
|
1481
|
-
// Method for checking whether an object should be considered a model for
|
|
1482
|
-
// the purposes of adding to the collection.
|
|
1483
|
-
_isModel: function(model) {
|
|
1484
|
-
return model instanceof Model;
|
|
1485
|
-
},
|
|
1486
|
-
|
|
1487
|
-
// Internal method to create a model's ties to a collection.
|
|
1488
|
-
_addReference: function(model, options) {
|
|
1489
|
-
this._byId[model.cid] = model;
|
|
1490
|
-
const id = this.modelId(model.attributes);
|
|
1491
|
-
if (id != null) this._byId[id] = model;
|
|
1492
|
-
model.on('all', this._onModelEvent, this);
|
|
1493
|
-
},
|
|
1494
|
-
|
|
1495
|
-
// Internal method to sever a model's ties to a collection.
|
|
1496
|
-
_removeReference: function(model, options) {
|
|
1497
|
-
delete this._byId[model.cid];
|
|
1498
|
-
const id = this.modelId(model.attributes);
|
|
1499
|
-
if (id != null) delete this._byId[id];
|
|
1500
|
-
if (this === model.collection) delete model.collection;
|
|
1501
|
-
model.off('all', this._onModelEvent, this);
|
|
1502
|
-
},
|
|
1503
|
-
|
|
1504
|
-
// Internal method called every time a model in the set fires an event.
|
|
1505
|
-
// Sets need to update their indexes when models change ids. All other
|
|
1506
|
-
// events simply proxy through. "add" and "remove" events that originate
|
|
1507
|
-
// in other collections are ignored.
|
|
1508
|
-
_onModelEvent: function(event, model, collection, options) {
|
|
1509
|
-
if (model) {
|
|
1510
|
-
if ((event === 'add' || event === 'remove') && collection !== this) return;
|
|
1511
|
-
if (event === 'destroy') this.remove(model, options);
|
|
1512
|
-
if (event === 'change') {
|
|
1513
|
-
const prevId = this.modelId(model.previousAttributes());
|
|
1514
|
-
const id = this.modelId(model.attributes);
|
|
1515
|
-
if (prevId !== id) {
|
|
1516
|
-
if (prevId != null) delete this._byId[prevId];
|
|
1517
|
-
if (id != null) this._byId[id] = model;
|
|
1518
|
-
}
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
this.trigger.apply(this, arguments);
|
|
1522
|
-
}
|
|
1523
|
-
|
|
1524
|
-
});
|
|
1525
|
-
|
|
1526
|
-
// Defining an @@iterator method implements JavaScript's Iterable protocol.
|
|
1527
|
-
// In modern ES2015 browsers, this value is found at Symbol.iterator.
|
|
1528
|
-
/* global Symbol */
|
|
1529
|
-
const $$iterator = typeof Symbol === 'function' && Symbol.iterator;
|
|
1530
|
-
if ($$iterator) {
|
|
1531
|
-
Collection.prototype[$$iterator] = Collection.prototype.values;
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
// CollectionIterator
|
|
1535
|
-
// ------------------
|
|
1536
|
-
|
|
1537
|
-
// A CollectionIterator implements JavaScript's Iterator protocol, allowing the
|
|
1538
|
-
// use of `for of` loops in modern browsers and interoperation between
|
|
1539
|
-
// Collection and other JavaScript functions and third-party libraries
|
|
1540
|
-
// which can operate on Iterables.
|
|
1541
|
-
const CollectionIterator = function(collection, kind) {
|
|
1542
|
-
this._collection = collection;
|
|
1543
|
-
this._kind = kind;
|
|
1544
|
-
this._index = 0;
|
|
1545
|
-
};
|
|
1546
|
-
|
|
1547
|
-
// This "enum" defines the three possible kinds of values which can be emitted
|
|
1548
|
-
// by a CollectionIterator that correspond to the values(), keys() and entries()
|
|
1549
|
-
// methods on Collection, respectively.
|
|
1550
|
-
const ITERATOR_VALUES = 1;
|
|
1551
|
-
const ITERATOR_KEYS = 2;
|
|
1552
|
-
const ITERATOR_KEYSVALUES = 3;
|
|
1553
|
-
|
|
1554
|
-
// All Iterators should themselves be Iterable.
|
|
1555
|
-
if ($$iterator) {
|
|
1556
|
-
CollectionIterator.prototype[$$iterator] = function() {
|
|
1557
|
-
return this;
|
|
1558
|
-
};
|
|
1559
|
-
}
|
|
1560
|
-
|
|
1561
|
-
CollectionIterator.prototype.next = function() {
|
|
1562
|
-
if (this._collection) {
|
|
1563
|
-
|
|
1564
|
-
// Only continue iterating if the iterated collection is long enough.
|
|
1565
|
-
if (this._index < this._collection.length) {
|
|
1566
|
-
const model = this._collection.at(this._index);
|
|
1567
|
-
this._index++;
|
|
1568
|
-
|
|
1569
|
-
// Construct a value depending on what kind of values should be iterated.
|
|
1570
|
-
let value;
|
|
1571
|
-
if (this._kind === ITERATOR_VALUES) {
|
|
1572
|
-
value = model;
|
|
1573
|
-
} else {
|
|
1574
|
-
const id = this._collection.modelId(model.attributes);
|
|
1575
|
-
if (this._kind === ITERATOR_KEYS) {
|
|
1576
|
-
value = id;
|
|
1577
|
-
} else { // ITERATOR_KEYSVALUES
|
|
1578
|
-
value = [id, model];
|
|
1579
|
-
}
|
|
1580
|
-
}
|
|
1581
|
-
return {value: value, done: false};
|
|
1582
|
-
}
|
|
1583
|
-
|
|
1584
|
-
// Once exhausted, remove the reference to the collection so future
|
|
1585
|
-
// calls to the next method always return done.
|
|
1586
|
-
this._collection = undefined;
|
|
1587
|
-
}
|
|
1588
|
-
|
|
1589
|
-
return {value: undefined, done: true};
|
|
1590
|
-
};
|
|
1591
|
-
|
|
1592
|
-
// Backbone.js 1.4.0
|
|
1593
|
-
|
|
1594
|
-
// History
|
|
1595
|
-
// -------
|
|
1596
|
-
|
|
1597
|
-
// Handles cross-browser history management, based on either
|
|
1598
|
-
// [pushState](http://diveintohtml5.info/history.html) and real URLs, or
|
|
1599
|
-
// [onhashchange](https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange)
|
|
1600
|
-
// and URL fragments. If the browser supports neither (old IE, natch),
|
|
1601
|
-
// falls back to polling.
|
|
1602
|
-
const History = function() {
|
|
1603
|
-
this.handlers = [];
|
|
1604
|
-
this.checkUrl = this.checkUrl.bind(this);
|
|
1605
|
-
|
|
1606
|
-
// Ensure that `History` can be used outside of the browser.
|
|
1607
|
-
if (typeof window !== 'undefined') {
|
|
1608
|
-
this.location = window.location;
|
|
1609
|
-
this.history = window.history;
|
|
1610
|
-
}
|
|
1611
|
-
};
|
|
1612
|
-
|
|
1613
|
-
History.extend = inherits;
|
|
1614
|
-
|
|
1615
|
-
// Cached regex for stripping a leading hash/slash and trailing space.
|
|
1616
|
-
const routeStripper = /^[#\/]|\s+$/g;
|
|
1617
|
-
// Cached regex for stripping leading and trailing slashes.
|
|
1618
|
-
const rootStripper = /^\/+|\/+$/g;
|
|
1619
|
-
// Cached regex for stripping urls of hash.
|
|
1620
|
-
const pathStripper = /#.*$/;
|
|
1621
|
-
|
|
1622
|
-
// Has the history handling already been started?
|
|
1623
|
-
History.started = false;
|
|
1624
|
-
|
|
1625
|
-
// Set up all inheritable **History** properties and methods.
|
|
1626
|
-
Object.assign(History.prototype, Events, {
|
|
1627
|
-
|
|
1628
|
-
// The default interval to poll for hash changes, if necessary, is
|
|
1629
|
-
// twenty times a second.
|
|
1630
|
-
interval: 50,
|
|
1631
|
-
|
|
1632
|
-
// Are we at the app root?
|
|
1633
|
-
atRoot: function() {
|
|
1634
|
-
const path = this.location.pathname.replace(/[^\/]$/, '$&/');
|
|
1635
|
-
return path === this.root && !this.getSearch();
|
|
1636
|
-
},
|
|
1637
|
-
|
|
1638
|
-
// Does the pathname match the root?
|
|
1639
|
-
matchRoot: function() {
|
|
1640
|
-
const path = this.decodeFragment(this.location.pathname);
|
|
1641
|
-
const rootPath = path.slice(0, this.root.length - 1) + '/';
|
|
1642
|
-
return rootPath === this.root;
|
|
1643
|
-
},
|
|
1644
|
-
|
|
1645
|
-
// Unicode characters in `location.pathname` are percent encoded so they're
|
|
1646
|
-
// decoded for comparison. `%25` should not be decoded since it may be part
|
|
1647
|
-
// of an encoded parameter.
|
|
1648
|
-
decodeFragment: function(fragment) {
|
|
1649
|
-
return decodeURI(fragment.replace(/%25/g, '%2525'));
|
|
1650
|
-
},
|
|
1651
|
-
|
|
1652
|
-
// In IE6, the hash fragment and search params are incorrect if the
|
|
1653
|
-
// fragment contains `?`.
|
|
1654
|
-
getSearch: function() {
|
|
1655
|
-
const match = this.location.href.replace(/#.*/, '').match(/\?.+/);
|
|
1656
|
-
return match ? match[0] : '';
|
|
1657
|
-
},
|
|
1658
|
-
|
|
1659
|
-
// Gets the true hash value. Cannot use location.hash directly due to bug
|
|
1660
|
-
// in Firefox where location.hash will always be decoded.
|
|
1661
|
-
getHash: function(window) {
|
|
1662
|
-
const match = (window || this).location.href.match(/#(.*)$/);
|
|
1663
|
-
return match ? match[1] : '';
|
|
1664
|
-
},
|
|
1665
|
-
|
|
1666
|
-
// Get the pathname and search params, without the root.
|
|
1667
|
-
getPath: function() {
|
|
1668
|
-
const path = this.decodeFragment(
|
|
1669
|
-
this.location.pathname + this.getSearch()
|
|
1670
|
-
).slice(this.root.length - 1);
|
|
1671
|
-
return path.charAt(0) === '/' ? path.slice(1) : path;
|
|
1672
|
-
},
|
|
1673
|
-
|
|
1674
|
-
// Get the cross-browser normalized URL fragment from the path or hash.
|
|
1675
|
-
getFragment: function(fragment) {
|
|
1676
|
-
if (fragment == null) {
|
|
1677
|
-
if (this._usePushState || !this._wantsHashChange) {
|
|
1678
|
-
fragment = this.getPath();
|
|
1679
|
-
} else {
|
|
1680
|
-
fragment = this.getHash();
|
|
1681
|
-
}
|
|
1682
|
-
}
|
|
1683
|
-
return fragment.replace(routeStripper, '');
|
|
1684
|
-
},
|
|
1685
|
-
|
|
1686
|
-
// Start the hash change handling, returning `true` if the current URL matches
|
|
1687
|
-
// an existing route, and `false` otherwise.
|
|
1688
|
-
start: function(options) {
|
|
1689
|
-
if (History.started) throw new Error('history has already been started');
|
|
1690
|
-
History.started = true;
|
|
1691
|
-
|
|
1692
|
-
// Figure out the initial configuration. Do we need an iframe?
|
|
1693
|
-
// Is pushState desired ... is it available?
|
|
1694
|
-
this.options = extend({root: '/'}, this.options, options);
|
|
1695
|
-
this.root = this.options.root;
|
|
1696
|
-
this._wantsHashChange = this.options.hashChange !== false;
|
|
1697
|
-
this._hasHashChange = 'onhashchange' in window && (document.documentMode === undefined|| document.documentMode > 7);
|
|
1698
|
-
this._useHashChange = this._wantsHashChange && this._hasHashChange;
|
|
1699
|
-
this._wantsPushState = !!this.options.pushState;
|
|
1700
|
-
this._hasPushState = !!(this.history && this.history.pushState);
|
|
1701
|
-
this._usePushState = this._wantsPushState && this._hasPushState;
|
|
1702
|
-
this.fragment = this.getFragment();
|
|
1703
|
-
|
|
1704
|
-
// Normalize root to always include a leading and trailing slash.
|
|
1705
|
-
this.root = ('/' + this.root + '/').replace(rootStripper, '/');
|
|
1706
|
-
|
|
1707
|
-
// Transition from hashChange to pushState or vice versa if both are
|
|
1708
|
-
// requested.
|
|
1709
|
-
if (this._wantsHashChange && this._wantsPushState) {
|
|
1710
|
-
|
|
1711
|
-
// If we've started off with a route from a `pushState`-enabled
|
|
1712
|
-
// browser, but we're currently in a browser that doesn't support it...
|
|
1713
|
-
if (!this._hasPushState && !this.atRoot()) {
|
|
1714
|
-
const rootPath = this.root.slice(0, -1) || '/';
|
|
1715
|
-
this.location.replace(rootPath + '#' + this.getPath());
|
|
1716
|
-
// Return immediately as browser will do redirect to new url
|
|
1717
|
-
return true;
|
|
1718
|
-
|
|
1719
|
-
// Or if we've started out with a hash-based route, but we're currently
|
|
1720
|
-
// in a browser where it could be `pushState`-based instead...
|
|
1721
|
-
} else if (this._hasPushState && this.atRoot()) {
|
|
1722
|
-
this.navigate(this.getHash(), {replace: true});
|
|
1723
|
-
}
|
|
1724
|
-
|
|
1725
|
-
}
|
|
1726
|
-
|
|
1727
|
-
// Proxy an iframe to handle location events if the browser doesn't
|
|
1728
|
-
// support the `hashchange` event, HTML5 history, or the user wants
|
|
1729
|
-
// `hashChange` but not `pushState`.
|
|
1730
|
-
if (!this._hasHashChange && this._wantsHashChange && !this._usePushState) {
|
|
1731
|
-
this.iframe = document.createElement('iframe');
|
|
1732
|
-
this.iframe.src = 'javascript:0';
|
|
1733
|
-
this.iframe.style.display = 'none';
|
|
1734
|
-
this.iframe.tabIndex = -1;
|
|
1735
|
-
const body = document.body;
|
|
1736
|
-
// Using `appendChild` will throw on IE < 9 if the document is not ready.
|
|
1737
|
-
const iWindow = body.insertBefore(this.iframe, body.firstChild).contentWindow;
|
|
1738
|
-
iWindow.document.open();
|
|
1739
|
-
iWindow.document.close();
|
|
1740
|
-
iWindow.location.hash = '#' + this.fragment;
|
|
1741
|
-
}
|
|
1742
|
-
|
|
1743
|
-
// Depending on whether we're using pushState or hashes, and whether
|
|
1744
|
-
// 'onhashchange' is supported, determine how we check the URL state.
|
|
1745
|
-
if (this._usePushState) {
|
|
1746
|
-
addEventListener('popstate', this.checkUrl, false);
|
|
1747
|
-
} else if (this._useHashChange && !this.iframe) {
|
|
1748
|
-
addEventListener('hashchange', this.checkUrl, false);
|
|
1749
|
-
} else if (this._wantsHashChange) {
|
|
1750
|
-
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
|
|
1751
|
-
}
|
|
1752
|
-
|
|
1753
|
-
if (!this.options.silent) return this.loadUrl();
|
|
1754
|
-
},
|
|
1755
|
-
|
|
1756
|
-
// Disable history, perhaps temporarily. Not useful in a real app,
|
|
1757
|
-
// but possibly useful for unit testing Routers.
|
|
1758
|
-
stop: function() {
|
|
1759
|
-
// Remove window listeners.
|
|
1760
|
-
if (this._usePushState) {
|
|
1761
|
-
removeEventListener('popstate', this.checkUrl, false);
|
|
1762
|
-
} else if (this._useHashChange && !this.iframe) {
|
|
1763
|
-
removeEventListener('hashchange', this.checkUrl, false);
|
|
1764
|
-
}
|
|
1765
|
-
|
|
1766
|
-
// Clean up the iframe if necessary.
|
|
1767
|
-
if (this.iframe) {
|
|
1768
|
-
document.body.removeChild(this.iframe);
|
|
1769
|
-
this.iframe = null;
|
|
1770
|
-
}
|
|
1771
|
-
|
|
1772
|
-
// Some environments will throw when clearing an undefined interval.
|
|
1773
|
-
if (this._checkUrlInterval) clearInterval(this._checkUrlInterval);
|
|
1774
|
-
History.started = false;
|
|
1775
|
-
},
|
|
1776
|
-
|
|
1777
|
-
// Add a route to be tested when the fragment changes. Routes added later
|
|
1778
|
-
// may override previous routes.
|
|
1779
|
-
route: function(route, callback) {
|
|
1780
|
-
this.handlers.unshift({route: route, callback: callback});
|
|
1781
|
-
},
|
|
1782
|
-
|
|
1783
|
-
// Checks the current URL to see if it has changed, and if it has,
|
|
1784
|
-
// calls `loadUrl`, normalizing across the hidden iframe.
|
|
1785
|
-
checkUrl: function(e) {
|
|
1786
|
-
let current = this.getFragment();
|
|
1787
|
-
|
|
1788
|
-
// If the user pressed the back button, the iframe's hash will have
|
|
1789
|
-
// changed and we should use that for comparison.
|
|
1790
|
-
if (current === this.fragment && this.iframe) {
|
|
1791
|
-
current = this.getHash(this.iframe.contentWindow);
|
|
1792
|
-
}
|
|
1793
|
-
|
|
1794
|
-
if (current === this.fragment) return false;
|
|
1795
|
-
if (this.iframe) this.navigate(current);
|
|
1796
|
-
this.loadUrl();
|
|
1797
|
-
},
|
|
1798
|
-
|
|
1799
|
-
// Attempt to load the current URL fragment. If a route succeeds with a
|
|
1800
|
-
// match, returns `true`. If no defined routes matches the fragment,
|
|
1801
|
-
// returns `false`.
|
|
1802
|
-
loadUrl: function(fragment) {
|
|
1803
|
-
// If the root doesn't match, no routes can match either.
|
|
1804
|
-
if (!this.matchRoot()) return false;
|
|
1805
|
-
fragment = this.fragment = this.getFragment(fragment);
|
|
1806
|
-
return some(this.handlers, function(handler) {
|
|
1807
|
-
if (handler.route.test(fragment)) {
|
|
1808
|
-
handler.callback(fragment);
|
|
1809
|
-
return true;
|
|
1810
|
-
}
|
|
1811
|
-
});
|
|
1812
|
-
},
|
|
1813
|
-
|
|
1814
|
-
// Save a fragment into the hash history, or replace the URL state if the
|
|
1815
|
-
// 'replace' option is passed. You are responsible for properly URL-encoding
|
|
1816
|
-
// the fragment in advance.
|
|
1817
|
-
//
|
|
1818
|
-
// The options object can contain `trigger: true` if you wish to have the
|
|
1819
|
-
// route callback be fired (not usually desirable), or `replace: true`, if
|
|
1820
|
-
// you wish to modify the current URL without adding an entry to the history.
|
|
1821
|
-
navigate: function(fragment, options) {
|
|
1822
|
-
if (!History.started) return false;
|
|
1823
|
-
if (!options || options === true) options = {trigger: !!options};
|
|
1824
|
-
|
|
1825
|
-
// Normalize the fragment.
|
|
1826
|
-
fragment = this.getFragment(fragment || '');
|
|
1827
|
-
|
|
1828
|
-
// Don't include a trailing slash on the root.
|
|
1829
|
-
let rootPath = this.root;
|
|
1830
|
-
if (fragment === '' || fragment.charAt(0) === '?') {
|
|
1831
|
-
rootPath = rootPath.slice(0, -1) || '/';
|
|
1832
|
-
}
|
|
1833
|
-
const url = rootPath + fragment;
|
|
1834
|
-
|
|
1835
|
-
// Strip the fragment of the query and hash for matching.
|
|
1836
|
-
fragment = fragment.replace(pathStripper, '');
|
|
1837
|
-
|
|
1838
|
-
// Decode for matching.
|
|
1839
|
-
const decodedFragment = this.decodeFragment(fragment);
|
|
1840
|
-
|
|
1841
|
-
if (this.fragment === decodedFragment) return;
|
|
1842
|
-
this.fragment = decodedFragment;
|
|
1843
|
-
|
|
1844
|
-
// If pushState is available, we use it to set the fragment as a real URL.
|
|
1845
|
-
if (this._usePushState) {
|
|
1846
|
-
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
|
|
1847
|
-
|
|
1848
|
-
// If hash changes haven't been explicitly disabled, update the hash
|
|
1849
|
-
// fragment to store history.
|
|
1850
|
-
} else if (this._wantsHashChange) {
|
|
1851
|
-
this._updateHash(this.location, fragment, options.replace);
|
|
1852
|
-
if (this.iframe && fragment !== this.getHash(this.iframe.contentWindow)) {
|
|
1853
|
-
const iWindow = this.iframe.contentWindow;
|
|
1854
|
-
|
|
1855
|
-
// Opening and closing the iframe tricks IE7 and earlier to push a
|
|
1856
|
-
// history entry on hash-tag change. When replace is true, we don't
|
|
1857
|
-
// want this.
|
|
1858
|
-
if (!options.replace) {
|
|
1859
|
-
iWindow.document.open();
|
|
1860
|
-
iWindow.document.close();
|
|
1861
|
-
}
|
|
1862
|
-
this._updateHash(iWindow.location, fragment, options.replace);
|
|
1863
|
-
}
|
|
1864
|
-
// If you've told us that you explicitly don't want fallback hashchange-
|
|
1865
|
-
// based history, then `navigate` becomes a page refresh.
|
|
1866
|
-
} else {
|
|
1867
|
-
return this.location.assign(url);
|
|
1868
|
-
}
|
|
1869
|
-
if (options.trigger) return this.loadUrl(fragment);
|
|
1870
|
-
},
|
|
1871
|
-
|
|
1872
|
-
// Update the hash location, either replacing the current entry, or adding
|
|
1873
|
-
// a new one to the browser history.
|
|
1874
|
-
_updateHash: function(location, fragment, replace) {
|
|
1875
|
-
if (replace) {
|
|
1876
|
-
const href = location.href.replace(/(javascript:|#).*$/, '');
|
|
1877
|
-
location.replace(href + '#' + fragment);
|
|
1878
|
-
} else {
|
|
1879
|
-
// Some browsers require that `hash` contains a leading #.
|
|
1880
|
-
location.hash = '#' + fragment;
|
|
1881
|
-
}
|
|
1882
|
-
}
|
|
1883
|
-
});
|
|
1884
|
-
|
|
1885
|
-
// Backbone.js 1.4.0
|
|
1886
|
-
|
|
1887
|
-
// Routers map faux-URLs to actions, and fire events when routes are
|
|
1888
|
-
// matched. Creating a new one sets its `routes` hash, if not set statically.
|
|
1889
|
-
const Router = function(options={}) {
|
|
1890
|
-
this.history = options.history || new History();
|
|
1891
|
-
this.preinitialize.apply(this, arguments);
|
|
1892
|
-
if (options.routes) this.routes = options.routes;
|
|
1893
|
-
this._bindRoutes();
|
|
1894
|
-
this.initialize.apply(this, arguments);
|
|
1895
|
-
};
|
|
1896
|
-
|
|
1897
|
-
Router.extend = inherits;
|
|
1898
|
-
|
|
1899
|
-
// Cached regular expressions for matching named param parts and splatted
|
|
1900
|
-
// parts of route strings.
|
|
1901
|
-
const optionalParam = /\((.*?)\)/g;
|
|
1902
|
-
const namedParam = /(\(\?)?:\w+/g;
|
|
1903
|
-
const splatParam = /\*\w+/g;
|
|
1904
|
-
const escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
|
|
1905
|
-
|
|
1906
|
-
// Set up all inheritable **Router** properties and methods.
|
|
1907
|
-
Object.assign(Router.prototype, Events, {
|
|
1908
|
-
|
|
1909
|
-
// preinitialize is an empty function by default. You can override it with a function
|
|
1910
|
-
// or object. preinitialize will run before any instantiation logic is run in the Router.
|
|
1911
|
-
preinitialize: function(){},
|
|
1912
|
-
|
|
1913
|
-
// Initialize is an empty function by default. Override it with your own
|
|
1914
|
-
// initialization logic.
|
|
1915
|
-
initialize: function(){},
|
|
1916
|
-
|
|
1917
|
-
// Manually bind a single named route to a callback. For example:
|
|
1918
|
-
//
|
|
1919
|
-
// this.route('search/:query/p:num', 'search', function(query, num) {
|
|
1920
|
-
// ...
|
|
1921
|
-
// });
|
|
1922
|
-
//
|
|
1923
|
-
route: function(route, name, callback) {
|
|
1924
|
-
if (!isRegExp(route)) route = this._routeToRegExp(route);
|
|
1925
|
-
if (isFunction(name)) {
|
|
1926
|
-
callback = name;
|
|
1927
|
-
name = '';
|
|
1928
|
-
}
|
|
1929
|
-
if (!callback) callback = this[name];
|
|
1930
|
-
this.history.route(route, (fragment) => {
|
|
1931
|
-
const args = this._extractParameters(route, fragment);
|
|
1932
|
-
if (this.execute(callback, args, name) !== false) {
|
|
1933
|
-
this.trigger.apply(this, ['route:' + name].concat(args));
|
|
1934
|
-
this.trigger('route', name, args);
|
|
1935
|
-
this.history.trigger('route', this, name, args);
|
|
1936
|
-
}
|
|
1937
|
-
});
|
|
1938
|
-
return this;
|
|
1939
|
-
},
|
|
1940
|
-
|
|
1941
|
-
// Execute a route handler with the provided parameters. This is an
|
|
1942
|
-
// excellent place to do pre-route setup or post-route cleanup.
|
|
1943
|
-
execute: function(callback, args, name) {
|
|
1944
|
-
if (callback) callback.apply(this, args);
|
|
1945
|
-
},
|
|
1946
|
-
|
|
1947
|
-
// Simple proxy to `history` to save a fragment into the history.
|
|
1948
|
-
navigate: function(fragment, options) {
|
|
1949
|
-
this.history.navigate(fragment, options);
|
|
1950
|
-
return this;
|
|
1951
|
-
},
|
|
1952
|
-
|
|
1953
|
-
// Bind all defined routes to `history`. We have to reverse the
|
|
1954
|
-
// order of the routes here to support behavior where the most general
|
|
1955
|
-
// routes can be defined at the bottom of the route map.
|
|
1956
|
-
_bindRoutes: function() {
|
|
1957
|
-
if (!this.routes) return;
|
|
1958
|
-
this.routes = result(this, 'routes');
|
|
1959
|
-
let route;
|
|
1960
|
-
const routes = Object.keys(this.routes);
|
|
1961
|
-
while ((route = routes.pop()) != null) {
|
|
1962
|
-
this.route(route, this.routes[route]);
|
|
1963
|
-
}
|
|
1964
|
-
},
|
|
1965
|
-
|
|
1966
|
-
// Convert a route string into a regular expression, suitable for matching
|
|
1967
|
-
// against the current location hash.
|
|
1968
|
-
_routeToRegExp: function(route) {
|
|
1969
|
-
route = route.replace(escapeRegExp, '\\$&')
|
|
1970
|
-
.replace(optionalParam, '(?:$1)?')
|
|
1971
|
-
.replace(namedParam, function(match, optional) {
|
|
1972
|
-
return optional ? match : '([^/?]+)';
|
|
1973
|
-
})
|
|
1974
|
-
.replace(splatParam, '([^?]*?)');
|
|
1975
|
-
return new RegExp('^' + route + '(?:\\?([\\s\\S]*))?$');
|
|
1976
|
-
},
|
|
1977
|
-
|
|
1978
|
-
// Given a route, and a URL fragment that it matches, return the array of
|
|
1979
|
-
// extracted decoded parameters. Empty or unmatched parameters will be
|
|
1980
|
-
// treated as `null` to normalize cross-browser behavior.
|
|
1981
|
-
_extractParameters: function(route, fragment) {
|
|
1982
|
-
const params = route.exec(fragment).slice(1);
|
|
1983
|
-
return params.map(function(param, i) {
|
|
1984
|
-
// Don't decode the search params.
|
|
1985
|
-
if (i === params.length - 1) return param || null;
|
|
1986
|
-
return param ? decodeURIComponent(param) : null;
|
|
1987
|
-
});
|
|
1988
|
-
}
|
|
1989
|
-
});
|
|
1990
|
-
|
|
1991
|
-
// Backbone.js 1.4.0
|
|
1992
|
-
|
|
1993
|
-
const paddedLt = /^\s*</;
|
|
1994
|
-
|
|
1995
|
-
// Caches a local reference to `Element.prototype` for faster access.
|
|
1996
|
-
const ElementProto = (typeof Element !== 'undefined' && Element.prototype) || {};
|
|
1997
|
-
|
|
1998
|
-
const indexOf = function(array, item) {
|
|
1999
|
-
for (let i = 0, len = array.length; i < len; i++) if (array[i] === item) return i;
|
|
2000
|
-
return -1;
|
|
2001
|
-
};
|
|
2002
|
-
|
|
2003
|
-
// Find the right `Element#matches` for IE>=9 and modern browsers.
|
|
2004
|
-
const matchesSelector = ElementProto.matches ||
|
|
2005
|
-
ElementProto.webkitMatchesSelector ||
|
|
2006
|
-
ElementProto.mozMatchesSelector ||
|
|
2007
|
-
ElementProto.msMatchesSelector ||
|
|
2008
|
-
ElementProto.oMatchesSelector ||
|
|
2009
|
-
// Make our own `Element#matches` for IE8
|
|
2010
|
-
function(selector) {
|
|
2011
|
-
// Use querySelectorAll to find all elements matching the selector,
|
|
2012
|
-
// then check if the given element is included in that list.
|
|
2013
|
-
// Executing the query on the parentNode reduces the resulting nodeList,
|
|
2014
|
-
// (document doesn't have a parentNode).
|
|
2015
|
-
const nodeList = (this.parentNode || document).querySelectorAll(selector) || [];
|
|
2016
|
-
return ~indexOf(nodeList, this);
|
|
2017
|
-
};
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
// Creating a View creates its initial element outside of the DOM,
|
|
2021
|
-
// if an existing element is not provided...
|
|
2022
|
-
const View = function(options) {
|
|
2023
|
-
this.cid = uniqueId('view');
|
|
2024
|
-
this._domEvents = [];
|
|
2025
|
-
this.preinitialize.apply(this, arguments);
|
|
2026
|
-
extend(this, pick(options, viewOptions));
|
|
2027
|
-
this._ensureElement();
|
|
2028
|
-
this.initialize.apply(this, arguments);
|
|
2029
|
-
};
|
|
2030
|
-
|
|
2031
|
-
View.extend = inherits;
|
|
2032
|
-
|
|
2033
|
-
// Cached regex to split keys for `delegate`.
|
|
2034
|
-
const delegateEventSplitter = /^(\S+)\s*(.*)$/;
|
|
2035
|
-
|
|
2036
|
-
// List of view options to be set as properties.
|
|
2037
|
-
const viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events'];
|
|
2038
|
-
|
|
2039
|
-
// Set up all inheritable **View** properties and methods.
|
|
2040
|
-
Object.assign(View.prototype, Events, {
|
|
2041
|
-
|
|
2042
|
-
// The default `tagName` of a View's element is `"div"`.
|
|
2043
|
-
tagName: 'div',
|
|
2044
|
-
|
|
2045
|
-
$: function(selector) {
|
|
2046
|
-
return this.el.querySelectorAll(selector);
|
|
2047
|
-
},
|
|
2048
|
-
|
|
2049
|
-
// preinitialize is an empty function by default. You can override it with a function
|
|
2050
|
-
// or object. preinitialize will run before any instantiation logic is run in the View
|
|
2051
|
-
preinitialize: function(){},
|
|
2052
|
-
|
|
2053
|
-
// Initialize is an empty function by default. Override it with your own
|
|
2054
|
-
// initialization logic.
|
|
2055
|
-
initialize: function(){},
|
|
2056
|
-
|
|
2057
|
-
// **render** is the core function that your view should override, in order
|
|
2058
|
-
// to populate its element (`this.el`), with the appropriate HTML. The
|
|
2059
|
-
// convention is for **render** to always return `this`.
|
|
2060
|
-
render: function() {
|
|
2061
|
-
isFunction(this.beforeRender) && this.beforeRender();
|
|
2062
|
-
isFunction(this.toHTML) && render(this.toHTML(), this.el);
|
|
2063
|
-
isFunction(this.afterRender) && this.afterRender();
|
|
2064
|
-
return this;
|
|
2065
|
-
},
|
|
2066
|
-
|
|
2067
|
-
// Remove this view by taking the element out of the DOM, and removing any
|
|
2068
|
-
// applicable Backbone.Events listeners.
|
|
2069
|
-
remove: function() {
|
|
2070
|
-
this._removeElement();
|
|
2071
|
-
this.stopListening();
|
|
2072
|
-
return this;
|
|
2073
|
-
},
|
|
2074
|
-
|
|
2075
|
-
// Remove this view's element from the document and all event listeners
|
|
2076
|
-
// attached to it. Exposed for subclasses using an alternative DOM
|
|
2077
|
-
// manipulation API.
|
|
2078
|
-
_removeElement: function() {
|
|
2079
|
-
this.undelegateEvents();
|
|
2080
|
-
if (this.el.parentNode) this.el.parentNode.removeChild(this.el);
|
|
2081
|
-
},
|
|
2082
|
-
|
|
2083
|
-
// Change the view's element (`this.el` property) and re-delegate the
|
|
2084
|
-
// view's events on the new element.
|
|
2085
|
-
setElement: function(element) {
|
|
2086
|
-
this.undelegateEvents();
|
|
2087
|
-
this._setElement(element);
|
|
2088
|
-
this.delegateEvents();
|
|
2089
|
-
return this;
|
|
2090
|
-
},
|
|
2091
|
-
|
|
2092
|
-
// Apply the `element` to the view. `element` can be a CSS selector,
|
|
2093
|
-
// a string of HTML, or an Element node. If passed a NodeList or CSS
|
|
2094
|
-
// selector, uses just the first match.
|
|
2095
|
-
_setElement: function(element) {
|
|
2096
|
-
if (typeof element == 'string') {
|
|
2097
|
-
if (paddedLt.test(element)) {
|
|
2098
|
-
const el = document.createElement('div');
|
|
2099
|
-
el.innerHTML = element;
|
|
2100
|
-
this.el = el.firstChild;
|
|
2101
|
-
} else {
|
|
2102
|
-
this.el = document.querySelector(element);
|
|
2103
|
-
}
|
|
2104
|
-
} else if (element && !isElement(element) && element.length) {
|
|
2105
|
-
this.el = element[0];
|
|
2106
|
-
} else {
|
|
2107
|
-
this.el = element;
|
|
2108
|
-
}
|
|
2109
|
-
},
|
|
2110
|
-
|
|
2111
|
-
// Set callbacks, where `this.events` is a hash of
|
|
2112
|
-
//
|
|
2113
|
-
// *{"event selector": "callback"}*
|
|
2114
|
-
//
|
|
2115
|
-
// {
|
|
2116
|
-
// 'mousedown .title': 'edit',
|
|
2117
|
-
// 'click .button': 'save',
|
|
2118
|
-
// 'click .open': function(e) { ... }
|
|
2119
|
-
// }
|
|
2120
|
-
//
|
|
2121
|
-
// pairs. Callbacks will be bound to the view, with `this` set properly.
|
|
2122
|
-
// Uses event delegation for efficiency.
|
|
2123
|
-
// Omitting the selector binds the event to `this.el`.
|
|
2124
|
-
delegateEvents: function(events) {
|
|
2125
|
-
events || (events = result(this, 'events'));
|
|
2126
|
-
if (!events) return this;
|
|
2127
|
-
this.undelegateEvents();
|
|
2128
|
-
for (const key in events) {
|
|
2129
|
-
let method = events[key];
|
|
2130
|
-
if (!isFunction(method)) method = this[method];
|
|
2131
|
-
if (!method) continue;
|
|
2132
|
-
const match = key.match(delegateEventSplitter);
|
|
2133
|
-
this.delegate(match[1], match[2], method.bind(this));
|
|
2134
|
-
}
|
|
2135
|
-
return this;
|
|
2136
|
-
},
|
|
2137
|
-
|
|
2138
|
-
// Make a event delegation handler for the given `eventName` and `selector`
|
|
2139
|
-
// and attach it to `this.el`.
|
|
2140
|
-
// If selector is empty, the listener will be bound to `this.el`. If not, a
|
|
2141
|
-
// new handler that will recursively traverse up the event target's DOM
|
|
2142
|
-
// hierarchy looking for a node that matches the selector. If one is found,
|
|
2143
|
-
// the event's `delegateTarget` property is set to it and the return the
|
|
2144
|
-
// result of calling bound `listener` with the parameters given to the
|
|
2145
|
-
// handler.
|
|
2146
|
-
delegate: function(eventName, selector, listener) {
|
|
2147
|
-
const root = this.el;
|
|
2148
|
-
if (!root) {
|
|
2149
|
-
return this;
|
|
2150
|
-
}
|
|
2151
|
-
if (typeof selector === 'function') {
|
|
2152
|
-
listener = selector;
|
|
2153
|
-
selector = null;
|
|
2154
|
-
}
|
|
2155
|
-
// Given that `focus` and `blur` events do not bubble, do not delegate these events
|
|
2156
|
-
if (['focus', 'blur'].indexOf(eventName) !== -1) {
|
|
2157
|
-
const els = this.el.querySelectorAll(selector);
|
|
2158
|
-
for (let i = 0, len = els.length; i < len; i++) {
|
|
2159
|
-
const item = els[i];
|
|
2160
|
-
item.addEventListener(eventName, listener, false);
|
|
2161
|
-
this._domEvents.push({el: item, eventName: eventName, handler: listener});
|
|
2162
|
-
}
|
|
2163
|
-
return listener;
|
|
2164
|
-
}
|
|
2165
|
-
|
|
2166
|
-
const handler = selector ? function (e) {
|
|
2167
|
-
let node = e.target || e.srcElement;
|
|
2168
|
-
for (; node && node != root; node = node.parentNode) {
|
|
2169
|
-
if (matchesSelector.call(node, selector)) {
|
|
2170
|
-
e.delegateTarget = node;
|
|
2171
|
-
listener(e);
|
|
2172
|
-
}
|
|
2173
|
-
}
|
|
2174
|
-
} : listener;
|
|
2175
|
-
|
|
2176
|
-
this.el.addEventListener(eventName, handler, false);
|
|
2177
|
-
this._domEvents.push({el: this.el, eventName: eventName, handler: handler, listener: listener, selector: selector});
|
|
2178
|
-
return this;
|
|
2179
|
-
},
|
|
2180
|
-
|
|
2181
|
-
// Clears all callbacks previously bound to the view by `delegateEvents`.
|
|
2182
|
-
// You usually don't need to use this, but may wish to if you have multiple
|
|
2183
|
-
// Backbone views attached to the same DOM element.
|
|
2184
|
-
undelegateEvents: function() {
|
|
2185
|
-
if (this.el) {
|
|
2186
|
-
for (let i = 0, len = this._domEvents.length; i < len; i++) {
|
|
2187
|
-
const item = this._domEvents[i];
|
|
2188
|
-
item.el.removeEventListener(item.eventName, item.handler, false);
|
|
2189
|
-
}
|
|
2190
|
-
this._domEvents.length = 0;
|
|
2191
|
-
}
|
|
2192
|
-
return this;
|
|
2193
|
-
},
|
|
2194
|
-
|
|
2195
|
-
// A finer-grained `undelegateEvents` for removing a single delegated event.
|
|
2196
|
-
// `selector` and `listener` are both optional.
|
|
2197
|
-
undelegate: function(eventName, selector, listener) {
|
|
2198
|
-
if (typeof selector === 'function') {
|
|
2199
|
-
listener = selector;
|
|
2200
|
-
selector = null;
|
|
2201
|
-
}
|
|
2202
|
-
if (this.el) {
|
|
2203
|
-
const handlers = this._domEvents.slice();
|
|
2204
|
-
let i = handlers.length;
|
|
2205
|
-
while (i--) {
|
|
2206
|
-
const item = handlers[i];
|
|
2207
|
-
const match = item.eventName === eventName &&
|
|
2208
|
-
(listener ? item.listener === listener : true) &&
|
|
2209
|
-
(selector ? item.selector === selector : true);
|
|
2210
|
-
|
|
2211
|
-
if (!match) {
|
|
2212
|
-
continue;
|
|
2213
|
-
}
|
|
2214
|
-
item.el.removeEventListener(item.eventName, item.handler, false);
|
|
2215
|
-
this._domEvents.splice(i, 1);
|
|
2216
|
-
}
|
|
2217
|
-
}
|
|
2218
|
-
return this;
|
|
2219
|
-
},
|
|
2220
|
-
|
|
2221
|
-
// Produces a DOM element to be assigned to your view. Exposed for
|
|
2222
|
-
// subclasses using an alternative DOM manipulation API.
|
|
2223
|
-
_createElement: function(tagName) {
|
|
2224
|
-
return document.createElement(tagName);
|
|
2225
|
-
},
|
|
2226
|
-
|
|
2227
|
-
// Ensure that the View has a DOM element to render into.
|
|
2228
|
-
// If `this.el` is a string, pass it through `$()`, take the first
|
|
2229
|
-
// matching element, and re-assign it to `el`. Otherwise, create
|
|
2230
|
-
// an element from the `id`, `className` and `tagName` properties.
|
|
2231
|
-
_ensureElement: function() {
|
|
2232
|
-
if (!this.el) {
|
|
2233
|
-
const attrs = extend({}, result(this, 'attributes'));
|
|
2234
|
-
if (this.id) attrs.id = result(this, 'id');
|
|
2235
|
-
if (this.className) attrs['class'] = result(this, 'className');
|
|
2236
|
-
this.setElement(this._createElement(result(this, 'tagName')));
|
|
2237
|
-
this._setAttributes(attrs);
|
|
2238
|
-
} else {
|
|
2239
|
-
this.setElement(result(this, 'el'));
|
|
2240
|
-
}
|
|
2241
|
-
},
|
|
2242
|
-
|
|
2243
|
-
// Set attributes from a hash on this view's element. Exposed for
|
|
2244
|
-
// subclasses using an alternative DOM manipulation API.
|
|
2245
|
-
_setAttributes: function(attrs) {
|
|
2246
|
-
for (const attr in attrs) {
|
|
2247
|
-
attr in this.el ? this.el[attr] = attrs[attr] : this.el.setAttribute(attr, attrs[attr]);
|
|
2248
|
-
}
|
|
2249
|
-
},
|
|
2250
|
-
});
|
|
2251
|
-
|
|
2252
|
-
/* global global */
|
|
2253
|
-
|
|
2254
|
-
const skeletor = {
|
|
2255
|
-
Collection,
|
|
2256
|
-
Events,
|
|
2257
|
-
History,
|
|
2258
|
-
Model,
|
|
2259
|
-
Router,
|
|
2260
|
-
View,
|
|
2261
|
-
ajax,
|
|
2262
|
-
sync
|
|
2263
|
-
};
|
|
2264
|
-
|
|
2265
|
-
Object.assign(skeletor, Events);
|
|
2266
|
-
|
|
2267
|
-
// Establish the root object, `window` (`self`) in the browser, or `global` on the server.
|
|
2268
|
-
// We use `self` instead of `window` for `WebWorker` support.
|
|
2269
|
-
const root = typeof self == 'object' && self.self === self && self ||
|
|
2270
|
-
typeof global == 'object' && global.global === global && global;
|
|
2271
|
-
|
|
2272
|
-
// Current version of the library. Keep in sync with `package.json`.
|
|
2273
|
-
skeletor.VERSION = '0.0.1';
|
|
2274
|
-
|
|
2275
|
-
// Save the previous value of the `Skeletor` variable, so that it can be
|
|
2276
|
-
// restored later on, if `noConflict` is used.
|
|
2277
|
-
const previousSkeletor = root.Skeletor;
|
|
2278
|
-
|
|
2279
|
-
// Runs Skeletor.js in *noConflict* mode, returning the `Skeletor` variable
|
|
2280
|
-
// to its previous owner. Returns a reference to this Skeletor object.
|
|
2281
|
-
skeletor.noConflict = function() {
|
|
2282
|
-
root.Skeletor = previousSkeletor;
|
|
2283
|
-
return this;
|
|
2284
|
-
};
|
|
2285
|
-
|
|
2286
|
-
root.Skeletor = skeletor;
|
|
2287
|
-
|
|
2288
|
-
return skeletor;
|
|
2289
|
-
|
|
2290
|
-
})));
|
|
2291
|
-
//# sourceMappingURL=skeletor.js.map
|