mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Update vendors.
Former-commit-id: baf89d2c3bd7077462995bffa7f8bff1e1cf28f9
This commit is contained in:
409
vendor/backbone/backbone.js
vendored
409
vendor/backbone/backbone.js
vendored
@@ -160,7 +160,8 @@
|
||||
if (callback || context) {
|
||||
for (j = 0, k = list.length; j < k; j++) {
|
||||
ev = list[j];
|
||||
if ((callback && callback !== (ev.callback._callback || ev.callback)) ||
|
||||
if ((callback && callback !== ev.callback &&
|
||||
callback !== ev.callback._callback) ||
|
||||
(context && context !== ev.context)) {
|
||||
events.push(ev);
|
||||
}
|
||||
@@ -190,27 +191,25 @@
|
||||
|
||||
// An inversion-of-control version of `on`. Tell *this* object to listen to
|
||||
// an event in another object ... keeping track of what it's listening to.
|
||||
listenTo: function(object, events, callback, context) {
|
||||
context = context || this;
|
||||
listenTo: function(object, events, callback) {
|
||||
var listeners = this._listeners || (this._listeners = {});
|
||||
var id = object._listenerId || (object._listenerId = _.uniqueId('l'));
|
||||
listeners[id] = object;
|
||||
object.on(events, callback || context, context);
|
||||
object.on(events, callback || this, this);
|
||||
return this;
|
||||
},
|
||||
|
||||
// Tell this object to stop listening to either specific events ... or
|
||||
// to every object it's currently listening to.
|
||||
stopListening: function(object, events, callback, context) {
|
||||
context = context || this;
|
||||
stopListening: function(object, events, callback) {
|
||||
var listeners = this._listeners;
|
||||
if (!listeners) return;
|
||||
if (object) {
|
||||
object.off(events, callback, context);
|
||||
object.off(events, callback, this);
|
||||
if (!events && !callback) delete listeners[object._listenerId];
|
||||
} else {
|
||||
for (var id in listeners) {
|
||||
listeners[id].off(null, null, context);
|
||||
listeners[id].off(null, null, this);
|
||||
}
|
||||
this._listeners = {};
|
||||
}
|
||||
@@ -235,15 +234,14 @@
|
||||
var defaults;
|
||||
var attrs = attributes || {};
|
||||
this.cid = _.uniqueId('c');
|
||||
this.changed = {};
|
||||
this.attributes = {};
|
||||
this._changes = [];
|
||||
if (options && options.collection) this.collection = options.collection;
|
||||
if (options && options.parse) attrs = this.parse(attrs, options);
|
||||
if (defaults = _.result(this, 'defaults')) _.defaults(attrs, defaults);
|
||||
this.set(attrs, {silent: true});
|
||||
this._currentAttributes = _.clone(this.attributes);
|
||||
this._previousAttributes = _.clone(this.attributes);
|
||||
if (options && options.parse) attrs = this.parse(attrs, options) || {};
|
||||
if (defaults = _.result(this, 'defaults')) {
|
||||
attrs = _.defaults({}, attrs, defaults);
|
||||
}
|
||||
this.set(attrs, options);
|
||||
this.changed = {};
|
||||
this.initialize.apply(this, arguments);
|
||||
};
|
||||
|
||||
@@ -287,47 +285,72 @@
|
||||
return this.get(attr) != null;
|
||||
},
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
// Set a hash of model attributes on the object, firing `"change"` unless
|
||||
// you choose to silence it.
|
||||
set: function(key, val, options) {
|
||||
var attr, attrs;
|
||||
var attr, attrs, unset, changes, silent, changing, prev, current;
|
||||
if (key == null) return this;
|
||||
|
||||
// Handle both `"key", value` and `{key: value}` -style arguments.
|
||||
if (_.isObject(key)) {
|
||||
if (typeof key === 'object') {
|
||||
attrs = key;
|
||||
options = val;
|
||||
} else {
|
||||
(attrs = {})[key] = val;
|
||||
}
|
||||
|
||||
// Extract attributes and options.
|
||||
var silent = options && options.silent;
|
||||
var unset = options && options.unset;
|
||||
options || (options = {});
|
||||
|
||||
// Run validation.
|
||||
if (!this._validate(attrs, options)) return false;
|
||||
|
||||
// Extract attributes and options.
|
||||
unset = options.unset;
|
||||
silent = options.silent;
|
||||
changes = [];
|
||||
changing = this._changing;
|
||||
this._changing = true;
|
||||
|
||||
if (!changing) {
|
||||
this._previousAttributes = _.clone(this.attributes);
|
||||
this.changed = {};
|
||||
}
|
||||
current = this.attributes, prev = this._previousAttributes;
|
||||
|
||||
// Check for changes of `id`.
|
||||
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
|
||||
|
||||
var now = this.attributes;
|
||||
|
||||
// For each `set` attribute...
|
||||
// For each `set` attribute, update or delete the current value.
|
||||
for (attr in attrs) {
|
||||
val = attrs[attr];
|
||||
|
||||
// Update or delete the current value, and track the change.
|
||||
unset ? delete now[attr] : now[attr] = val;
|
||||
this._changes.push(attr, val);
|
||||
if (!_.isEqual(current[attr], val)) changes.push(attr);
|
||||
if (!_.isEqual(prev[attr], val)) {
|
||||
this.changed[attr] = val;
|
||||
} else {
|
||||
delete this.changed[attr];
|
||||
}
|
||||
unset ? delete current[attr] : current[attr] = val;
|
||||
}
|
||||
|
||||
// Signal that the model's state has potentially changed, and we need
|
||||
// to recompute the actual changes.
|
||||
this._hasComputed = false;
|
||||
// Trigger all relevant attribute changes.
|
||||
if (!silent) {
|
||||
if (changes.length) this._pending = true;
|
||||
for (var i = 0, l = changes.length; i < l; i++) {
|
||||
this.trigger('change:' + changes[i], this, current[changes[i]], options);
|
||||
}
|
||||
}
|
||||
|
||||
// Fire the `"change"` events.
|
||||
if (!silent) this.change(options);
|
||||
if (changing) return this;
|
||||
if (!silent) {
|
||||
while (this._pending) {
|
||||
this._pending = false;
|
||||
this.trigger('change', this, options);
|
||||
}
|
||||
}
|
||||
this._pending = false;
|
||||
this._changing = false;
|
||||
return this;
|
||||
},
|
||||
|
||||
@@ -345,15 +368,53 @@
|
||||
return this.set(attrs, _.extend({}, options, {unset: true}));
|
||||
},
|
||||
|
||||
// Determine if the model has changed since the last `"change"` event.
|
||||
// If you specify an attribute name, determine if that attribute has changed.
|
||||
hasChanged: function(attr) {
|
||||
if (attr == null) return !_.isEmpty(this.changed);
|
||||
return _.has(this.changed, attr);
|
||||
},
|
||||
|
||||
// Return an object containing all the attributes that have changed, or
|
||||
// false if there are no changed attributes. Useful for determining what
|
||||
// parts of a view need to be updated and/or what attributes need to be
|
||||
// persisted to the server. Unset attributes will be set to undefined.
|
||||
// You can also pass an attributes object to diff against the model,
|
||||
// determining if there *would be* a change.
|
||||
changedAttributes: function(diff) {
|
||||
if (!diff) return this.hasChanged() ? _.clone(this.changed) : false;
|
||||
var val, changed = false;
|
||||
var old = this._changing ? this._previousAttributes : this.attributes;
|
||||
for (var attr in diff) {
|
||||
if (_.isEqual(old[attr], (val = diff[attr]))) continue;
|
||||
(changed || (changed = {}))[attr] = val;
|
||||
}
|
||||
return changed;
|
||||
},
|
||||
|
||||
// Get the previous value of an attribute, recorded at the time the last
|
||||
// `"change"` event was fired.
|
||||
previous: function(attr) {
|
||||
if (attr == null || !this._previousAttributes) return null;
|
||||
return this._previousAttributes[attr];
|
||||
},
|
||||
|
||||
// Get all of the attributes of the model at the time of the previous
|
||||
// `"change"` event.
|
||||
previousAttributes: function() {
|
||||
return _.clone(this._previousAttributes);
|
||||
},
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// Fetch the model from the server. If the server's representation of the
|
||||
// model differs from its current attributes, they will be overriden,
|
||||
// triggering a `"change"` event.
|
||||
fetch: function(options) {
|
||||
options = options ? _.clone(options) : {};
|
||||
if (options.parse === void 0) options.parse = true;
|
||||
var model = this;
|
||||
var success = options.success;
|
||||
options.success = function(resp, status, xhr) {
|
||||
options.success = function(model, resp, options) {
|
||||
if (!model.set(model.parse(resp, options), options)) return false;
|
||||
if (success) success(model, resp, options);
|
||||
};
|
||||
@@ -364,55 +425,50 @@
|
||||
// If the server returns an attributes hash that differs, the model's
|
||||
// state will be `set` again.
|
||||
save: function(key, val, options) {
|
||||
var attrs, current, done;
|
||||
var attrs, model, success, method, xhr, attributes = this.attributes;
|
||||
|
||||
// Handle both `"key", value` and `{key: value}` -style arguments.
|
||||
if (key == null || _.isObject(key)) {
|
||||
if (key == null || typeof key === 'object') {
|
||||
attrs = key;
|
||||
options = val;
|
||||
} else if (key != null) {
|
||||
} else {
|
||||
(attrs = {})[key] = val;
|
||||
}
|
||||
options = options ? _.clone(options) : {};
|
||||
|
||||
// If we're "wait"-ing to set changed attributes, validate early.
|
||||
if (options.wait) {
|
||||
if (attrs && !this._validate(attrs, options)) return false;
|
||||
current = _.clone(this.attributes);
|
||||
}
|
||||
// If we're not waiting and attributes exist, save acts as `set(attr).save(null, opts)`.
|
||||
if (attrs && (!options || !options.wait) && !this.set(attrs, options)) return false;
|
||||
|
||||
// Regular saves `set` attributes before persisting to the server.
|
||||
var silentOptions = _.extend({}, options, {silent: true});
|
||||
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
|
||||
return false;
|
||||
}
|
||||
options = _.extend({validate: true}, options);
|
||||
|
||||
// Do not persist invalid models.
|
||||
if (!attrs && !this._validate(null, options)) return false;
|
||||
if (!this._validate(attrs, options)) return false;
|
||||
|
||||
// Set temporary attributes if `{wait: true}`.
|
||||
if (attrs && options.wait) {
|
||||
this.attributes = _.extend({}, attributes, attrs);
|
||||
}
|
||||
|
||||
// After a successful server-side save, the client is (optionally)
|
||||
// updated with the server-side state.
|
||||
var model = this;
|
||||
var success = options.success;
|
||||
options.success = function(resp, status, xhr) {
|
||||
done = true;
|
||||
success = options.success;
|
||||
options.success = function(model, resp, options) {
|
||||
// Ensure attributes are restored during synchronous saves.
|
||||
model.attributes = attributes;
|
||||
var serverAttrs = model.parse(resp, options);
|
||||
if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
|
||||
if (!model.set(serverAttrs, options)) return false;
|
||||
if (_.isObject(serverAttrs) && !model.set(serverAttrs, options)) {
|
||||
return false;
|
||||
}
|
||||
if (success) success(model, resp, options);
|
||||
};
|
||||
|
||||
// Finish configuring and sending the Ajax request.
|
||||
var method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update');
|
||||
method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update');
|
||||
if (method == 'patch') options.attrs = attrs;
|
||||
var xhr = this.sync(method, this, options);
|
||||
xhr = this.sync(method, this, options);
|
||||
|
||||
// When using `wait`, reset attributes to original values unless
|
||||
// `success` has been called already.
|
||||
if (!done && options.wait) {
|
||||
this.clear(silentOptions);
|
||||
this.set(current, silentOptions);
|
||||
}
|
||||
// Restore attributes.
|
||||
if (attrs && options.wait) this.attributes = attributes;
|
||||
|
||||
return xhr;
|
||||
},
|
||||
@@ -429,13 +485,13 @@
|
||||
model.trigger('destroy', model, model.collection, options);
|
||||
};
|
||||
|
||||
options.success = function(resp) {
|
||||
options.success = function(model, resp, options) {
|
||||
if (options.wait || model.isNew()) destroy();
|
||||
if (success) success(model, resp, options);
|
||||
};
|
||||
|
||||
if (this.isNew()) {
|
||||
options.success();
|
||||
options.success(this, null, options);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -469,115 +525,20 @@
|
||||
return this.id == null;
|
||||
},
|
||||
|
||||
// Call this method to manually fire a `"change"` event for this model and
|
||||
// a `"change:attribute"` event for each changed attribute.
|
||||
// Calling this will cause all objects observing the model to update.
|
||||
change: function(options) {
|
||||
var changing = this._changing;
|
||||
this._changing = true;
|
||||
|
||||
// Generate the changes to be triggered on the model.
|
||||
var triggers = this._computeChanges(true);
|
||||
|
||||
this._pending = !!triggers.length;
|
||||
|
||||
for (var i = triggers.length - 2; i >= 0; i -= 2) {
|
||||
this.trigger('change:' + triggers[i], this, triggers[i + 1], options);
|
||||
}
|
||||
|
||||
if (changing) return this;
|
||||
|
||||
// Trigger a `change` while there have been changes.
|
||||
while (this._pending) {
|
||||
this._pending = false;
|
||||
this.trigger('change', this, options);
|
||||
this._previousAttributes = _.clone(this.attributes);
|
||||
}
|
||||
|
||||
this._changing = false;
|
||||
return this;
|
||||
},
|
||||
|
||||
// Determine if the model has changed since the last `"change"` event.
|
||||
// If you specify an attribute name, determine if that attribute has changed.
|
||||
hasChanged: function(attr) {
|
||||
if (!this._hasComputed) this._computeChanges();
|
||||
if (attr == null) return !_.isEmpty(this.changed);
|
||||
return _.has(this.changed, attr);
|
||||
},
|
||||
|
||||
// Return an object containing all the attributes that have changed, or
|
||||
// false if there are no changed attributes. Useful for determining what
|
||||
// parts of a view need to be updated and/or what attributes need to be
|
||||
// persisted to the server. Unset attributes will be set to undefined.
|
||||
// You can also pass an attributes object to diff against the model,
|
||||
// determining if there *would be* a change.
|
||||
changedAttributes: function(diff) {
|
||||
if (!diff) return this.hasChanged() ? _.clone(this.changed) : false;
|
||||
var val, changed = false, old = this._previousAttributes;
|
||||
for (var attr in diff) {
|
||||
if (_.isEqual(old[attr], (val = diff[attr]))) continue;
|
||||
(changed || (changed = {}))[attr] = val;
|
||||
}
|
||||
return changed;
|
||||
},
|
||||
|
||||
// Looking at the built up list of `set` attribute changes, compute how
|
||||
// many of the attributes have actually changed. If `loud`, return a
|
||||
// boiled-down list of only the real changes.
|
||||
_computeChanges: function(loud) {
|
||||
this.changed = {};
|
||||
var already = {};
|
||||
var triggers = [];
|
||||
var current = this._currentAttributes;
|
||||
var changes = this._changes;
|
||||
|
||||
// Loop through the current queue of potential model changes.
|
||||
for (var i = changes.length - 2; i >= 0; i -= 2) {
|
||||
var key = changes[i], val = changes[i + 1];
|
||||
if (already[key]) continue;
|
||||
already[key] = true;
|
||||
|
||||
// Check if the attribute has been modified since the last change,
|
||||
// and update `this.changed` accordingly. If we're inside of a `change`
|
||||
// call, also add a trigger to the list.
|
||||
if (!_.isEqual(current[key], val)) {
|
||||
this.changed[key] = val;
|
||||
if (!loud) continue;
|
||||
triggers.push(key, val);
|
||||
current[key] = val;
|
||||
}
|
||||
}
|
||||
if (loud) this._changes = [];
|
||||
|
||||
// Signals `this.changed` is current to prevent duplicate calls from `this.hasChanged`.
|
||||
this._hasComputed = true;
|
||||
return triggers;
|
||||
},
|
||||
|
||||
// Get the previous value of an attribute, recorded at the time the last
|
||||
// `"change"` event was fired.
|
||||
previous: function(attr) {
|
||||
if (attr == null || !this._previousAttributes) return null;
|
||||
return this._previousAttributes[attr];
|
||||
},
|
||||
|
||||
// Get all of the attributes of the model at the time of the previous
|
||||
// `"change"` event.
|
||||
previousAttributes: function() {
|
||||
return _.clone(this._previousAttributes);
|
||||
// Check if the model is currently in a valid state.
|
||||
isValid: function(options) {
|
||||
return !this.validate || !this.validate(this.attributes, options);
|
||||
},
|
||||
|
||||
// Run validation against the next complete set of model attributes,
|
||||
// returning `true` if all is well. Otherwise, fire a general
|
||||
// `"error"` event and call the error callback, if specified.
|
||||
_validate: function(attrs, options) {
|
||||
if (!this.validate) return true;
|
||||
if (!options.validate || !this.validate) return true;
|
||||
attrs = _.extend({}, this.attributes, attrs);
|
||||
var error = this.validate(attrs, options);
|
||||
var error = this.validationError = this.validate(attrs, options) || null;
|
||||
if (!error) return true;
|
||||
if (options && options.error) options.error(this, error, options);
|
||||
this.trigger('error', this, error, options);
|
||||
this.trigger('invalid', this, error, options || {});
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -620,20 +581,22 @@
|
||||
return Backbone.sync.apply(this, arguments);
|
||||
},
|
||||
|
||||
// Add a model, or list of models to the set. Pass **silent** to avoid
|
||||
// firing the `add` event for every new model.
|
||||
// Add a model, or list of models to the set.
|
||||
add: function(models, options) {
|
||||
var i, args, length, model, existing, needsSort;
|
||||
var at = options && options.at;
|
||||
var sort = ((options && options.sort) == null ? true : options.sort);
|
||||
models = _.isArray(models) ? models.slice() : [models];
|
||||
options || (options = {});
|
||||
var i, l, model, attrs, existing, sort, doSort, sortAttr, at, add;
|
||||
add = [];
|
||||
at = options.at;
|
||||
sort = this.comparator && (at == null) && (options.sort == null || options.sort);
|
||||
sortAttr = _.isString(this.comparator) ? this.comparator : null;
|
||||
|
||||
// Turn bare objects into model references, and prevent invalid models
|
||||
// from being added.
|
||||
for (i = models.length - 1; i >= 0; i--) {
|
||||
if(!(model = this._prepareModel(models[i], options))) {
|
||||
this.trigger("error", this, models[i], options);
|
||||
models.splice(i, 1);
|
||||
for (i = 0, l = models.length; i < l; i++) {
|
||||
attrs = models[i];
|
||||
if(!(model = this._prepareModel(attrs, options))) {
|
||||
this.trigger('invalid', this, attrs, options);
|
||||
continue;
|
||||
}
|
||||
models[i] = model;
|
||||
@@ -641,14 +604,16 @@
|
||||
// If a duplicate is found, prevent it from being added and
|
||||
// optionally merge it into the existing model.
|
||||
if (existing = this.get(model)) {
|
||||
if (options && options.merge) {
|
||||
existing.set(model.attributes, options);
|
||||
needsSort = sort;
|
||||
if (options.merge) {
|
||||
existing.set(attrs === model ? model.attributes : attrs, options);
|
||||
if (sort && !doSort && existing.hasChanged(sortAttr)) doSort = true;
|
||||
}
|
||||
models.splice(i, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// This is a new model, push it to the `add` list.
|
||||
add.push(model);
|
||||
|
||||
// Listen to added models' events, and index models for lookup by
|
||||
// `id` and by `cid`.
|
||||
model.on('all', this._onModelEvent, this);
|
||||
@@ -657,31 +622,37 @@
|
||||
}
|
||||
|
||||
// See if sorting is needed, update `length` and splice in new models.
|
||||
if (models.length) needsSort = sort;
|
||||
this.length += models.length;
|
||||
args = [at != null ? at : this.models.length, 0];
|
||||
push.apply(args, models);
|
||||
splice.apply(this.models, args);
|
||||
if (add.length) {
|
||||
if (sort) doSort = true;
|
||||
this.length += add.length;
|
||||
if (at != null) {
|
||||
splice.apply(this.models, [at, 0].concat(add));
|
||||
} else {
|
||||
push.apply(this.models, add);
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the collection if appropriate.
|
||||
if (needsSort && this.comparator && at == null) this.sort({silent: true});
|
||||
// Silently sort the collection if appropriate.
|
||||
if (doSort) this.sort({silent: true});
|
||||
|
||||
if (options && options.silent) return this;
|
||||
if (options.silent) return this;
|
||||
|
||||
// Trigger `add` events.
|
||||
while (model = models.shift()) {
|
||||
model.trigger('add', model, this, options);
|
||||
for (i = 0, l = add.length; i < l; i++) {
|
||||
(model = add[i]).trigger('add', model, this, options);
|
||||
}
|
||||
|
||||
// Trigger `sort` if the collection was sorted.
|
||||
if (doSort) this.trigger('sort', this, options);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
// Remove a model, or a list of models from the set. Pass silent to avoid
|
||||
// firing the `remove` event for every model removed.
|
||||
// Remove a model, or a list of models from the set.
|
||||
remove: function(models, options) {
|
||||
var i, l, index, model;
|
||||
options || (options = {});
|
||||
models = _.isArray(models) ? models.slice() : [models];
|
||||
options || (options = {});
|
||||
var i, l, index, model;
|
||||
for (i = 0, l = models.length; i < l; i++) {
|
||||
model = this.get(models[i]);
|
||||
if (!model) continue;
|
||||
@@ -762,14 +733,16 @@
|
||||
if (!this.comparator) {
|
||||
throw new Error('Cannot sort a set without a comparator');
|
||||
}
|
||||
options || (options = {});
|
||||
|
||||
// Run sort based on type of `comparator`.
|
||||
if (_.isString(this.comparator) || this.comparator.length === 1) {
|
||||
this.models = this.sortBy(this.comparator, this);
|
||||
} else {
|
||||
this.models.sort(_.bind(this.comparator, this));
|
||||
}
|
||||
|
||||
if (!options || !options.silent) this.trigger('sort', this, options);
|
||||
if (!options.silent) this.trigger('sort', this, options);
|
||||
return this;
|
||||
},
|
||||
|
||||
@@ -781,10 +754,10 @@
|
||||
// Smartly update a collection with a change set of models, adding,
|
||||
// removing, and merging as necessary.
|
||||
update: function(models, options) {
|
||||
var model, i, l, existing;
|
||||
var add = [], remove = [], modelMap = {};
|
||||
options = _.extend({add: true, merge: true, remove: true}, options);
|
||||
if (options.parse) models = this.parse(models, options);
|
||||
var model, i, l, existing;
|
||||
var add = [], remove = [], modelMap = {};
|
||||
|
||||
// Allow a single model (or no argument) to be passed.
|
||||
if (!_.isArray(models)) models = models ? [models] : [];
|
||||
@@ -836,9 +809,8 @@
|
||||
fetch: function(options) {
|
||||
options = options ? _.clone(options) : {};
|
||||
if (options.parse === void 0) options.parse = true;
|
||||
var collection = this;
|
||||
var success = options.success;
|
||||
options.success = function(resp, status, xhr) {
|
||||
options.success = function(collection, resp, options) {
|
||||
var method = options.update ? 'update' : 'reset';
|
||||
collection[method](resp, options);
|
||||
if (success) success(collection, resp, options);
|
||||
@@ -850,9 +822,9 @@
|
||||
// collection immediately, unless `wait: true` is passed, in which case we
|
||||
// wait for the server to agree.
|
||||
create: function(model, options) {
|
||||
var collection = this;
|
||||
options = options ? _.clone(options) : {};
|
||||
model = this._prepareModel(model, options);
|
||||
var collection = this;
|
||||
if (!model) return false;
|
||||
if (!options.wait) collection.add(model, options);
|
||||
var success = options.success;
|
||||
@@ -929,7 +901,7 @@
|
||||
'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
|
||||
'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke',
|
||||
'max', 'min', 'sortedIndex', 'toArray', 'size', 'first', 'head', 'take',
|
||||
'initial', 'rest', 'tail', 'last', 'without', 'indexOf', 'shuffle',
|
||||
'initial', 'rest', 'tail', 'drop', 'last', 'without', 'indexOf', 'shuffle',
|
||||
'lastIndexOf', 'isEmpty'];
|
||||
|
||||
// Mix in each Underscore method as a proxy to `Collection#models`.
|
||||
@@ -969,7 +941,7 @@
|
||||
// Cached regular expressions for matching named param parts and splatted
|
||||
// parts of route strings.
|
||||
var optionalParam = /\((.*?)\)/g;
|
||||
var namedParam = /:\w+/g;
|
||||
var namedParam = /(\(\?)?:\w+/g;
|
||||
var splatParam = /\*\w+/g;
|
||||
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
|
||||
|
||||
@@ -1020,7 +992,9 @@
|
||||
_routeToRegExp: function(route) {
|
||||
route = route.replace(escapeRegExp, '\\$&')
|
||||
.replace(optionalParam, '(?:$1)?')
|
||||
.replace(namedParam, '([^\/]+)')
|
||||
.replace(namedParam, function(match, optional){
|
||||
return optional ? match : '([^\/]+)';
|
||||
})
|
||||
.replace(splatParam, '(.*?)');
|
||||
return new RegExp('^' + route + '$');
|
||||
},
|
||||
@@ -1121,9 +1095,9 @@
|
||||
// Depending on whether we're using pushState or hashes, and whether
|
||||
// 'onhashchange' is supported, determine how we check the URL state.
|
||||
if (this._hasPushState) {
|
||||
Backbone.$(window).bind('popstate', this.checkUrl);
|
||||
Backbone.$(window).on('popstate', this.checkUrl);
|
||||
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
|
||||
Backbone.$(window).bind('hashchange', this.checkUrl);
|
||||
Backbone.$(window).on('hashchange', this.checkUrl);
|
||||
} else if (this._wantsHashChange) {
|
||||
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
|
||||
}
|
||||
@@ -1155,7 +1129,7 @@
|
||||
// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
|
||||
// but possibly useful for unit testing Routers.
|
||||
stop: function() {
|
||||
Backbone.$(window).unbind('popstate', this.checkUrl).unbind('hashchange', this.checkUrl);
|
||||
Backbone.$(window).off('popstate', this.checkUrl).off('hashchange', this.checkUrl);
|
||||
clearInterval(this._checkUrlInterval);
|
||||
History.started = false;
|
||||
},
|
||||
@@ -1298,18 +1272,6 @@
|
||||
return this;
|
||||
},
|
||||
|
||||
// For small amounts of DOM Elements, where a full-blown template isn't
|
||||
// needed, use **make** to manufacture elements, one at a time.
|
||||
//
|
||||
// var el = this.make('li', {'class': 'row'}, this.model.escape('title'));
|
||||
//
|
||||
make: function(tagName, attributes, content) {
|
||||
var el = document.createElement(tagName);
|
||||
if (attributes) Backbone.$(el).attr(attributes);
|
||||
if (content != null) Backbone.$(el).html(content);
|
||||
return el;
|
||||
},
|
||||
|
||||
// Change the view's element (`this.el` property), including event
|
||||
// re-delegation.
|
||||
setElement: function(element, delegate) {
|
||||
@@ -1347,9 +1309,9 @@
|
||||
method = _.bind(method, this);
|
||||
eventName += '.delegateEvents' + this.cid;
|
||||
if (selector === '') {
|
||||
this.$el.bind(eventName, method);
|
||||
this.$el.on(eventName, method);
|
||||
} else {
|
||||
this.$el.delegate(selector, eventName, method);
|
||||
this.$el.on(eventName, selector, method);
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -1358,7 +1320,7 @@
|
||||
// You usually don't need to use this, but may wish to if you have multiple
|
||||
// Backbone views attached to the same DOM element.
|
||||
undelegateEvents: function() {
|
||||
this.$el.unbind('.delegateEvents' + this.cid);
|
||||
this.$el.off('.delegateEvents' + this.cid);
|
||||
},
|
||||
|
||||
// Performs the initial configuration of a View with a set of options.
|
||||
@@ -1379,7 +1341,8 @@
|
||||
var attrs = _.extend({}, _.result(this, 'attributes'));
|
||||
if (this.id) attrs.id = _.result(this, 'id');
|
||||
if (this.className) attrs['class'] = _.result(this, 'className');
|
||||
this.setElement(this.make(_.result(this, 'tagName'), attrs), false);
|
||||
var $el = Backbone.$('<' + _.result(this, 'tagName') + '>').attr(attrs);
|
||||
this.setElement($el, false);
|
||||
} else {
|
||||
this.setElement(_.result(this, 'el'), false);
|
||||
}
|
||||
@@ -1461,13 +1424,13 @@
|
||||
}
|
||||
|
||||
var success = options.success;
|
||||
options.success = function(resp, status, xhr) {
|
||||
if (success) success(resp, status, xhr);
|
||||
options.success = function(resp) {
|
||||
if (success) success(model, resp, options);
|
||||
model.trigger('sync', model, resp, options);
|
||||
};
|
||||
|
||||
var error = options.error;
|
||||
options.error = function(xhr, status, thrown) {
|
||||
options.error = function(xhr) {
|
||||
if (error) error(model, xhr, options);
|
||||
model.trigger('error', model, xhr, options);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user