"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "vendor/backbone/test/model.js" between
lodash-4.0.0.tar.gz and lodash-4.17.21.tar.gz

About: lodash is a modern JavaScript utility library delivering modularity, performance, & extras. Tagged version.

model.js  (lodash-4.0.0):model.js  (lodash-4.17.21)
(function() { (function(QUnit) {
var ProxyModel = Backbone.Model.extend(); var ProxyModel = Backbone.Model.extend();
var Klass = Backbone.Collection.extend({ var Klass = Backbone.Collection.extend({
url: function() { return '/collection'; } url: function() { return '/collection'; }
}); });
var doc, collection; var doc, collection;
QUnit.module('Backbone.Model', { QUnit.module('Backbone.Model', {
beforeEach: function(assert) { beforeEach: function(assert) {
skipping to change at line 37 skipping to change at line 37
initialize: function() { initialize: function() {
this.one = 1; this.one = 1;
assert.equal(this.collection, collection); assert.equal(this.collection, collection);
} }
}); });
var model = new Model({}, {collection: collection}); var model = new Model({}, {collection: collection});
assert.equal(model.one, 1); assert.equal(model.one, 1);
assert.equal(model.collection, collection); assert.equal(model.collection, collection);
}); });
QUnit.test('Object.prototype properties are overridden by attributes', functio
n(assert) {
assert.expect(1);
var model = new Backbone.Model({hasOwnProperty: true});
assert.equal(model.get('hasOwnProperty'), true);
});
QUnit.test('initialize with attributes and options', function(assert) { QUnit.test('initialize with attributes and options', function(assert) {
assert.expect(1); assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
initialize: function(attributes, options) { initialize: function(attributes, options) {
this.one = options.one; this.one = options.one;
} }
}); });
var model = new Model({}, {one: 1}); var model = new Model({}, {one: 1});
assert.equal(model.one, 1); assert.equal(model.one, 1);
}); });
skipping to change at line 60 skipping to change at line 66
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
parse: function(attrs) { parse: function(attrs) {
attrs.value += 1; attrs.value += 1;
return attrs; return attrs;
} }
}); });
var model = new Model({value: 1}, {parse: true}); var model = new Model({value: 1}, {parse: true});
assert.equal(model.get('value'), 2); assert.equal(model.get('value'), 2);
}); });
QUnit.test('initialize with defaults', function(assert) { QUnit.test('preinitialize', function(assert) {
assert.expect(2); assert.expect(2);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
defaults: {
firstName: 'Unknown', preinitialize: function() {
lastName: 'Unknown' this.one = 1;
} }
}); });
var model = new Model({'firstName': 'John'}); var model = new Model({}, {collection: collection});
assert.equal(model.get('firstName'), 'John'); assert.equal(model.one, 1);
assert.equal(model.get('lastName'), 'Unknown'); assert.equal(model.collection, collection);
});
QUnit.test('preinitialize occurs before the model is set up', function(assert)
{
assert.expect(6);
var Model = Backbone.Model.extend({
preinitialize: function() {
assert.equal(this.collection, undefined);
assert.equal(this.cid, undefined);
assert.equal(this.id, undefined);
}
});
var model = new Model({id: 'foo'}, {collection: collection});
assert.equal(model.collection, collection);
assert.equal(model.id, 'foo');
assert.notEqual(model.cid, undefined);
}); });
QUnit.test('parse can return null', function(assert) { QUnit.test('parse can return null', function(assert) {
assert.expect(1); assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
parse: function(attrs) { parse: function(attrs) {
attrs.value += 1; attrs.value += 1;
return null; return null;
} }
}); });
skipping to change at line 92 skipping to change at line 114
assert.equal(JSON.stringify(model.toJSON()), '{}'); assert.equal(JSON.stringify(model.toJSON()), '{}');
}); });
QUnit.test('url', function(assert) { QUnit.test('url', function(assert) {
assert.expect(3); assert.expect(3);
doc.urlRoot = null; doc.urlRoot = null;
assert.equal(doc.url(), '/collection/1-the-tempest'); assert.equal(doc.url(), '/collection/1-the-tempest');
doc.collection.url = '/collection/'; doc.collection.url = '/collection/';
assert.equal(doc.url(), '/collection/1-the-tempest'); assert.equal(doc.url(), '/collection/1-the-tempest');
doc.collection = null; doc.collection = null;
assert.throws(function() { doc.url(); }); assert.raises(function() { doc.url(); });
doc.collection = collection; doc.collection = collection;
}); });
QUnit.test('url when using urlRoot, and uri encoding', function(assert) { QUnit.test('url when using urlRoot, and uri encoding', function(assert) {
assert.expect(2); assert.expect(2);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
urlRoot: '/collection' urlRoot: '/collection'
}); });
var model = new Model(); var model = new Model();
assert.equal(model.url(), '/collection'); assert.equal(model.url(), '/collection');
skipping to change at line 254 skipping to change at line 276
return attr.a > 1 && attr.b != null; return attr.a > 1 && attr.b != null;
}), true); }), true);
}); });
QUnit.test('set and unset', function(assert) { QUnit.test('set and unset', function(assert) {
assert.expect(8); assert.expect(8);
var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3}); var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
var changeCount = 0; var changeCount = 0;
a.on('change:foo', function() { changeCount += 1; }); a.on('change:foo', function() { changeCount += 1; });
a.set({foo: 2}); a.set({foo: 2});
assert.ok(a.get('foo') == 2, 'Foo should have changed.'); assert.equal(a.get('foo'), 2, 'Foo should have changed.');
assert.ok(changeCount == 1, 'Change count should have incremented.'); assert.equal(changeCount, 1, 'Change count should have incremented.');
// set with value that is not new shouldn't fire change event // set with value that is not new shouldn't fire change event
a.set({foo: 2}); a.set({foo: 2});
assert.ok(a.get('foo') == 2, 'Foo should NOT have changed, still 2'); assert.equal(a.get('foo'), 2, 'Foo should NOT have changed, still 2');
assert.ok(changeCount == 1, 'Change count should NOT have incremented.'); assert.equal(changeCount, 1, 'Change count should NOT have incremented.');
a.validate = function(attrs) { a.validate = function(attrs) {
assert.equal(attrs.foo, void 0, 'validate:true passed while unsetting'); assert.equal(attrs.foo, void 0, 'validate:true passed while unsetting');
}; };
a.unset('foo', {validate: true}); a.unset('foo', {validate: true});
assert.equal(a.get('foo'), void 0, 'Foo should have changed'); assert.equal(a.get('foo'), void 0, 'Foo should have changed');
delete a.validate; delete a.validate;
assert.ok(changeCount == 2, 'Change count should have incremented for unset. '); assert.equal(changeCount, 2, 'Change count should have incremented for unset .');
a.unset('id'); a.unset('id');
assert.equal(a.id, undefined, 'Unsetting the id should remove the id propert y.'); assert.equal(a.id, undefined, 'Unsetting the id should remove the id propert y.');
}); });
QUnit.test('#2030 - set with failed validate, followed by another set triggers change', function(assert) { QUnit.test('#2030 - set with failed validate, followed by another set triggers change', function(assert) {
var attr = 0, main = 0, error = 0; var attr = 0, main = 0, error = 0;
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
validate: function(attr) { validate: function(attrs) {
if (attr.x > 1) { if (attrs.x > 1) {
error++; error++;
return 'this is an error'; return 'this is an error';
} }
} }
}); });
var model = new Model({x: 0}); var model = new Model({x: 0});
model.on('change:x', function() { attr++; }); model.on('change:x', function() { attr++; });
model.on('change', function() { main++; }); model.on('change', function() { main++; });
model.set({x: 2}, {validate: true}); model.set({x: 2}, {validate: true});
model.set({x: 1}, {validate: true}); model.set({x: 1}, {validate: true});
skipping to change at line 382 skipping to change at line 404
var model = new Model(); var model = new Model();
assert.equal(model.cid.charAt(0), 'm'); assert.equal(model.cid.charAt(0), 'm');
model = new Backbone.Model(); model = new Backbone.Model();
assert.equal(model.cid.charAt(0), 'c'); assert.equal(model.cid.charAt(0), 'c');
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
model: Model model: Model
}); });
var collection = new Collection([{id: 'c5'}, {id: 'c6'}, {id: 'c7'}]); var col = new Collection([{id: 'c5'}, {id: 'c6'}, {id: 'c7'}]);
assert.equal(collection.get('c6').cid.charAt(0), 'm'); assert.equal(col.get('c6').cid.charAt(0), 'm');
collection.set([{id: 'c6', value: 'test'}], { col.set([{id: 'c6', value: 'test'}], {
merge: true, merge: true,
add: true, add: true,
remove: false remove: false
}); });
assert.ok(collection.get('c6').has('value')); assert.ok(col.get('c6').has('value'));
}); });
QUnit.test('set an empty string', function(assert) { QUnit.test('set an empty string', function(assert) {
assert.expect(1); assert.expect(1);
var model = new Backbone.Model({name: 'Model'}); var model = new Backbone.Model({name: 'Model'});
model.set({name: ''}); model.set({name: ''});
assert.equal(model.get('name'), ''); assert.equal(model.get('name'), '');
}); });
QUnit.test('setting an object', function(assert) { QUnit.test('setting an object', function(assert) {
skipping to change at line 431 skipping to change at line 453
model.on('change', function() { model.on('change', function() {
var changedAttrs = model.changedAttributes(); var changedAttrs = model.changedAttributes();
assert.ok('name' in changedAttrs); assert.ok('name' in changedAttrs);
}); });
model.clear(); model.clear();
assert.equal(changed, true); assert.equal(changed, true);
assert.equal(model.get('name'), undefined); assert.equal(model.get('name'), undefined);
}); });
QUnit.test('defaults', function(assert) { QUnit.test('defaults', function(assert) {
assert.expect(4); assert.expect(9);
var Defaulted = Backbone.Model.extend({ var Defaulted = Backbone.Model.extend({
defaults: { defaults: {
one: 1, one: 1,
two: 2 two: 2
} }
}); });
var model = new Defaulted({two: undefined}); var model = new Defaulted({two: undefined});
assert.equal(model.get('one'), 1); assert.equal(model.get('one'), 1);
assert.equal(model.get('two'), 2); assert.equal(model.get('two'), 2);
model = new Defaulted({two: 3});
assert.equal(model.get('one'), 1);
assert.equal(model.get('two'), 3);
Defaulted = Backbone.Model.extend({ Defaulted = Backbone.Model.extend({
defaults: function() { defaults: function() {
return { return {
one: 3, one: 3,
two: 4 two: 4
}; };
} }
}); });
model = new Defaulted({two: undefined}); model = new Defaulted({two: undefined});
assert.equal(model.get('one'), 3); assert.equal(model.get('one'), 3);
assert.equal(model.get('two'), 4); assert.equal(model.get('two'), 4);
Defaulted = Backbone.Model.extend({
defaults: {hasOwnProperty: true}
});
model = new Defaulted();
assert.equal(model.get('hasOwnProperty'), true);
model = new Defaulted({hasOwnProperty: undefined});
assert.equal(model.get('hasOwnProperty'), true);
model = new Defaulted({hasOwnProperty: false});
assert.equal(model.get('hasOwnProperty'), false);
}); });
QUnit.test('change, hasChanged, changedAttributes, previous, previousAttribute s', function(assert) { QUnit.test('change, hasChanged, changedAttributes, previous, previousAttribute s', function(assert) {
assert.expect(9); assert.expect(9);
var model = new Backbone.Model({name: 'Tim', age: 10}); var model = new Backbone.Model({name: 'Tim', age: 10});
assert.deepEqual(model.changedAttributes(), false); assert.deepEqual(model.changedAttributes(), false);
model.on('change', function() { model.on('change', function() {
assert.ok(model.hasChanged('name'), 'name changed'); assert.ok(model.hasChanged('name'), 'name changed');
assert.ok(!model.hasChanged('age'), 'age did not'); assert.ok(!model.hasChanged('age'), 'age did not');
assert.ok(_.isEqual(model.changedAttributes(), {name: 'Rob'}), 'changedAtt ributes returns the changed attrs'); assert.ok(_.isEqual(model.changedAttributes(), {name: 'Rob'}), 'changedAtt ributes returns the changed attrs');
skipping to change at line 483 skipping to change at line 517
var model = new Backbone.Model({a: 'a', b: 'b'}); var model = new Backbone.Model({a: 'a', b: 'b'});
assert.deepEqual(model.changedAttributes(), false); assert.deepEqual(model.changedAttributes(), false);
assert.equal(model.changedAttributes({a: 'a'}), false); assert.equal(model.changedAttributes({a: 'a'}), false);
assert.equal(model.changedAttributes({a: 'b'}).a, 'b'); assert.equal(model.changedAttributes({a: 'b'}).a, 'b');
}); });
QUnit.test('change with options', function(assert) { QUnit.test('change with options', function(assert) {
assert.expect(2); assert.expect(2);
var value; var value;
var model = new Backbone.Model({name: 'Rob'}); var model = new Backbone.Model({name: 'Rob'});
model.on('change', function(model, options) { model.on('change', function(m, options) {
value = options.prefix + model.get('name'); value = options.prefix + m.get('name');
}); });
model.set({name: 'Bob'}, {prefix: 'Mr. '}); model.set({name: 'Bob'}, {prefix: 'Mr. '});
assert.equal(value, 'Mr. Bob'); assert.equal(value, 'Mr. Bob');
model.set({name: 'Sue'}, {prefix: 'Ms. '}); model.set({name: 'Sue'}, {prefix: 'Ms. '});
assert.equal(value, 'Ms. Sue'); assert.equal(value, 'Ms. Sue');
}); });
QUnit.test('change after initialize', function(assert) { QUnit.test('change after initialize', function(assert) {
assert.expect(1); assert.expect(1);
var changed = 0; var changed = 0;
skipping to change at line 520 skipping to change at line 554
}); });
model.set({lastName: 'Hicks'}); model.set({lastName: 'Hicks'});
}); });
QUnit.test('validate after save', function(assert) { QUnit.test('validate after save', function(assert) {
assert.expect(2); assert.expect(2);
var lastError, model = new Backbone.Model(); var lastError, model = new Backbone.Model();
model.validate = function(attrs) { model.validate = function(attrs) {
if (attrs.admin) return "Can't change admin status."; if (attrs.admin) return "Can't change admin status.";
}; };
model.sync = function(method, model, options) { model.sync = function(method, m, options) {
options.success.call(this, {admin: true}); options.success.call(this, {admin: true});
}; };
model.on('invalid', function(model, error) { model.on('invalid', function(m, error) {
lastError = error; lastError = error;
}); });
model.save(null); model.save(null);
assert.equal(lastError, "Can't change admin status."); assert.equal(lastError, "Can't change admin status.");
assert.equal(model.validationError, "Can't change admin status."); assert.equal(model.validationError, "Can't change admin status.");
}); });
QUnit.test('save', function(assert) { QUnit.test('save', function(assert) {
assert.expect(2); assert.expect(2);
skipping to change at line 545 skipping to change at line 579
assert.equal(this.syncArgs.method, 'update'); assert.equal(this.syncArgs.method, 'update');
assert.ok(_.isEqual(this.syncArgs.model, doc)); assert.ok(_.isEqual(this.syncArgs.model, doc));
}); });
QUnit.test('save, fetch, destroy triggers error event when an error occurs', f unction(assert) { QUnit.test('save, fetch, destroy triggers error event when an error occurs', f unction(assert) {
assert.expect(3); assert.expect(3);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('error', function() { model.on('error', function() {
assert.ok(true); assert.ok(true);
}); });
model.sync = function(method, model, options) { model.sync = function(method, m, options) {
options.error(); options.error();
}; };
model.save({data: 2, id: 1}); model.save({data: 2, id: 1});
model.fetch(); model.fetch();
model.destroy(); model.destroy();
}); });
QUnit.test('#3283 - save, fetch, destroy calls success with context', function (assert) { QUnit.test('#3283 - save, fetch, destroy calls success with context', function (assert) {
assert.expect(3); assert.expect(3);
var model = new Backbone.Model(); var model = new Backbone.Model();
var obj = {}; var obj = {};
var options = { var options = {
context: obj, context: obj,
success: function() { success: function() {
assert.equal(this, obj); assert.equal(this, obj);
} }
}; };
model.sync = function(method, model, options) { model.sync = function(method, m, opts) {
options.success.call(options.context); opts.success.call(opts.context);
}; };
model.save({data: 2, id: 1}, options); model.save({data: 2, id: 1}, options);
model.fetch(options); model.fetch(options);
model.destroy(options); model.destroy(options);
}); });
QUnit.test('#3283 - save, fetch, destroy calls error with context', function(a ssert) { QUnit.test('#3283 - save, fetch, destroy calls error with context', function(a ssert) {
assert.expect(3); assert.expect(3);
var model = new Backbone.Model(); var model = new Backbone.Model();
var obj = {}; var obj = {};
var options = { var options = {
context: obj, context: obj,
error: function() { error: function() {
assert.equal(this, obj); assert.equal(this, obj);
} }
}; };
model.sync = function(method, model, options) { model.sync = function(method, m, opts) {
options.error.call(options.context); opts.error.call(opts.context);
}; };
model.save({data: 2, id: 1}, options); model.save({data: 2, id: 1}, options);
model.fetch(options); model.fetch(options);
model.destroy(options); model.destroy(options);
}); });
QUnit.test('#3470 - save and fetch with parse false', function(assert) { QUnit.test('#3470 - save and fetch with parse false', function(assert) {
assert.expect(2); assert.expect(2);
var i = 0; var i = 0;
var model = new Backbone.Model(); var model = new Backbone.Model();
model.parse = function() { model.parse = function() {
assert.ok(false); assert.ok(false);
}; };
model.sync = function(method, model, options) { model.sync = function(method, m, options) {
options.success({i: ++i}); options.success({i: ++i});
}; };
model.fetch({parse: false}); model.fetch({parse: false});
assert.equal(model.get('i'), i); assert.equal(model.get('i'), i);
model.save(null, {parse: false}); model.save(null, {parse: false});
assert.equal(model.get('i'), i); assert.equal(model.get('i'), i);
}); });
QUnit.test('save with PATCH', function(assert) { QUnit.test('save with PATCH', function(assert) {
doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4}); doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
skipping to change at line 630 skipping to change at line 664
doc.clear().save({b: 2, d: 4}, {patch: true, attrs: {B: 1, D: 3}}); doc.clear().save({b: 2, d: 4}, {patch: true, attrs: {B: 1, D: 3}});
assert.equal(this.syncArgs.options.attrs.D, 3); assert.equal(this.syncArgs.options.attrs.D, 3);
assert.equal(this.syncArgs.options.attrs.d, undefined); assert.equal(this.syncArgs.options.attrs.d, undefined);
assert.equal(this.ajaxSettings.data, '{"B":1,"D":3}'); assert.equal(this.ajaxSettings.data, '{"B":1,"D":3}');
assert.deepEqual(doc.attributes, {b: 2, d: 4}); assert.deepEqual(doc.attributes, {b: 2, d: 4});
}); });
QUnit.test('save in positional style', function(assert) { QUnit.test('save in positional style', function(assert) {
assert.expect(1); assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.sync = function(method, model, options) { model.sync = function(method, m, options) {
options.success(); options.success();
}; };
model.save('title', 'Twelfth Night'); model.save('title', 'Twelfth Night');
assert.equal(model.get('title'), 'Twelfth Night'); assert.equal(model.get('title'), 'Twelfth Night');
}); });
QUnit.test('save with non-object success response', function(assert) { QUnit.test('save with non-object success response', function(assert) {
assert.expect(2); assert.expect(2);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.sync = function(method, model, options) { model.sync = function(method, m, options) {
options.success('', options); options.success('', options);
options.success(null, options); options.success(null, options);
}; };
model.save({testing: 'empty'}, { model.save({testing: 'empty'}, {
success: function(model) { success: function(m) {
assert.deepEqual(model.attributes, {testing: 'empty'}); assert.deepEqual(m.attributes, {testing: 'empty'});
} }
}); });
}); });
QUnit.test('save with wait and supplied id', function(assert) { QUnit.test('save with wait and supplied id', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
urlRoot: '/collection' urlRoot: '/collection'
}); });
var model = new Model(); var model = new Model();
model.save({id: 42}, {wait: true}); model.save({id: 42}, {wait: true});
assert.equal(this.ajaxSettings.url, '/collection/42'); assert.equal(this.ajaxSettings.url, '/collection/42');
}); });
QUnit.test('save will pass extra options to success callback', function(assert ) { QUnit.test('save will pass extra options to success callback', function(assert ) {
assert.expect(1); assert.expect(1);
var SpecialSyncModel = Backbone.Model.extend({ var SpecialSyncModel = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, m, options) {
_.extend(options, {specialSync: true}); _.extend(options, {specialSync: true});
return Backbone.Model.prototype.sync.call(this, method, model, options); return Backbone.Model.prototype.sync.call(this, method, m, options);
}, },
urlRoot: '/test' urlRoot: '/test'
}); });
var model = new SpecialSyncModel(); var model = new SpecialSyncModel();
var onSuccess = function(model, response, options) { var onSuccess = function(m, response, options) {
assert.ok(options.specialSync, 'Options were passed correctly to callback' ); assert.ok(options.specialSync, 'Options were passed correctly to callback' );
}; };
model.save(null, {success: onSuccess}); model.save(null, {success: onSuccess});
this.ajaxSettings.success(); this.ajaxSettings.success();
}); });
QUnit.test('fetch', function(assert) { QUnit.test('fetch', function(assert) {
assert.expect(2); assert.expect(2);
doc.fetch(); doc.fetch();
assert.equal(this.syncArgs.method, 'read'); assert.equal(this.syncArgs.method, 'read');
assert.ok(_.isEqual(this.syncArgs.model, doc)); assert.ok(_.isEqual(this.syncArgs.model, doc));
}); });
QUnit.test('fetch will pass extra options to success callback', function(asser t) { QUnit.test('fetch will pass extra options to success callback', function(asser t) {
assert.expect(1); assert.expect(1);
var SpecialSyncModel = Backbone.Model.extend({ var SpecialSyncModel = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, m, options) {
_.extend(options, {specialSync: true}); _.extend(options, {specialSync: true});
return Backbone.Model.prototype.sync.call(this, method, model, options); return Backbone.Model.prototype.sync.call(this, method, m, options);
}, },
urlRoot: '/test' urlRoot: '/test'
}); });
var model = new SpecialSyncModel(); var model = new SpecialSyncModel();
var onSuccess = function(model, response, options) { var onSuccess = function(m, response, options) {
assert.ok(options.specialSync, 'Options were passed correctly to callback' ); assert.ok(options.specialSync, 'Options were passed correctly to callback' );
}; };
model.fetch({success: onSuccess}); model.fetch({success: onSuccess});
this.ajaxSettings.success(); this.ajaxSettings.success();
}); });
QUnit.test('destroy', function(assert) { QUnit.test('destroy', function(assert) {
assert.expect(3); assert.expect(3);
doc.destroy(); doc.destroy();
assert.equal(this.syncArgs.method, 'delete'); assert.equal(this.syncArgs.method, 'delete');
assert.ok(_.isEqual(this.syncArgs.model, doc)); assert.ok(_.isEqual(this.syncArgs.model, doc));
var newModel = new Backbone.Model; var newModel = new Backbone.Model;
assert.equal(newModel.destroy(), false); assert.equal(newModel.destroy(), false);
}); });
QUnit.test('destroy will pass extra options to success callback', function(ass ert) { QUnit.test('destroy will pass extra options to success callback', function(ass ert) {
assert.expect(1); assert.expect(1);
var SpecialSyncModel = Backbone.Model.extend({ var SpecialSyncModel = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, m, options) {
_.extend(options, {specialSync: true}); _.extend(options, {specialSync: true});
return Backbone.Model.prototype.sync.call(this, method, model, options); return Backbone.Model.prototype.sync.call(this, method, m, options);
}, },
urlRoot: '/test' urlRoot: '/test'
}); });
var model = new SpecialSyncModel({id: 'id'}); var model = new SpecialSyncModel({id: 'id'});
var onSuccess = function(model, response, options) { var onSuccess = function(m, response, options) {
assert.ok(options.specialSync, 'Options were passed correctly to callback' ); assert.ok(options.specialSync, 'Options were passed correctly to callback' );
}; };
model.destroy({success: onSuccess}); model.destroy({success: onSuccess});
this.ajaxSettings.success(); this.ajaxSettings.success();
}); });
QUnit.test('non-persisted destroy', function(assert) { QUnit.test('non-persisted destroy', function(assert) {
assert.expect(1); assert.expect(1);
var a = new Backbone.Model({foo: 1, bar: 2, baz: 3}); var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
a.sync = function() { throw 'should not be called'; }; a.sync = function() { throw 'should not be called'; };
a.destroy(); a.destroy();
assert.ok(true, 'non-persisted model should not call sync'); assert.ok(true, 'non-persisted model should not call sync');
}); });
QUnit.test('validate', function(assert) { QUnit.test('validate', function(assert) {
var lastError; var lastError;
var model = new Backbone.Model(); var model = new Backbone.Model();
model.validate = function(attrs) { model.validate = function(attrs) {
if (attrs.admin != this.get('admin')) return "Can't change admin status."; if (attrs.admin !== this.get('admin')) return "Can't change admin status." ;
}; };
model.on('invalid', function(model, error) { model.on('invalid', function(m, error) {
lastError = error; lastError = error;
}); });
var result = model.set({a: 100}); var result = model.set({a: 100});
assert.equal(result, model); assert.equal(result, model);
assert.equal(model.get('a'), 100); assert.equal(model.get('a'), 100);
assert.equal(lastError, undefined); assert.equal(lastError, undefined);
result = model.set({admin: true}); result = model.set({admin: true});
assert.equal(model.get('admin'), true); assert.equal(model.get('admin'), true);
result = model.set({a: 200, admin: false}, {validate: true}); result = model.set({a: 200, admin: false}, {validate: true});
assert.equal(lastError, "Can't change admin status."); assert.equal(lastError, "Can't change admin status.");
skipping to change at line 796 skipping to change at line 830
assert.equal(model.get('name'), undefined); assert.equal(model.get('name'), undefined);
}); });
QUnit.test('validate with error callback', function(assert) { QUnit.test('validate with error callback', function(assert) {
assert.expect(8); assert.expect(8);
var lastError, boundError; var lastError, boundError;
var model = new Backbone.Model(); var model = new Backbone.Model();
model.validate = function(attrs) { model.validate = function(attrs) {
if (attrs.admin) return "Can't change admin status."; if (attrs.admin) return "Can't change admin status.";
}; };
model.on('invalid', function(model, error) { model.on('invalid', function(m, error) {
boundError = true; boundError = true;
}); });
var result = model.set({a: 100}, {validate: true}); var result = model.set({a: 100}, {validate: true});
assert.equal(result, model); assert.equal(result, model);
assert.equal(model.get('a'), 100); assert.equal(model.get('a'), 100);
assert.equal(model.validationError, null); assert.equal(model.validationError, null);
assert.equal(boundError, undefined); assert.equal(boundError, undefined);
result = model.set({a: 200, admin: true}, {validate: true}); result = model.set({a: 200, admin: true}, {validate: true});
assert.equal(result, false); assert.equal(result, false);
assert.equal(model.get('a'), 100); assert.equal(model.get('a'), 100);
skipping to change at line 851 skipping to change at line 885
assert.equal(kid.instancePropSame, adult.instancePropSame); assert.equal(kid.instancePropSame, adult.instancePropSame);
assert.notEqual(kid.instancePropSame, undefined); assert.notEqual(kid.instancePropSame, undefined);
assert.notEqual(Child.prototype.instancePropDiff, Parent.prototype.instanceP ropDiff); assert.notEqual(Child.prototype.instancePropDiff, Parent.prototype.instanceP ropDiff);
assert.notEqual(Child.prototype.instancePropDiff, undefined); assert.notEqual(Child.prototype.instancePropDiff, undefined);
}); });
QUnit.test("Nested change events don't clobber previous attributes", function( assert) { QUnit.test("Nested change events don't clobber previous attributes", function( assert) {
assert.expect(4); assert.expect(4);
new Backbone.Model() new Backbone.Model()
.on('change:state', function(model, newState) { .on('change:state', function(m, newState) {
assert.equal(model.previous('state'), undefined); assert.equal(m.previous('state'), undefined);
assert.equal(newState, 'hello'); assert.equal(newState, 'hello');
// Fire a nested change event. // Fire a nested change event.
model.set({other: 'whatever'}); m.set({other: 'whatever'});
}) })
.on('change:state', function(model, newState) { .on('change:state', function(m, newState) {
assert.equal(model.previous('state'), undefined); assert.equal(m.previous('state'), undefined);
assert.equal(newState, 'hello'); assert.equal(newState, 'hello');
}) })
.set({state: 'hello'}); .set({state: 'hello'});
}); });
QUnit.test('hasChanged/set should use same comparison', function(assert) { QUnit.test('hasChanged/set should use same comparison', function(assert) {
assert.expect(2); assert.expect(2);
var changed = 0, model = new Backbone.Model({a: null}); var changed = 0, model = new Backbone.Model({a: null});
model.on('change', function() { model.on('change', function() {
assert.ok(this.hasChanged('a')); assert.ok(this.hasChanged('a'));
skipping to change at line 992 skipping to change at line 1026
QUnit.test('`hasChanged` for falsey keys', function(assert) { QUnit.test('`hasChanged` for falsey keys', function(assert) {
assert.expect(2); assert.expect(2);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.set({x: true}, {silent: true}); model.set({x: true}, {silent: true});
assert.ok(!model.hasChanged(0)); assert.ok(!model.hasChanged(0));
assert.ok(!model.hasChanged('')); assert.ok(!model.hasChanged(''));
}); });
QUnit.test('`previous` for falsey keys', function(assert) { QUnit.test('`previous` for falsey keys', function(assert) {
assert.expect(2); assert.expect(2);
var model = new Backbone.Model({0: true, '': true}); var model = new Backbone.Model({'0': true, '': true});
model.set({0: false, '': false}, {silent: true}); model.set({'0': false, '': false}, {silent: true});
assert.equal(model.previous(0), true); assert.equal(model.previous(0), true);
assert.equal(model.previous(''), true); assert.equal(model.previous(''), true);
}); });
QUnit.test('`save` with `wait` sends correct attributes', function(assert) { QUnit.test('`save` with `wait` sends correct attributes', function(assert) {
assert.expect(5); assert.expect(5);
var changed = 0; var changed = 0;
var model = new Backbone.Model({x: 1, y: 2}); var model = new Backbone.Model({x: 1, y: 2});
model.url = '/test'; model.url = '/test';
model.on('change:x', function() { changed++; }); model.on('change:x', function() { changed++; });
skipping to change at line 1024 skipping to change at line 1058
assert.expect(1); assert.expect(1);
var model = new Backbone.Model; var model = new Backbone.Model;
model.url = '/test'; model.url = '/test';
model.save({x: 1}, {wait: true}); model.save({x: 1}, {wait: true});
assert.equal(model.get('x'), void 0); assert.equal(model.get('x'), void 0);
}); });
QUnit.test('#1030 - `save` with `wait` results in correct attributes if succes s is called during sync', function(assert) { QUnit.test('#1030 - `save` with `wait` results in correct attributes if succes s is called during sync', function(assert) {
assert.expect(2); assert.expect(2);
var model = new Backbone.Model({x: 1, y: 2}); var model = new Backbone.Model({x: 1, y: 2});
model.sync = function(method, model, options) { model.sync = function(method, m, options) {
options.success(); options.success();
}; };
model.on('change:x', function() { assert.ok(true); }); model.on('change:x', function() { assert.ok(true); });
model.save({x: 3}, {wait: true}); model.save({x: 3}, {wait: true});
assert.equal(model.get('x'), 3); assert.equal(model.get('x'), 3);
}); });
QUnit.test('save with wait validates attributes', function(assert) { QUnit.test('save with wait validates attributes', function(assert) {
var model = new Backbone.Model(); var model = new Backbone.Model();
model.url = '/test'; model.url = '/test';
model.validate = function() { assert.ok(true); }; model.validate = function() { assert.ok(true); };
model.save({x: 1}, {wait: true}); model.save({x: 1}, {wait: true});
}); });
QUnit.test('save turns on parse flag', function(assert) { QUnit.test('save turns on parse flag', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
sync: function(method, model, options) { assert.ok(options.parse); } sync: function(method, m, options) { assert.ok(options.parse); }
}); });
new Model().save(); new Model().save();
}); });
QUnit.test("nested `set` during `'change:attr'`", function(assert) { QUnit.test("nested `set` during `'change:attr'`", function(assert) {
assert.expect(2); assert.expect(2);
var events = []; var events = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('all', function(event) { events.push(event); }); model.on('all', function(event) { events.push(event); });
model.on('change', function() { model.on('change', function() {
skipping to change at line 1145 skipping to change at line 1179
model.set({x: true}); model.set({x: true});
}); });
QUnit.test('multiple nested changes with silent', function(assert) { QUnit.test('multiple nested changes with silent', function(assert) {
assert.expect(1); assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:x', function() { model.on('change:x', function() {
model.set({y: 1}, {silent: true}); model.set({y: 1}, {silent: true});
model.set({y: 2}); model.set({y: 2});
}); });
model.on('change:y', function(model, val) { model.on('change:y', function(m, val) {
assert.equal(val, 2); assert.equal(val, 2);
}); });
model.set({x: true}); model.set({x: true});
}); });
QUnit.test('multiple nested changes with silent', function(assert) { QUnit.test('multiple nested changes with silent', function(assert) {
assert.expect(1); assert.expect(1);
var changes = []; var changes = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:b', function(model, val) { changes.push(val); }); model.on('change:b', function(m, val) { changes.push(val); });
model.on('change', function() { model.on('change', function() {
model.set({b: 1}); model.set({b: 1});
}); });
model.set({b: 0}); model.set({b: 0});
assert.deepEqual(changes, [0, 1]); assert.deepEqual(changes, [0, 1]);
}); });
QUnit.test('basic silent change semantics', function(assert) { QUnit.test('basic silent change semantics', function(assert) {
assert.expect(1); assert.expect(1);
var model = new Backbone.Model; var model = new Backbone.Model;
skipping to change at line 1205 skipping to change at line 1239
var model = new Backbone.Model(); var model = new Backbone.Model();
var options = {}; var options = {};
model.unset('x', options); model.unset('x', options);
assert.ok(!options.unset); assert.ok(!options.unset);
}); });
QUnit.test('#1355 - `options` is passed to success callbacks', function(assert ) { QUnit.test('#1355 - `options` is passed to success callbacks', function(assert ) {
assert.expect(3); assert.expect(3);
var model = new Backbone.Model(); var model = new Backbone.Model();
var opts = { var opts = {
success: function( model, resp, options ) { success: function( m, resp, options ) {
assert.ok(options); assert.ok(options);
} }
}; };
model.sync = function(method, model, options) { model.sync = function(method, m, options) {
options.success(); options.success();
}; };
model.save({id: 1}, opts); model.save({id: 1}, opts);
model.fetch(opts); model.fetch(opts);
model.destroy(opts); model.destroy(opts);
}); });
QUnit.test("#1412 - Trigger 'sync' event.", function(assert) { QUnit.test("#1412 - Trigger 'sync' event.", function(assert) {
assert.expect(3); assert.expect(3);
var model = new Backbone.Model({id: 1}); var model = new Backbone.Model({id: 1});
model.sync = function(method, model, options) { options.success(); }; model.sync = function(method, m, options) { options.success(); };
model.on('sync', function(){ assert.ok(true); }); model.on('sync', function(){ assert.ok(true); });
model.fetch(); model.fetch();
model.save(); model.save();
model.destroy(); model.destroy();
}); });
QUnit.test('#1365 - Destroy: New models execute success callback.', function(a ssert) { QUnit.test('#1365 - Destroy: New models execute success callback.', function(a ssert) {
var done = assert.async(); var done = assert.async();
assert.expect(2); assert.expect(2);
new Backbone.Model() new Backbone.Model()
skipping to change at line 1251 skipping to change at line 1285
var model = new Backbone.Model; var model = new Backbone.Model;
model.validate = function(){ return 'invalid'; }; model.validate = function(){ return 'invalid'; };
model.sync = function(){ assert.ok(false); }; model.sync = function(){ assert.ok(false); };
assert.strictEqual(model.save(), false); assert.strictEqual(model.save(), false);
}); });
QUnit.test("#1377 - Save without attrs triggers 'error'.", function(assert) { QUnit.test("#1377 - Save without attrs triggers 'error'.", function(assert) {
assert.expect(1); assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
url: '/test/', url: '/test/',
sync: function(method, model, options){ options.success(); }, sync: function(method, m, options){ options.success(); },
validate: function(){ return 'invalid'; } validate: function(){ return 'invalid'; }
}); });
var model = new Model({id: 1}); var model = new Model({id: 1});
model.on('invalid', function(){ assert.ok(true); }); model.on('invalid', function(){ assert.ok(true); });
model.save(); model.save();
}); });
QUnit.test('#1545 - `undefined` can be passed to a model constructor without c oersion', function(assert) { QUnit.test('#1545 - `undefined` can be passed to a model constructor without c oersion', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
defaults: {one: 1}, defaults: {one: 1},
skipping to change at line 1274 skipping to change at line 1308
} }
}); });
var emptyattrs = new Model(); var emptyattrs = new Model();
var undefinedattrs = new Model(undefined); var undefinedattrs = new Model(undefined);
}); });
QUnit.test('#1478 - Model `save` does not trigger change on unchanged attribut es', function(assert) { QUnit.test('#1478 - Model `save` does not trigger change on unchanged attribut es', function(assert) {
var done = assert.async(); var done = assert.async();
assert.expect(0); assert.expect(0);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, m, options) {
setTimeout(function(){ setTimeout(function(){
options.success(); options.success();
done(); done();
}, 0); }, 0);
} }
}); });
new Model({x: true}) new Model({x: true})
.on('change:x', function(){ assert.ok(false); }) .on('change:x', function(){ assert.ok(false); })
.save(null, {wait: true}); .save(null, {wait: true});
}); });
skipping to change at line 1304 skipping to change at line 1338
QUnit.test('#1664 - multiple silent changes nested inside a change event', fun ction(assert) { QUnit.test('#1664 - multiple silent changes nested inside a change event', fun ction(assert) {
assert.expect(2); assert.expect(2);
var changes = []; var changes = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change', function() { model.on('change', function() {
model.set({a: 'c'}, {silent: true}); model.set({a: 'c'}, {silent: true});
model.set({b: 2}, {silent: true}); model.set({b: 2}, {silent: true});
model.unset('c', {silent: true}); model.unset('c', {silent: true});
}); });
model.on('change:a change:b change:c', function(model, val) { changes.push(v al); }); model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
model.set({a: 'a', b: 1, c: 'item'}); model.set({a: 'a', b: 1, c: 'item'});
assert.deepEqual(changes, ['a', 1, 'item']); assert.deepEqual(changes, ['a', 1, 'item']);
assert.deepEqual(model.attributes, {a: 'c', b: 2}); assert.deepEqual(model.attributes, {a: 'c', b: 2});
}); });
QUnit.test('#1791 - `attributes` is available for `parse`', function(assert) { QUnit.test('#1791 - `attributes` is available for `parse`', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
parse: function() { this.has('a'); } // shouldn't throw an error parse: function() { this.has('a'); } // shouldn't throw an error
}); });
var model = new Model(null, {parse: true}); var model = new Model(null, {parse: true});
assert.expect(0); assert.expect(0);
}); });
QUnit.test('silent changes in last `change` event back to original triggers ch ange', function(assert) { QUnit.test('silent changes in last `change` event back to original triggers ch ange', function(assert) {
assert.expect(2); assert.expect(2);
var changes = []; var changes = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:a change:b change:c', function(model, val) { changes.push(v al); }); model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
model.on('change', function() { model.on('change', function() {
model.set({a: 'c'}, {silent: true}); model.set({a: 'c'}, {silent: true});
}); });
model.set({a: 'a'}); model.set({a: 'a'});
assert.deepEqual(changes, ['a']); assert.deepEqual(changes, ['a']);
model.set({a: 'a'}); model.set({a: 'a'});
assert.deepEqual(changes, ['a', 'a']); assert.deepEqual(changes, ['a', 'a']);
}); });
QUnit.test('#1943 change calculations should use _.isEqual', function(assert) { QUnit.test('#1943 change calculations should use _.isEqual', function(assert) {
skipping to change at line 1413 skipping to change at line 1447
QUnit.test('#3778 - id will only be updated if it is set', function(assert) { QUnit.test('#3778 - id will only be updated if it is set', function(assert) {
assert.expect(2); assert.expect(2);
var model = new Backbone.Model({id: 1}); var model = new Backbone.Model({id: 1});
model.id = 2; model.id = 2;
model.set({foo: 'bar'}); model.set({foo: 'bar'});
assert.equal(model.id, 2); assert.equal(model.id, 2);
model.set({id: 3}); model.set({id: 3});
assert.equal(model.id, 3); assert.equal(model.id, 3);
}); });
})(); })(QUnit);
 End of changes. 54 change blocks. 
65 lines changed or deleted 101 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)