mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Update vendors, minified files, and docs.
Former-commit-id: 018dfcade1386aa84492f60c8404ea00c01cbe11
This commit is contained in:
180
vendor/backbone/backbone.js
vendored
180
vendor/backbone/backbone.js
vendored
@@ -184,22 +184,20 @@
|
||||
var Model = Backbone.Model = function(attributes, options) {
|
||||
var defaults;
|
||||
var attrs = attributes || {};
|
||||
if (options && options.collection) this.collection = options.collection;
|
||||
this.attributes = {};
|
||||
this._escapedAttributes = {};
|
||||
this.cid = _.uniqueId('c');
|
||||
this.changed = {};
|
||||
this._changes = {};
|
||||
this._pending = {};
|
||||
this.attributes = {};
|
||||
this._escapedAttributes = {};
|
||||
this._modelState = [];
|
||||
if (options && options.collection) this.collection = options.collection;
|
||||
if (options && options.parse) attrs = this.parse(attrs);
|
||||
if (defaults = _.result(this, 'defaults')) {
|
||||
attrs = _.extend({}, defaults, attrs);
|
||||
}
|
||||
this.set(attrs, {silent: true});
|
||||
// Reset change tracking.
|
||||
this.changed = {};
|
||||
this._changes = {};
|
||||
this._pending = {};
|
||||
this._cleanChange = true;
|
||||
this._modelState = [];
|
||||
this._currentState = _.clone(this.attributes);
|
||||
this._previousAttributes = _.clone(this.attributes);
|
||||
this.initialize.apply(this, arguments);
|
||||
};
|
||||
@@ -210,17 +208,26 @@
|
||||
// A hash of attributes whose current and previous value differ.
|
||||
changed: null,
|
||||
|
||||
// A hash of attributes that have changed since the last time `change`
|
||||
// was called.
|
||||
_changes: null,
|
||||
// Whether there is a pending request to fire in the final `change` loop.
|
||||
_pending: false,
|
||||
|
||||
// A hash of attributes that have changed since the last `change` event
|
||||
// began.
|
||||
_pending: null,
|
||||
// Whether the model is in the midst of a change cycle.
|
||||
_changing: false,
|
||||
|
||||
// A hash of attributes with the current model state to determine if
|
||||
// a `change` should be recorded within a nested `change` block.
|
||||
_changing : null,
|
||||
// Whether there has been a `set` call since the last
|
||||
// calculation of the changed hash, for efficiency.
|
||||
_cleanChange: true,
|
||||
|
||||
// The model state used for comparison in determining if a
|
||||
// change should be fired.
|
||||
_currentState: null,
|
||||
|
||||
// An array queue of all changes attributed to a model
|
||||
// on the next non-silent change event.
|
||||
_modelState: null,
|
||||
|
||||
// A hash of the model's attributes when the last `change` occured.
|
||||
_previousAttributes: null,
|
||||
|
||||
// The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
||||
// CouchDB users may want to set this to `"_id"`.
|
||||
@@ -276,6 +283,7 @@
|
||||
// Extract attributes and options.
|
||||
var silent = options && options.silent;
|
||||
var unset = options && options.unset;
|
||||
|
||||
if (attrs instanceof Model) attrs = attrs.attributes;
|
||||
if (unset) for (attr in attrs) attrs[attr] = void 0;
|
||||
|
||||
@@ -285,38 +293,26 @@
|
||||
// Check for changes of `id`.
|
||||
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
|
||||
|
||||
var changing = this._changing;
|
||||
var now = this.attributes;
|
||||
var escaped = this._escapedAttributes;
|
||||
var prev = this._previousAttributes || {};
|
||||
var esc = this._escapedAttributes;
|
||||
|
||||
// For each `set` attribute...
|
||||
for (attr in attrs) {
|
||||
val = attrs[attr];
|
||||
|
||||
// If the new and current value differ, record the change.
|
||||
if (!_.isEqual(now[attr], val) || (unset && _.has(now, attr))) {
|
||||
delete escaped[attr];
|
||||
this._changes[attr] = true;
|
||||
}
|
||||
// If an escaped attr exists, and the new and current value differ, remove the escaped attr.
|
||||
if (esc[attr] && !_.isEqual(now[attr], val) || (unset && _.has(now, attr))) delete esc[attr];
|
||||
|
||||
// Update or delete the current value.
|
||||
unset ? delete now[attr] : now[attr] = val;
|
||||
|
||||
// If the new and previous value differ, record the change. If not,
|
||||
// then remove changes for this attribute.
|
||||
if (!_.isEqual(prev[attr], val) || (_.has(now, attr) !== _.has(prev, attr))) {
|
||||
this.changed[attr] = val;
|
||||
if (!silent) this._pending[attr] = true;
|
||||
} else {
|
||||
delete this.changed[attr];
|
||||
delete this._pending[attr];
|
||||
if (!changing) delete this._changes[attr];
|
||||
}
|
||||
|
||||
if (changing && _.isEqual(now[attr], changing[attr])) delete this._changes[attr];
|
||||
// Track any action on the attribute.
|
||||
this._modelState.push(attr, val, unset);
|
||||
}
|
||||
|
||||
// Signal that the model's state has potentially changed.
|
||||
this._cleanChange = false;
|
||||
|
||||
// Fire the `"change"` events.
|
||||
if (!silent) this.change(options);
|
||||
return this;
|
||||
@@ -341,6 +337,7 @@
|
||||
// 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) {
|
||||
@@ -461,37 +458,23 @@
|
||||
// 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;
|
||||
var current = this._changing = {};
|
||||
var i, changing = this._changing;
|
||||
this._changing = true;
|
||||
|
||||
// Silent changes become pending changes.
|
||||
for (var attr in this._changes) this._pending[attr] = true;
|
||||
// Generate the changes to be triggered on the model.
|
||||
var triggers = this._changeCenter(true);
|
||||
this._pending = triggers.length;
|
||||
|
||||
// Trigger 'change:attr' for any new or silent changes.
|
||||
var changes = this._changes;
|
||||
this._changes = {};
|
||||
|
||||
// Set the correct state for this._changing values
|
||||
var triggers = [];
|
||||
for (var attr in changes) {
|
||||
current[attr] = this.get(attr);
|
||||
triggers.push(attr);
|
||||
for (i = triggers.length - 2; i >= 0; i -= 2) {
|
||||
this.trigger('change:' + triggers[i], this, triggers[i + 1], options);
|
||||
}
|
||||
|
||||
for (var i=0, l=triggers.length; i < l; i++) {
|
||||
this.trigger('change:' + triggers[i], this, current[triggers[i]], options);
|
||||
}
|
||||
if (changing) return this;
|
||||
|
||||
// Continue firing `"change"` events while there are pending changes.
|
||||
while (!_.isEmpty(this._pending)) {
|
||||
this._pending = {};
|
||||
// Trigger a `change` while there have been changes.
|
||||
while (this._pending) {
|
||||
this._pending = false;
|
||||
this.trigger('change', this, options);
|
||||
// Pending and silent changes still remain.
|
||||
for (var attr in this.changed) {
|
||||
if (this._pending[attr] || this._changes[attr]) continue;
|
||||
delete this.changed[attr];
|
||||
}
|
||||
this._previousAttributes = _.clone(this.attributes);
|
||||
}
|
||||
|
||||
@@ -502,6 +485,7 @@
|
||||
// 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._cleanChange) this._changeCenter();
|
||||
if (attr == null) return !_.isEmpty(this.changed);
|
||||
return _.has(this.changed, attr);
|
||||
},
|
||||
@@ -522,6 +506,42 @@
|
||||
return changed;
|
||||
},
|
||||
|
||||
// Calculates and handles any changes in `this._modelState`,
|
||||
// checking against `this._currentState` to determine current changes.
|
||||
_changeCenter: function (change) {
|
||||
this.changed = {};
|
||||
var local = {};
|
||||
var triggers = [];
|
||||
var modelState = this._modelState;
|
||||
var currentState = this._currentState;
|
||||
|
||||
// Loop through the current queue of potential model changes.
|
||||
for (var i = modelState.length - 3; i >= 0; i -= 3) {
|
||||
var key = modelState[i], val = modelState[i + 1], unset = modelState[i + 2];
|
||||
|
||||
// If the item hasn't been set locally this round, proceed.
|
||||
if (!local[key]) {
|
||||
local[key] = val;
|
||||
|
||||
// Check if the attribute has been modified since the last change,
|
||||
// and update `this.changed` accordingly.
|
||||
if (currentState[key] !== val || (_.has(currentState, key) && unset)) {
|
||||
this.changed[key] = val;
|
||||
|
||||
// Triggers & modifications are only created inside a `change` call.
|
||||
if (!change) continue;
|
||||
triggers.push(key, val);
|
||||
(!unset) ? currentState[key] = val : delete currentState[key];
|
||||
}
|
||||
}
|
||||
modelState.splice(i,3);
|
||||
}
|
||||
|
||||
// Signals `this.changed` is current to prevent duplicate calls from `this.hasChanged`.
|
||||
this._cleanChange = true;
|
||||
return triggers;
|
||||
},
|
||||
|
||||
// Get the previous value of an attribute, recorded at the time the last
|
||||
// `"change"` event was fired.
|
||||
previous: function(attr) {
|
||||
@@ -599,26 +619,27 @@
|
||||
// Add a model, or list of models to the set. Pass **silent** to avoid
|
||||
// firing the `add` event for every new model.
|
||||
add: function(models, options) {
|
||||
var i, args, length, model, existing;
|
||||
var i, args, length, model, existing, sort;
|
||||
var at = options && options.at;
|
||||
models = _.isArray(models) ? models.slice() : [models];
|
||||
|
||||
// Begin by turning bare objects into model references, and preventing
|
||||
// invalid models from being added.
|
||||
for (i = 0, length = models.length; i < length; i++) {
|
||||
if (models[i] = this._prepareModel(models[i], options)) continue;
|
||||
throw new Error("Can't add an invalid model to a collection");
|
||||
}
|
||||
|
||||
// Turn bare objects into model references, and prevent invalid models
|
||||
// from being added.
|
||||
for (i = models.length - 1; i >= 0; i--) {
|
||||
model = models[i];
|
||||
existing = model.id != null && this._byId[model.id];
|
||||
if(!(model = this._prepareModel(models[i], options))) {
|
||||
this.trigger("error", this, models[i], options);
|
||||
models.splice(i, 1);
|
||||
continue;
|
||||
}
|
||||
models[i] = model;
|
||||
|
||||
// If a duplicate is found, splice it out and optionally merge it into
|
||||
// the existing model.
|
||||
existing = model.id != null && this._byId[model.id];
|
||||
// If a duplicate is found, prevent it from being added and
|
||||
// optionally merge it into the existing model.
|
||||
if (existing || this._byCid[model.cid]) {
|
||||
if (options && options.merge && existing) {
|
||||
existing.set(model, options);
|
||||
sort = true;
|
||||
}
|
||||
models.splice(i, 1);
|
||||
continue;
|
||||
@@ -631,14 +652,15 @@
|
||||
if (model.id != null) this._byId[model.id] = model;
|
||||
}
|
||||
|
||||
// Update `length` and splice in new models.
|
||||
// See if sorting is needed, update `length` and splice in new models.
|
||||
if (models.length) sort = true;
|
||||
this.length += models.length;
|
||||
args = [at != null ? at : this.models.length, 0];
|
||||
push.apply(args, models);
|
||||
splice.apply(this.models, args);
|
||||
|
||||
// Sort the collection if appropriate.
|
||||
if (this.comparator && at == null) this.sort({silent: true});
|
||||
if (sort && this.comparator && at == null) this.sort({silent: true});
|
||||
|
||||
if (options && options.silent) return this;
|
||||
|
||||
@@ -821,7 +843,7 @@
|
||||
},
|
||||
|
||||
// Reset all internal state. Called when the collection is reset.
|
||||
_reset: function(options) {
|
||||
_reset: function() {
|
||||
this.length = 0;
|
||||
this.models = [];
|
||||
this._byId = {};
|
||||
@@ -1313,7 +1335,7 @@
|
||||
// Keys with special meaning *(model, collection, id, className)*, are
|
||||
// attached directly to the view.
|
||||
_configure: function(options) {
|
||||
if (this.options) options = _.extend({}, this.options, options);
|
||||
if (this.options) options = _.extend({}, _.result(this, 'options'), options);
|
||||
_.extend(this, _.pick(options, viewOptions));
|
||||
this.options = options;
|
||||
},
|
||||
@@ -1381,7 +1403,7 @@
|
||||
// Ensure that we have the appropriate request data.
|
||||
if (!options.data && model && (method === 'create' || method === 'update')) {
|
||||
params.contentType = 'application/json';
|
||||
params.data = JSON.stringify(model);
|
||||
params.data = JSON.stringify(model.toJSON(options));
|
||||
}
|
||||
|
||||
// For older servers, emulate JSON by encoding the request into an HTML-form.
|
||||
|
||||
42
vendor/backbone/test/collection.js
vendored
42
vendor/backbone/test/collection.js
vendored
@@ -542,8 +542,7 @@ $(document).ready(function() {
|
||||
equal(col.length, 0);
|
||||
});
|
||||
|
||||
test("#861, adding models to a collection which do not pass validation", 1, function() {
|
||||
raises(function() {
|
||||
test("#861, adding models to a collection which do not pass validation", 2, function() {
|
||||
var Model = Backbone.Model.extend({
|
||||
validate: function(attrs) {
|
||||
if (attrs.id == 3) return "id can't be 3";
|
||||
@@ -554,26 +553,26 @@ $(document).ready(function() {
|
||||
model: Model
|
||||
});
|
||||
|
||||
var col = new Collection;
|
||||
var collection = new Collection;
|
||||
collection.on("error", function() { ok(true); });
|
||||
|
||||
col.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
|
||||
}, function(e) {
|
||||
return e.message === "Can't add an invalid model to a collection";
|
||||
});
|
||||
collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
|
||||
deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
|
||||
});
|
||||
|
||||
test("throwing during add leaves consistent state", 4, function() {
|
||||
var col = new Backbone.Collection();
|
||||
col.on('test', function() { ok(false); });
|
||||
col.model = Backbone.Model.extend({
|
||||
test("Invalid models are discarded.", 5, function() {
|
||||
var collection = new Backbone.Collection;
|
||||
collection.on('test', function() { ok(true); });
|
||||
collection.model = Backbone.Model.extend({
|
||||
validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
|
||||
});
|
||||
var model = new col.model({id: 1, valid: true});
|
||||
raises(function() { col.add([model, {id: 2}]); });
|
||||
var model = new collection.model({id: 1, valid: true});
|
||||
collection.add([model, {id: 2}]);;
|
||||
model.trigger('test');
|
||||
ok(!col.getByCid(model.cid));
|
||||
ok(!col.get(1));
|
||||
equal(col.length, 0);
|
||||
ok(collection.getByCid(model.cid));
|
||||
ok(collection.get(1));
|
||||
ok(!collection.get(2));
|
||||
equal(collection.length, 1);
|
||||
});
|
||||
|
||||
test("multiple copies of the same model", 3, function() {
|
||||
@@ -716,4 +715,15 @@ $(document).ready(function() {
|
||||
this.ajaxSettings.success([model]);
|
||||
});
|
||||
|
||||
test("`sort` shouldn't always fire on `add`", 1, function() {
|
||||
var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
|
||||
comparator: 'id'
|
||||
});
|
||||
c.sort = function(){ ok(true); };
|
||||
c.add([]);
|
||||
c.add({id: 1});
|
||||
c.add([{id: 2}, {id: 3}]);
|
||||
c.add({id: 4});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
14
vendor/backbone/test/model.js
vendored
14
vendor/backbone/test/model.js
vendored
@@ -903,4 +903,18 @@ $(document).ready(function() {
|
||||
expect(0);
|
||||
});
|
||||
|
||||
test("silent changes in last `change` event back to original does not trigger change", 2, function() {
|
||||
var changes = [];
|
||||
var model = new Backbone.Model();
|
||||
model.on('change:a change:b change:c', function(model, val) { changes.push(val); });
|
||||
model.on('change', function() {
|
||||
model.set({a:'c'}, {silent:true});
|
||||
});
|
||||
model.set({a:'a'});
|
||||
deepEqual(changes, ['a']);
|
||||
model.set({a:'a'}, {silent:true});
|
||||
model.change();
|
||||
deepEqual(changes, ['a']);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
24
vendor/backbone/test/view.js
vendored
24
vendor/backbone/test/view.js
vendored
@@ -165,6 +165,30 @@ $(document).ready(function() {
|
||||
strictEqual(new View().el.id, 'id');
|
||||
});
|
||||
|
||||
test("with options function", 3, function() {
|
||||
var View1 = Backbone.View.extend({
|
||||
options: function() {
|
||||
return {
|
||||
title: 'title1',
|
||||
acceptText: 'confirm'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var View2 = View1.extend({
|
||||
options: function() {
|
||||
return _.extend(View1.prototype.options.call(this), {
|
||||
title: 'title2',
|
||||
fixed: true
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
strictEqual(new View2().options.title, 'title2');
|
||||
strictEqual(new View2().options.acceptText, 'confirm');
|
||||
strictEqual(new View2().options.fixed, true);
|
||||
});
|
||||
|
||||
test("with attributes", 2, function() {
|
||||
var View = Backbone.View.extend({
|
||||
attributes: {
|
||||
|
||||
Reference in New Issue
Block a user