Edit File by line
/home/barbar84/www/wp-inclu.../js
File: backbone.js
[500] Fix | Delete
var current = this.attributes;
[501] Fix | Delete
var changed = this.changed;
[502] Fix | Delete
var prev = this._previousAttributes;
[503] Fix | Delete
[504] Fix | Delete
// For each `set` attribute, update or delete the current value.
[505] Fix | Delete
for (var attr in attrs) {
[506] Fix | Delete
val = attrs[attr];
[507] Fix | Delete
if (!_.isEqual(current[attr], val)) changes.push(attr);
[508] Fix | Delete
if (!_.isEqual(prev[attr], val)) {
[509] Fix | Delete
changed[attr] = val;
[510] Fix | Delete
} else {
[511] Fix | Delete
delete changed[attr];
[512] Fix | Delete
}
[513] Fix | Delete
unset ? delete current[attr] : current[attr] = val;
[514] Fix | Delete
}
[515] Fix | Delete
[516] Fix | Delete
// Update the `id`.
[517] Fix | Delete
if (this.idAttribute in attrs) this.id = this.get(this.idAttribute);
[518] Fix | Delete
[519] Fix | Delete
// Trigger all relevant attribute changes.
[520] Fix | Delete
if (!silent) {
[521] Fix | Delete
if (changes.length) this._pending = options;
[522] Fix | Delete
for (var i = 0; i < changes.length; i++) {
[523] Fix | Delete
this.trigger('change:' + changes[i], this, current[changes[i]], options);
[524] Fix | Delete
}
[525] Fix | Delete
}
[526] Fix | Delete
[527] Fix | Delete
// You might be wondering why there's a `while` loop here. Changes can
[528] Fix | Delete
// be recursively nested within `"change"` events.
[529] Fix | Delete
if (changing) return this;
[530] Fix | Delete
if (!silent) {
[531] Fix | Delete
while (this._pending) {
[532] Fix | Delete
options = this._pending;
[533] Fix | Delete
this._pending = false;
[534] Fix | Delete
this.trigger('change', this, options);
[535] Fix | Delete
}
[536] Fix | Delete
}
[537] Fix | Delete
this._pending = false;
[538] Fix | Delete
this._changing = false;
[539] Fix | Delete
return this;
[540] Fix | Delete
},
[541] Fix | Delete
[542] Fix | Delete
// Remove an attribute from the model, firing `"change"`. `unset` is a noop
[543] Fix | Delete
// if the attribute doesn't exist.
[544] Fix | Delete
unset: function(attr, options) {
[545] Fix | Delete
return this.set(attr, void 0, _.extend({}, options, {unset: true}));
[546] Fix | Delete
},
[547] Fix | Delete
[548] Fix | Delete
// Clear all attributes on the model, firing `"change"`.
[549] Fix | Delete
clear: function(options) {
[550] Fix | Delete
var attrs = {};
[551] Fix | Delete
for (var key in this.attributes) attrs[key] = void 0;
[552] Fix | Delete
return this.set(attrs, _.extend({}, options, {unset: true}));
[553] Fix | Delete
},
[554] Fix | Delete
[555] Fix | Delete
// Determine if the model has changed since the last `"change"` event.
[556] Fix | Delete
// If you specify an attribute name, determine if that attribute has changed.
[557] Fix | Delete
hasChanged: function(attr) {
[558] Fix | Delete
if (attr == null) return !_.isEmpty(this.changed);
[559] Fix | Delete
return _.has(this.changed, attr);
[560] Fix | Delete
},
[561] Fix | Delete
[562] Fix | Delete
// Return an object containing all the attributes that have changed, or
[563] Fix | Delete
// false if there are no changed attributes. Useful for determining what
[564] Fix | Delete
// parts of a view need to be updated and/or what attributes need to be
[565] Fix | Delete
// persisted to the server. Unset attributes will be set to undefined.
[566] Fix | Delete
// You can also pass an attributes object to diff against the model,
[567] Fix | Delete
// determining if there *would be* a change.
[568] Fix | Delete
changedAttributes: function(diff) {
[569] Fix | Delete
if (!diff) return this.hasChanged() ? _.clone(this.changed) : false;
[570] Fix | Delete
var old = this._changing ? this._previousAttributes : this.attributes;
[571] Fix | Delete
var changed = {};
[572] Fix | Delete
var hasChanged;
[573] Fix | Delete
for (var attr in diff) {
[574] Fix | Delete
var val = diff[attr];
[575] Fix | Delete
if (_.isEqual(old[attr], val)) continue;
[576] Fix | Delete
changed[attr] = val;
[577] Fix | Delete
hasChanged = true;
[578] Fix | Delete
}
[579] Fix | Delete
return hasChanged ? changed : false;
[580] Fix | Delete
},
[581] Fix | Delete
[582] Fix | Delete
// Get the previous value of an attribute, recorded at the time the last
[583] Fix | Delete
// `"change"` event was fired.
[584] Fix | Delete
previous: function(attr) {
[585] Fix | Delete
if (attr == null || !this._previousAttributes) return null;
[586] Fix | Delete
return this._previousAttributes[attr];
[587] Fix | Delete
},
[588] Fix | Delete
[589] Fix | Delete
// Get all of the attributes of the model at the time of the previous
[590] Fix | Delete
// `"change"` event.
[591] Fix | Delete
previousAttributes: function() {
[592] Fix | Delete
return _.clone(this._previousAttributes);
[593] Fix | Delete
},
[594] Fix | Delete
[595] Fix | Delete
// Fetch the model from the server, merging the response with the model's
[596] Fix | Delete
// local attributes. Any changed attributes will trigger a "change" event.
[597] Fix | Delete
fetch: function(options) {
[598] Fix | Delete
options = _.extend({parse: true}, options);
[599] Fix | Delete
var model = this;
[600] Fix | Delete
var success = options.success;
[601] Fix | Delete
options.success = function(resp) {
[602] Fix | Delete
var serverAttrs = options.parse ? model.parse(resp, options) : resp;
[603] Fix | Delete
if (!model.set(serverAttrs, options)) return false;
[604] Fix | Delete
if (success) success.call(options.context, model, resp, options);
[605] Fix | Delete
model.trigger('sync', model, resp, options);
[606] Fix | Delete
};
[607] Fix | Delete
wrapError(this, options);
[608] Fix | Delete
return this.sync('read', this, options);
[609] Fix | Delete
},
[610] Fix | Delete
[611] Fix | Delete
// Set a hash of model attributes, and sync the model to the server.
[612] Fix | Delete
// If the server returns an attributes hash that differs, the model's
[613] Fix | Delete
// state will be `set` again.
[614] Fix | Delete
save: function(key, val, options) {
[615] Fix | Delete
// Handle both `"key", value` and `{key: value}` -style arguments.
[616] Fix | Delete
var attrs;
[617] Fix | Delete
if (key == null || typeof key === 'object') {
[618] Fix | Delete
attrs = key;
[619] Fix | Delete
options = val;
[620] Fix | Delete
} else {
[621] Fix | Delete
(attrs = {})[key] = val;
[622] Fix | Delete
}
[623] Fix | Delete
[624] Fix | Delete
options = _.extend({validate: true, parse: true}, options);
[625] Fix | Delete
var wait = options.wait;
[626] Fix | Delete
[627] Fix | Delete
// If we're not waiting and attributes exist, save acts as
[628] Fix | Delete
// `set(attr).save(null, opts)` with validation. Otherwise, check if
[629] Fix | Delete
// the model will be valid when the attributes, if any, are set.
[630] Fix | Delete
if (attrs && !wait) {
[631] Fix | Delete
if (!this.set(attrs, options)) return false;
[632] Fix | Delete
} else if (!this._validate(attrs, options)) {
[633] Fix | Delete
return false;
[634] Fix | Delete
}
[635] Fix | Delete
[636] Fix | Delete
// After a successful server-side save, the client is (optionally)
[637] Fix | Delete
// updated with the server-side state.
[638] Fix | Delete
var model = this;
[639] Fix | Delete
var success = options.success;
[640] Fix | Delete
var attributes = this.attributes;
[641] Fix | Delete
options.success = function(resp) {
[642] Fix | Delete
// Ensure attributes are restored during synchronous saves.
[643] Fix | Delete
model.attributes = attributes;
[644] Fix | Delete
var serverAttrs = options.parse ? model.parse(resp, options) : resp;
[645] Fix | Delete
if (wait) serverAttrs = _.extend({}, attrs, serverAttrs);
[646] Fix | Delete
if (serverAttrs && !model.set(serverAttrs, options)) return false;
[647] Fix | Delete
if (success) success.call(options.context, model, resp, options);
[648] Fix | Delete
model.trigger('sync', model, resp, options);
[649] Fix | Delete
};
[650] Fix | Delete
wrapError(this, options);
[651] Fix | Delete
[652] Fix | Delete
// Set temporary attributes if `{wait: true}` to properly find new ids.
[653] Fix | Delete
if (attrs && wait) this.attributes = _.extend({}, attributes, attrs);
[654] Fix | Delete
[655] Fix | Delete
var method = this.isNew() ? 'create' : options.patch ? 'patch' : 'update';
[656] Fix | Delete
if (method === 'patch' && !options.attrs) options.attrs = attrs;
[657] Fix | Delete
var xhr = this.sync(method, this, options);
[658] Fix | Delete
[659] Fix | Delete
// Restore attributes.
[660] Fix | Delete
this.attributes = attributes;
[661] Fix | Delete
[662] Fix | Delete
return xhr;
[663] Fix | Delete
},
[664] Fix | Delete
[665] Fix | Delete
// Destroy this model on the server if it was already persisted.
[666] Fix | Delete
// Optimistically removes the model from its collection, if it has one.
[667] Fix | Delete
// If `wait: true` is passed, waits for the server to respond before removal.
[668] Fix | Delete
destroy: function(options) {
[669] Fix | Delete
options = options ? _.clone(options) : {};
[670] Fix | Delete
var model = this;
[671] Fix | Delete
var success = options.success;
[672] Fix | Delete
var wait = options.wait;
[673] Fix | Delete
[674] Fix | Delete
var destroy = function() {
[675] Fix | Delete
model.stopListening();
[676] Fix | Delete
model.trigger('destroy', model, model.collection, options);
[677] Fix | Delete
};
[678] Fix | Delete
[679] Fix | Delete
options.success = function(resp) {
[680] Fix | Delete
if (wait) destroy();
[681] Fix | Delete
if (success) success.call(options.context, model, resp, options);
[682] Fix | Delete
if (!model.isNew()) model.trigger('sync', model, resp, options);
[683] Fix | Delete
};
[684] Fix | Delete
[685] Fix | Delete
var xhr = false;
[686] Fix | Delete
if (this.isNew()) {
[687] Fix | Delete
_.defer(options.success);
[688] Fix | Delete
} else {
[689] Fix | Delete
wrapError(this, options);
[690] Fix | Delete
xhr = this.sync('delete', this, options);
[691] Fix | Delete
}
[692] Fix | Delete
if (!wait) destroy();
[693] Fix | Delete
return xhr;
[694] Fix | Delete
},
[695] Fix | Delete
[696] Fix | Delete
// Default URL for the model's representation on the server -- if you're
[697] Fix | Delete
// using Backbone's restful methods, override this to change the endpoint
[698] Fix | Delete
// that will be called.
[699] Fix | Delete
url: function() {
[700] Fix | Delete
var base =
[701] Fix | Delete
_.result(this, 'urlRoot') ||
[702] Fix | Delete
_.result(this.collection, 'url') ||
[703] Fix | Delete
urlError();
[704] Fix | Delete
if (this.isNew()) return base;
[705] Fix | Delete
var id = this.get(this.idAttribute);
[706] Fix | Delete
return base.replace(/[^\/]$/, '$&/') + encodeURIComponent(id);
[707] Fix | Delete
},
[708] Fix | Delete
[709] Fix | Delete
// **parse** converts a response into the hash of attributes to be `set` on
[710] Fix | Delete
// the model. The default implementation is just to pass the response along.
[711] Fix | Delete
parse: function(resp, options) {
[712] Fix | Delete
return resp;
[713] Fix | Delete
},
[714] Fix | Delete
[715] Fix | Delete
// Create a new model with identical attributes to this one.
[716] Fix | Delete
clone: function() {
[717] Fix | Delete
return new this.constructor(this.attributes);
[718] Fix | Delete
},
[719] Fix | Delete
[720] Fix | Delete
// A model is new if it has never been saved to the server, and lacks an id.
[721] Fix | Delete
isNew: function() {
[722] Fix | Delete
return !this.has(this.idAttribute);
[723] Fix | Delete
},
[724] Fix | Delete
[725] Fix | Delete
// Check if the model is currently in a valid state.
[726] Fix | Delete
isValid: function(options) {
[727] Fix | Delete
return this._validate({}, _.extend({}, options, {validate: true}));
[728] Fix | Delete
},
[729] Fix | Delete
[730] Fix | Delete
// Run validation against the next complete set of model attributes,
[731] Fix | Delete
// returning `true` if all is well. Otherwise, fire an `"invalid"` event.
[732] Fix | Delete
_validate: function(attrs, options) {
[733] Fix | Delete
if (!options.validate || !this.validate) return true;
[734] Fix | Delete
attrs = _.extend({}, this.attributes, attrs);
[735] Fix | Delete
var error = this.validationError = this.validate(attrs, options) || null;
[736] Fix | Delete
if (!error) return true;
[737] Fix | Delete
this.trigger('invalid', this, error, _.extend(options, {validationError: error}));
[738] Fix | Delete
return false;
[739] Fix | Delete
}
[740] Fix | Delete
[741] Fix | Delete
});
[742] Fix | Delete
[743] Fix | Delete
// Backbone.Collection
[744] Fix | Delete
// -------------------
[745] Fix | Delete
[746] Fix | Delete
// If models tend to represent a single row of data, a Backbone Collection is
[747] Fix | Delete
// more analogous to a table full of data ... or a small slice or page of that
[748] Fix | Delete
// table, or a collection of rows that belong together for a particular reason
[749] Fix | Delete
// -- all of the messages in this particular folder, all of the documents
[750] Fix | Delete
// belonging to this particular author, and so on. Collections maintain
[751] Fix | Delete
// indexes of their models, both in order, and for lookup by `id`.
[752] Fix | Delete
[753] Fix | Delete
// Create a new **Collection**, perhaps to contain a specific type of `model`.
[754] Fix | Delete
// If a `comparator` is specified, the Collection will maintain
[755] Fix | Delete
// its models in sort order, as they're added and removed.
[756] Fix | Delete
var Collection = Backbone.Collection = function(models, options) {
[757] Fix | Delete
options || (options = {});
[758] Fix | Delete
this.preinitialize.apply(this, arguments);
[759] Fix | Delete
if (options.model) this.model = options.model;
[760] Fix | Delete
if (options.comparator !== void 0) this.comparator = options.comparator;
[761] Fix | Delete
this._reset();
[762] Fix | Delete
this.initialize.apply(this, arguments);
[763] Fix | Delete
if (models) this.reset(models, _.extend({silent: true}, options));
[764] Fix | Delete
};
[765] Fix | Delete
[766] Fix | Delete
// Default options for `Collection#set`.
[767] Fix | Delete
var setOptions = {add: true, remove: true, merge: true};
[768] Fix | Delete
var addOptions = {add: true, remove: false};
[769] Fix | Delete
[770] Fix | Delete
// Splices `insert` into `array` at index `at`.
[771] Fix | Delete
var splice = function(array, insert, at) {
[772] Fix | Delete
at = Math.min(Math.max(at, 0), array.length);
[773] Fix | Delete
var tail = Array(array.length - at);
[774] Fix | Delete
var length = insert.length;
[775] Fix | Delete
var i;
[776] Fix | Delete
for (i = 0; i < tail.length; i++) tail[i] = array[i + at];
[777] Fix | Delete
for (i = 0; i < length; i++) array[i + at] = insert[i];
[778] Fix | Delete
for (i = 0; i < tail.length; i++) array[i + length + at] = tail[i];
[779] Fix | Delete
};
[780] Fix | Delete
[781] Fix | Delete
// Define the Collection's inheritable methods.
[782] Fix | Delete
_.extend(Collection.prototype, Events, {
[783] Fix | Delete
[784] Fix | Delete
// The default model for a collection is just a **Backbone.Model**.
[785] Fix | Delete
// This should be overridden in most cases.
[786] Fix | Delete
model: Model,
[787] Fix | Delete
[788] Fix | Delete
[789] Fix | Delete
// preinitialize is an empty function by default. You can override it with a function
[790] Fix | Delete
// or object. preinitialize will run before any instantiation logic is run in the Collection.
[791] Fix | Delete
preinitialize: function(){},
[792] Fix | Delete
[793] Fix | Delete
// Initialize is an empty function by default. Override it with your own
[794] Fix | Delete
// initialization logic.
[795] Fix | Delete
initialize: function(){},
[796] Fix | Delete
[797] Fix | Delete
// The JSON representation of a Collection is an array of the
[798] Fix | Delete
// models' attributes.
[799] Fix | Delete
toJSON: function(options) {
[800] Fix | Delete
return this.map(function(model) { return model.toJSON(options); });
[801] Fix | Delete
},
[802] Fix | Delete
[803] Fix | Delete
// Proxy `Backbone.sync` by default.
[804] Fix | Delete
sync: function() {
[805] Fix | Delete
return Backbone.sync.apply(this, arguments);
[806] Fix | Delete
},
[807] Fix | Delete
[808] Fix | Delete
// Add a model, or list of models to the set. `models` may be Backbone
[809] Fix | Delete
// Models or raw JavaScript objects to be converted to Models, or any
[810] Fix | Delete
// combination of the two.
[811] Fix | Delete
add: function(models, options) {
[812] Fix | Delete
return this.set(models, _.extend({merge: false}, options, addOptions));
[813] Fix | Delete
},
[814] Fix | Delete
[815] Fix | Delete
// Remove a model, or a list of models from the set.
[816] Fix | Delete
remove: function(models, options) {
[817] Fix | Delete
options = _.extend({}, options);
[818] Fix | Delete
var singular = !_.isArray(models);
[819] Fix | Delete
models = singular ? [models] : models.slice();
[820] Fix | Delete
var removed = this._removeModels(models, options);
[821] Fix | Delete
if (!options.silent && removed.length) {
[822] Fix | Delete
options.changes = {added: [], merged: [], removed: removed};
[823] Fix | Delete
this.trigger('update', this, options);
[824] Fix | Delete
}
[825] Fix | Delete
return singular ? removed[0] : removed;
[826] Fix | Delete
},
[827] Fix | Delete
[828] Fix | Delete
// Update a collection by `set`-ing a new list of models, adding new ones,
[829] Fix | Delete
// removing models that are no longer present, and merging models that
[830] Fix | Delete
// already exist in the collection, as necessary. Similar to **Model#set**,
[831] Fix | Delete
// the core operation for updating the data contained by the collection.
[832] Fix | Delete
set: function(models, options) {
[833] Fix | Delete
if (models == null) return;
[834] Fix | Delete
[835] Fix | Delete
options = _.extend({}, setOptions, options);
[836] Fix | Delete
if (options.parse && !this._isModel(models)) {
[837] Fix | Delete
models = this.parse(models, options) || [];
[838] Fix | Delete
}
[839] Fix | Delete
[840] Fix | Delete
var singular = !_.isArray(models);
[841] Fix | Delete
models = singular ? [models] : models.slice();
[842] Fix | Delete
[843] Fix | Delete
var at = options.at;
[844] Fix | Delete
if (at != null) at = +at;
[845] Fix | Delete
if (at > this.length) at = this.length;
[846] Fix | Delete
if (at < 0) at += this.length + 1;
[847] Fix | Delete
[848] Fix | Delete
var set = [];
[849] Fix | Delete
var toAdd = [];
[850] Fix | Delete
var toMerge = [];
[851] Fix | Delete
var toRemove = [];
[852] Fix | Delete
var modelMap = {};
[853] Fix | Delete
[854] Fix | Delete
var add = options.add;
[855] Fix | Delete
var merge = options.merge;
[856] Fix | Delete
var remove = options.remove;
[857] Fix | Delete
[858] Fix | Delete
var sort = false;
[859] Fix | Delete
var sortable = this.comparator && at == null && options.sort !== false;
[860] Fix | Delete
var sortAttr = _.isString(this.comparator) ? this.comparator : null;
[861] Fix | Delete
[862] Fix | Delete
// Turn bare objects into model references, and prevent invalid models
[863] Fix | Delete
// from being added.
[864] Fix | Delete
var model, i;
[865] Fix | Delete
for (i = 0; i < models.length; i++) {
[866] Fix | Delete
model = models[i];
[867] Fix | Delete
[868] Fix | Delete
// If a duplicate is found, prevent it from being added and
[869] Fix | Delete
// optionally merge it into the existing model.
[870] Fix | Delete
var existing = this.get(model);
[871] Fix | Delete
if (existing) {
[872] Fix | Delete
if (merge && model !== existing) {
[873] Fix | Delete
var attrs = this._isModel(model) ? model.attributes : model;
[874] Fix | Delete
if (options.parse) attrs = existing.parse(attrs, options);
[875] Fix | Delete
existing.set(attrs, options);
[876] Fix | Delete
toMerge.push(existing);
[877] Fix | Delete
if (sortable && !sort) sort = existing.hasChanged(sortAttr);
[878] Fix | Delete
}
[879] Fix | Delete
if (!modelMap[existing.cid]) {
[880] Fix | Delete
modelMap[existing.cid] = true;
[881] Fix | Delete
set.push(existing);
[882] Fix | Delete
}
[883] Fix | Delete
models[i] = existing;
[884] Fix | Delete
[885] Fix | Delete
// If this is a new, valid model, push it to the `toAdd` list.
[886] Fix | Delete
} else if (add) {
[887] Fix | Delete
model = models[i] = this._prepareModel(model, options);
[888] Fix | Delete
if (model) {
[889] Fix | Delete
toAdd.push(model);
[890] Fix | Delete
this._addReference(model, options);
[891] Fix | Delete
modelMap[model.cid] = true;
[892] Fix | Delete
set.push(model);
[893] Fix | Delete
}
[894] Fix | Delete
}
[895] Fix | Delete
}
[896] Fix | Delete
[897] Fix | Delete
// Remove stale models.
[898] Fix | Delete
if (remove) {
[899] Fix | Delete
for (i = 0; i < this.length; i++) {
[900] Fix | Delete
model = this.models[i];
[901] Fix | Delete
if (!modelMap[model.cid]) toRemove.push(model);
[902] Fix | Delete
}
[903] Fix | Delete
if (toRemove.length) this._removeModels(toRemove, options);
[904] Fix | Delete
}
[905] Fix | Delete
[906] Fix | Delete
// See if sorting is needed, update `length` and splice in new models.
[907] Fix | Delete
var orderChanged = false;
[908] Fix | Delete
var replace = !sortable && add && remove;
[909] Fix | Delete
if (set.length && replace) {
[910] Fix | Delete
orderChanged = this.length !== set.length || _.some(this.models, function(m, index) {
[911] Fix | Delete
return m !== set[index];
[912] Fix | Delete
});
[913] Fix | Delete
this.models.length = 0;
[914] Fix | Delete
splice(this.models, set, 0);
[915] Fix | Delete
this.length = this.models.length;
[916] Fix | Delete
} else if (toAdd.length) {
[917] Fix | Delete
if (sortable) sort = true;
[918] Fix | Delete
splice(this.models, toAdd, at == null ? this.length : at);
[919] Fix | Delete
this.length = this.models.length;
[920] Fix | Delete
}
[921] Fix | Delete
[922] Fix | Delete
// Silently sort the collection if appropriate.
[923] Fix | Delete
if (sort) this.sort({silent: true});
[924] Fix | Delete
[925] Fix | Delete
// Unless silenced, it's time to fire all appropriate add/sort/update events.
[926] Fix | Delete
if (!options.silent) {
[927] Fix | Delete
for (i = 0; i < toAdd.length; i++) {
[928] Fix | Delete
if (at != null) options.index = at + i;
[929] Fix | Delete
model = toAdd[i];
[930] Fix | Delete
model.trigger('add', model, this, options);
[931] Fix | Delete
}
[932] Fix | Delete
if (sort || orderChanged) this.trigger('sort', this, options);
[933] Fix | Delete
if (toAdd.length || toRemove.length || toMerge.length) {
[934] Fix | Delete
options.changes = {
[935] Fix | Delete
added: toAdd,
[936] Fix | Delete
removed: toRemove,
[937] Fix | Delete
merged: toMerge
[938] Fix | Delete
};
[939] Fix | Delete
this.trigger('update', this, options);
[940] Fix | Delete
}
[941] Fix | Delete
}
[942] Fix | Delete
[943] Fix | Delete
// Return the added (or merged) model (or models).
[944] Fix | Delete
return singular ? models[0] : models;
[945] Fix | Delete
},
[946] Fix | Delete
[947] Fix | Delete
// When you have more items than you want to add or remove individually,
[948] Fix | Delete
// you can reset the entire set with a new list of models, without firing
[949] Fix | Delete
// any granular `add` or `remove` events. Fires `reset` when finished.
[950] Fix | Delete
// Useful for bulk operations and optimizations.
[951] Fix | Delete
reset: function(models, options) {
[952] Fix | Delete
options = options ? _.clone(options) : {};
[953] Fix | Delete
for (var i = 0; i < this.models.length; i++) {
[954] Fix | Delete
this._removeReference(this.models[i], options);
[955] Fix | Delete
}
[956] Fix | Delete
options.previousModels = this.models;
[957] Fix | Delete
this._reset();
[958] Fix | Delete
models = this.add(models, _.extend({silent: true}, options));
[959] Fix | Delete
if (!options.silent) this.trigger('reset', this, options);
[960] Fix | Delete
return models;
[961] Fix | Delete
},
[962] Fix | Delete
[963] Fix | Delete
// Add a model to the end of the collection.
[964] Fix | Delete
push: function(model, options) {
[965] Fix | Delete
return this.add(model, _.extend({at: this.length}, options));
[966] Fix | Delete
},
[967] Fix | Delete
[968] Fix | Delete
// Remove a model from the end of the collection.
[969] Fix | Delete
pop: function(options) {
[970] Fix | Delete
var model = this.at(this.length - 1);
[971] Fix | Delete
return this.remove(model, options);
[972] Fix | Delete
},
[973] Fix | Delete
[974] Fix | Delete
// Add a model to the beginning of the collection.
[975] Fix | Delete
unshift: function(model, options) {
[976] Fix | Delete
return this.add(model, _.extend({at: 0}, options));
[977] Fix | Delete
},
[978] Fix | Delete
[979] Fix | Delete
// Remove a model from the beginning of the collection.
[980] Fix | Delete
shift: function(options) {
[981] Fix | Delete
var model = this.at(0);
[982] Fix | Delete
return this.remove(model, options);
[983] Fix | Delete
},
[984] Fix | Delete
[985] Fix | Delete
// Slice out a sub-array of models from the collection.
[986] Fix | Delete
slice: function() {
[987] Fix | Delete
return slice.apply(this.models, arguments);
[988] Fix | Delete
},
[989] Fix | Delete
[990] Fix | Delete
// Get a model from the set by id, cid, model object with id or cid
[991] Fix | Delete
// properties, or an attributes object that is transformed through modelId.
[992] Fix | Delete
get: function(obj) {
[993] Fix | Delete
if (obj == null) return void 0;
[994] Fix | Delete
return this._byId[obj] ||
[995] Fix | Delete
this._byId[this.modelId(this._isModel(obj) ? obj.attributes : obj)] ||
[996] Fix | Delete
obj.cid && this._byId[obj.cid];
[997] Fix | Delete
},
[998] Fix | Delete
[999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function