3 var ProxyModel = Backbone.Model.extend();
4 var Klass = Backbone.Collection.extend({
5 url: function() { return '/collection'; }
9 QUnit.module('Backbone.Model', {
11 beforeEach: function(assert) {
12 doc = new ProxyModel({
15 author: 'Bill Shakespeare',
18 collection = new Klass();
24 QUnit.test('initialize', function(assert) {
26 var Model = Backbone.Model.extend({
27 initialize: function() {
29 assert.equal(this.collection, collection);
32 var model = new Model({}, {collection: collection});
33 assert.equal(model.one, 1);
34 assert.equal(model.collection, collection);
37 QUnit.test('Object.prototype properties are overridden by attributes', function(assert) {
39 var model = new Backbone.Model({hasOwnProperty: true});
40 assert.equal(model.get('hasOwnProperty'), true);
43 QUnit.test('initialize with attributes and options', function(assert) {
45 var Model = Backbone.Model.extend({
46 initialize: function(attributes, options) {
47 this.one = options.one;
50 var model = new Model({}, {one: 1});
51 assert.equal(model.one, 1);
54 QUnit.test('initialize with parsed attributes', function(assert) {
56 var Model = Backbone.Model.extend({
57 parse: function(attrs) {
62 var model = new Model({value: 1}, {parse: true});
63 assert.equal(model.get('value'), 2);
67 QUnit.test('preinitialize', function(assert) {
69 var Model = Backbone.Model.extend({
71 preinitialize: function() {
75 var model = new Model({}, {collection: collection});
76 assert.equal(model.one, 1);
77 assert.equal(model.collection, collection);
80 QUnit.test('preinitialize occurs before the model is set up', function(assert) {
82 var Model = Backbone.Model.extend({
84 preinitialize: function() {
85 assert.equal(this.collection, undefined);
86 assert.equal(this.cid, undefined);
87 assert.equal(this.id, undefined);
90 var model = new Model({id: 'foo'}, {collection: collection});
91 assert.equal(model.collection, collection);
92 assert.equal(model.id, 'foo');
93 assert.notEqual(model.cid, undefined);
96 QUnit.test('parse can return null', function(assert) {
98 var Model = Backbone.Model.extend({
99 parse: function(attrs) {
104 var model = new Model({value: 1}, {parse: true});
105 assert.equal(JSON.stringify(model.toJSON()), '{}');
108 QUnit.test('url', function(assert) {
111 assert.equal(doc.url(), '/collection/1-the-tempest');
112 doc.collection.url = '/collection/';
113 assert.equal(doc.url(), '/collection/1-the-tempest');
114 doc.collection = null;
115 assert.raises(function() { doc.url(); });
116 doc.collection = collection;
119 QUnit.test('url when using urlRoot, and uri encoding', function(assert) {
121 var Model = Backbone.Model.extend({
122 urlRoot: '/collection'
124 var model = new Model();
125 assert.equal(model.url(), '/collection');
126 model.set({id: '+1+'});
127 assert.equal(model.url(), '/collection/%2B1%2B');
130 QUnit.test('url when using urlRoot as a function to determine urlRoot at runtime', function(assert) {
132 var Model = Backbone.Model.extend({
133 urlRoot: function() {
134 return '/nested/' + this.get('parentId') + '/collection';
138 var model = new Model({parentId: 1});
139 assert.equal(model.url(), '/nested/1/collection');
141 assert.equal(model.url(), '/nested/1/collection/2');
144 QUnit.test('underscore methods', function(assert) {
146 var model = new Backbone.Model({foo: 'a', bar: 'b', baz: 'c'});
147 var model2 = model.clone();
148 assert.deepEqual(model.keys(), ['foo', 'bar', 'baz']);
149 assert.deepEqual(model.values(), ['a', 'b', 'c']);
150 assert.deepEqual(model.invert(), {a: 'foo', b: 'bar', c: 'baz'});
151 assert.deepEqual(model.pick('foo', 'baz'), {foo: 'a', baz: 'c'});
152 assert.deepEqual(model.omit('foo', 'bar'), {baz: 'c'});
155 QUnit.test('chain', function(assert) {
156 var model = new Backbone.Model({a: 0, b: 1, c: 2});
157 assert.deepEqual(model.chain().pick('a', 'b', 'c').values().compact().value(), [1, 2]);
160 QUnit.test('clone', function(assert) {
162 var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
164 assert.equal(a.get('foo'), 1);
165 assert.equal(a.get('bar'), 2);
166 assert.equal(a.get('baz'), 3);
167 assert.equal(b.get('foo'), a.get('foo'), 'Foo should be the same on the clone.');
168 assert.equal(b.get('bar'), a.get('bar'), 'Bar should be the same on the clone.');
169 assert.equal(b.get('baz'), a.get('baz'), 'Baz should be the same on the clone.');
171 assert.equal(a.get('foo'), 100);
172 assert.equal(b.get('foo'), 1, 'Changing a parent attribute does not change the clone.');
174 var foo = new Backbone.Model({p: 1});
175 var bar = new Backbone.Model({p: 2});
176 bar.set(foo.clone().attributes, {unset: true});
177 assert.equal(foo.get('p'), 1);
178 assert.equal(bar.get('p'), undefined);
181 QUnit.test('isNew', function(assert) {
183 var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
184 assert.ok(a.isNew(), 'it should be new');
185 a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: -5});
186 assert.ok(!a.isNew(), 'any defined ID is legal, negative or positive');
187 a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: 0});
188 assert.ok(!a.isNew(), 'any defined ID is legal, including zero');
189 assert.ok(new Backbone.Model().isNew(), 'is true when there is no id');
190 assert.ok(!new Backbone.Model({id: 2}).isNew(), 'is false for a positive integer');
191 assert.ok(!new Backbone.Model({id: -5}).isNew(), 'is false for a negative integer');
194 QUnit.test('get', function(assert) {
196 assert.equal(doc.get('title'), 'The Tempest');
197 assert.equal(doc.get('author'), 'Bill Shakespeare');
200 QUnit.test('escape', function(assert) {
202 assert.equal(doc.escape('title'), 'The Tempest');
203 doc.set({audience: 'Bill & Bob'});
204 assert.equal(doc.escape('audience'), 'Bill & Bob');
205 doc.set({audience: 'Tim > Joan'});
206 assert.equal(doc.escape('audience'), 'Tim > Joan');
207 doc.set({audience: 10101});
208 assert.equal(doc.escape('audience'), '10101');
209 doc.unset('audience');
210 assert.equal(doc.escape('audience'), '');
213 QUnit.test('has', function(assert) {
215 var model = new Backbone.Model();
217 assert.strictEqual(model.has('name'), false);
227 'undefined': undefined
230 assert.strictEqual(model.has('0'), true);
231 assert.strictEqual(model.has('1'), true);
232 assert.strictEqual(model.has('true'), true);
233 assert.strictEqual(model.has('false'), true);
234 assert.strictEqual(model.has('empty'), true);
235 assert.strictEqual(model.has('name'), true);
239 assert.strictEqual(model.has('name'), false);
240 assert.strictEqual(model.has('null'), false);
241 assert.strictEqual(model.has('undefined'), false);
244 QUnit.test('matches', function(assert) {
246 var model = new Backbone.Model();
248 assert.strictEqual(model.matches({name: 'Jonas', cool: true}), false);
250 model.set({name: 'Jonas', cool: true});
252 assert.strictEqual(model.matches({name: 'Jonas'}), true);
253 assert.strictEqual(model.matches({name: 'Jonas', cool: true}), true);
254 assert.strictEqual(model.matches({name: 'Jonas', cool: false}), false);
257 QUnit.test('matches with predicate', function(assert) {
258 var model = new Backbone.Model({a: 0});
260 assert.strictEqual(model.matches(function(attr) {
261 return attr.a > 1 && attr.b != null;
264 model.set({a: 3, b: true});
266 assert.strictEqual(model.matches(function(attr) {
267 return attr.a > 1 && attr.b != null;
271 QUnit.test('set and unset', function(assert) {
273 var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
275 a.on('change:foo', function() { changeCount += 1; });
277 assert.equal(a.get('foo'), 2, 'Foo should have changed.');
278 assert.equal(changeCount, 1, 'Change count should have incremented.');
279 // set with value that is not new shouldn't fire change event
281 assert.equal(a.get('foo'), 2, 'Foo should NOT have changed, still 2');
282 assert.equal(changeCount, 1, 'Change count should NOT have incremented.');
284 a.validate = function(attrs) {
285 assert.equal(attrs.foo, void 0, 'validate:true passed while unsetting');
287 a.unset('foo', {validate: true});
288 assert.equal(a.get('foo'), void 0, 'Foo should have changed');
290 assert.equal(changeCount, 2, 'Change count should have incremented for unset.');
293 assert.equal(a.id, undefined, 'Unsetting the id should remove the id property.');
296 QUnit.test('#2030 - set with failed validate, followed by another set triggers change', function(assert) {
297 var attr = 0, main = 0, error = 0;
298 var Model = Backbone.Model.extend({
299 validate: function(attrs) {
302 return 'this is an error';
306 var model = new Model({x: 0});
307 model.on('change:x', function() { attr++; });
308 model.on('change', function() { main++; });
309 model.set({x: 2}, {validate: true});
310 model.set({x: 1}, {validate: true});
311 assert.deepEqual([attr, main, error], [1, 1, 1]);
314 QUnit.test('set triggers changes in the correct order', function(assert) {
316 var model = new Backbone.Model;
317 model.on('last', function(){ value = 'last'; });
318 model.on('first', function(){ value = 'first'; });
319 model.trigger('first');
320 model.trigger('last');
321 assert.equal(value, 'last');
324 QUnit.test('set falsy values in the correct order', function(assert) {
326 var model = new Backbone.Model({result: 'result'});
327 model.on('change', function() {
328 assert.equal(model.changed.result, void 0);
329 assert.equal(model.previous('result'), false);
331 model.set({result: void 0}, {silent: true});
332 model.set({result: null}, {silent: true});
333 model.set({result: false}, {silent: true});
334 model.set({result: void 0});
337 QUnit.test('nested set triggers with the correct options', function(assert) {
338 var model = new Backbone.Model();
342 model.on('change', function(__, options) {
343 switch (model.get('a')) {
345 assert.equal(options, o1);
346 return model.set('a', 2, o2);
348 assert.equal(options, o2);
349 return model.set('a', 3, o3);
351 assert.equal(options, o3);
354 model.set('a', 1, o1);
357 QUnit.test('multiple unsets', function(assert) {
360 var counter = function(){ i++; };
361 var model = new Backbone.Model({a: 1});
362 model.on('change:a', counter);
366 assert.equal(i, 2, 'Unset does not fire an event for missing attributes.');
369 QUnit.test('unset and changedAttributes', function(assert) {
371 var model = new Backbone.Model({a: 1});
372 model.on('change', function() {
373 assert.ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties');
378 QUnit.test('using a non-default id attribute.', function(assert) {
380 var MongoModel = Backbone.Model.extend({idAttribute: '_id'});
381 var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
382 assert.equal(model.get('id'), 'eye-dee');
383 assert.equal(model.id, 25);
384 assert.equal(model.isNew(), false);
386 assert.equal(model.id, undefined);
387 assert.equal(model.isNew(), true);
390 QUnit.test('setting an alternative cid prefix', function(assert) {
392 var Model = Backbone.Model.extend({
395 var model = new Model();
397 assert.equal(model.cid.charAt(0), 'm');
399 model = new Backbone.Model();
400 assert.equal(model.cid.charAt(0), 'c');
402 var Collection = Backbone.Collection.extend({
405 var col = new Collection([{id: 'c5'}, {id: 'c6'}, {id: 'c7'}]);
407 assert.equal(col.get('c6').cid.charAt(0), 'm');
408 col.set([{id: 'c6', value: 'test'}], {
413 assert.ok(col.get('c6').has('value'));
416 QUnit.test('set an empty string', function(assert) {
418 var model = new Backbone.Model({name: 'Model'});
419 model.set({name: ''});
420 assert.equal(model.get('name'), '');
423 QUnit.test('setting an object', function(assert) {
425 var model = new Backbone.Model({
428 model.on('change', function() {
432 custom: {foo: 1} // no change should be fired
435 custom: {foo: 2} // change event should be fired
439 QUnit.test('clear', function(assert) {
442 var model = new Backbone.Model({id: 1, name: 'Model'});
443 model.on('change:name', function(){ changed = true; });
444 model.on('change', function() {
445 var changedAttrs = model.changedAttributes();
446 assert.ok('name' in changedAttrs);
449 assert.equal(changed, true);
450 assert.equal(model.get('name'), undefined);
453 QUnit.test('defaults', function(assert) {
455 var Defaulted = Backbone.Model.extend({
461 var model = new Defaulted({two: undefined});
462 assert.equal(model.get('one'), 1);
463 assert.equal(model.get('two'), 2);
464 model = new Defaulted({two: 3});
465 assert.equal(model.get('one'), 1);
466 assert.equal(model.get('two'), 3);
467 Defaulted = Backbone.Model.extend({
468 defaults: function() {
475 model = new Defaulted({two: undefined});
476 assert.equal(model.get('one'), 3);
477 assert.equal(model.get('two'), 4);
478 Defaulted = Backbone.Model.extend({
479 defaults: {hasOwnProperty: true}
481 model = new Defaulted();
482 assert.equal(model.get('hasOwnProperty'), true);
483 model = new Defaulted({hasOwnProperty: undefined});
484 assert.equal(model.get('hasOwnProperty'), true);
485 model = new Defaulted({hasOwnProperty: false});
486 assert.equal(model.get('hasOwnProperty'), false);
489 QUnit.test('change, hasChanged, changedAttributes, previous, previousAttributes', function(assert) {
491 var model = new Backbone.Model({name: 'Tim', age: 10});
492 assert.deepEqual(model.changedAttributes(), false);
493 model.on('change', function() {
494 assert.ok(model.hasChanged('name'), 'name changed');
495 assert.ok(!model.hasChanged('age'), 'age did not');
496 assert.ok(_.isEqual(model.changedAttributes(), {name: 'Rob'}), 'changedAttributes returns the changed attrs');
497 assert.equal(model.previous('name'), 'Tim');
498 assert.ok(_.isEqual(model.previousAttributes(), {name: 'Tim', age: 10}), 'previousAttributes is correct');
500 assert.equal(model.hasChanged(), false);
501 assert.equal(model.hasChanged(undefined), false);
502 model.set({name: 'Rob'});
503 assert.equal(model.get('name'), 'Rob');
506 QUnit.test('changedAttributes', function(assert) {
508 var model = new Backbone.Model({a: 'a', b: 'b'});
509 assert.deepEqual(model.changedAttributes(), false);
510 assert.equal(model.changedAttributes({a: 'a'}), false);
511 assert.equal(model.changedAttributes({a: 'b'}).a, 'b');
514 QUnit.test('change with options', function(assert) {
517 var model = new Backbone.Model({name: 'Rob'});
518 model.on('change', function(m, options) {
519 value = options.prefix + m.get('name');
521 model.set({name: 'Bob'}, {prefix: 'Mr. '});
522 assert.equal(value, 'Mr. Bob');
523 model.set({name: 'Sue'}, {prefix: 'Ms. '});
524 assert.equal(value, 'Ms. Sue');
527 QUnit.test('change after initialize', function(assert) {
530 var attrs = {id: 1, label: 'c'};
531 var obj = new Backbone.Model(attrs);
532 obj.on('change', function() { changed += 1; });
534 assert.equal(changed, 0);
537 QUnit.test('save within change event', function(assert) {
540 var model = new Backbone.Model({firstName: 'Taylor', lastName: 'Swift'});
542 model.on('change', function() {
544 assert.ok(_.isEqual(env.syncArgs.model, model));
546 model.set({lastName: 'Hicks'});
549 QUnit.test('validate after save', function(assert) {
551 var lastError, model = new Backbone.Model();
552 model.validate = function(attrs) {
553 if (attrs.admin) return "Can't change admin status.";
555 model.sync = function(method, m, options) {
556 options.success.call(this, {admin: true});
558 model.on('invalid', function(m, error) {
563 assert.equal(lastError, "Can't change admin status.");
564 assert.equal(model.validationError, "Can't change admin status.");
567 QUnit.test('save', function(assert) {
569 doc.save({title: 'Henry V'});
570 assert.equal(this.syncArgs.method, 'update');
571 assert.ok(_.isEqual(this.syncArgs.model, doc));
574 QUnit.test('save, fetch, destroy triggers error event when an error occurs', function(assert) {
576 var model = new Backbone.Model();
577 model.on('error', function() {
580 model.sync = function(method, m, options) {
583 model.save({data: 2, id: 1});
588 QUnit.test('#3283 - save, fetch, destroy calls success with context', function(assert) {
590 var model = new Backbone.Model();
594 success: function() {
595 assert.equal(this, obj);
598 model.sync = function(method, m, opts) {
599 opts.success.call(opts.context);
601 model.save({data: 2, id: 1}, options);
602 model.fetch(options);
603 model.destroy(options);
606 QUnit.test('#3283 - save, fetch, destroy calls error with context', function(assert) {
608 var model = new Backbone.Model();
613 assert.equal(this, obj);
616 model.sync = function(method, m, opts) {
617 opts.error.call(opts.context);
619 model.save({data: 2, id: 1}, options);
620 model.fetch(options);
621 model.destroy(options);
624 QUnit.test('#3470 - save and fetch with parse false', function(assert) {
627 var model = new Backbone.Model();
628 model.parse = function() {
631 model.sync = function(method, m, options) {
632 options.success({i: ++i});
634 model.fetch({parse: false});
635 assert.equal(model.get('i'), i);
636 model.save(null, {parse: false});
637 assert.equal(model.get('i'), i);
640 QUnit.test('save with PATCH', function(assert) {
641 doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
643 assert.equal(this.syncArgs.method, 'update');
644 assert.equal(this.syncArgs.options.attrs, undefined);
646 doc.save({b: 2, d: 4}, {patch: true});
647 assert.equal(this.syncArgs.method, 'patch');
648 assert.equal(_.size(this.syncArgs.options.attrs), 2);
649 assert.equal(this.syncArgs.options.attrs.d, 4);
650 assert.equal(this.syncArgs.options.attrs.a, undefined);
651 assert.equal(this.ajaxSettings.data, '{"b":2,"d":4}');
654 QUnit.test('save with PATCH and different attrs', function(assert) {
655 doc.clear().save({b: 2, d: 4}, {patch: true, attrs: {B: 1, D: 3}});
656 assert.equal(this.syncArgs.options.attrs.D, 3);
657 assert.equal(this.syncArgs.options.attrs.d, undefined);
658 assert.equal(this.ajaxSettings.data, '{"B":1,"D":3}');
659 assert.deepEqual(doc.attributes, {b: 2, d: 4});
662 QUnit.test('save in positional style', function(assert) {
664 var model = new Backbone.Model();
665 model.sync = function(method, m, options) {
668 model.save('title', 'Twelfth Night');
669 assert.equal(model.get('title'), 'Twelfth Night');
672 QUnit.test('save with non-object success response', function(assert) {
674 var model = new Backbone.Model();
675 model.sync = function(method, m, options) {
676 options.success('', options);
677 options.success(null, options);
679 model.save({testing: 'empty'}, {
680 success: function(m) {
681 assert.deepEqual(m.attributes, {testing: 'empty'});
686 QUnit.test('save with wait and supplied id', function(assert) {
687 var Model = Backbone.Model.extend({
688 urlRoot: '/collection'
690 var model = new Model();
691 model.save({id: 42}, {wait: true});
692 assert.equal(this.ajaxSettings.url, '/collection/42');
695 QUnit.test('save will pass extra options to success callback', function(assert) {
697 var SpecialSyncModel = Backbone.Model.extend({
698 sync: function(method, m, options) {
699 _.extend(options, {specialSync: true});
700 return Backbone.Model.prototype.sync.call(this, method, m, options);
705 var model = new SpecialSyncModel();
707 var onSuccess = function(m, response, options) {
708 assert.ok(options.specialSync, 'Options were passed correctly to callback');
711 model.save(null, {success: onSuccess});
712 this.ajaxSettings.success();
715 QUnit.test('fetch', function(assert) {
718 assert.equal(this.syncArgs.method, 'read');
719 assert.ok(_.isEqual(this.syncArgs.model, doc));
722 QUnit.test('fetch will pass extra options to success callback', function(assert) {
724 var SpecialSyncModel = Backbone.Model.extend({
725 sync: function(method, m, options) {
726 _.extend(options, {specialSync: true});
727 return Backbone.Model.prototype.sync.call(this, method, m, options);
732 var model = new SpecialSyncModel();
734 var onSuccess = function(m, response, options) {
735 assert.ok(options.specialSync, 'Options were passed correctly to callback');
738 model.fetch({success: onSuccess});
739 this.ajaxSettings.success();
742 QUnit.test('destroy', function(assert) {
745 assert.equal(this.syncArgs.method, 'delete');
746 assert.ok(_.isEqual(this.syncArgs.model, doc));
748 var newModel = new Backbone.Model;
749 assert.equal(newModel.destroy(), false);
752 QUnit.test('destroy will pass extra options to success callback', function(assert) {
754 var SpecialSyncModel = Backbone.Model.extend({
755 sync: function(method, m, options) {
756 _.extend(options, {specialSync: true});
757 return Backbone.Model.prototype.sync.call(this, method, m, options);
762 var model = new SpecialSyncModel({id: 'id'});
764 var onSuccess = function(m, response, options) {
765 assert.ok(options.specialSync, 'Options were passed correctly to callback');
768 model.destroy({success: onSuccess});
769 this.ajaxSettings.success();
772 QUnit.test('non-persisted destroy', function(assert) {
774 var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
775 a.sync = function() { throw 'should not be called'; };
777 assert.ok(true, 'non-persisted model should not call sync');
780 QUnit.test('validate', function(assert) {
782 var model = new Backbone.Model();
783 model.validate = function(attrs) {
784 if (attrs.admin !== this.get('admin')) return "Can't change admin status.";
786 model.on('invalid', function(m, error) {
789 var result = model.set({a: 100});
790 assert.equal(result, model);
791 assert.equal(model.get('a'), 100);
792 assert.equal(lastError, undefined);
793 result = model.set({admin: true});
794 assert.equal(model.get('admin'), true);
795 result = model.set({a: 200, admin: false}, {validate: true});
796 assert.equal(lastError, "Can't change admin status.");
797 assert.equal(result, false);
798 assert.equal(model.get('a'), 100);
801 QUnit.test('validate on unset and clear', function(assert) {
804 var model = new Backbone.Model({name: 'One'});
805 model.validate = function(attrs) {
811 model.set({name: 'Two'});
812 assert.equal(model.get('name'), 'Two');
813 assert.equal(error, undefined);
814 model.unset('name', {validate: true});
815 assert.equal(error, true);
816 assert.equal(model.get('name'), 'Two');
817 model.clear({validate: true});
818 assert.equal(model.get('name'), 'Two');
819 delete model.validate;
821 assert.equal(model.get('name'), undefined);
824 QUnit.test('validate with error callback', function(assert) {
826 var lastError, boundError;
827 var model = new Backbone.Model();
828 model.validate = function(attrs) {
829 if (attrs.admin) return "Can't change admin status.";
831 model.on('invalid', function(m, error) {
834 var result = model.set({a: 100}, {validate: true});
835 assert.equal(result, model);
836 assert.equal(model.get('a'), 100);
837 assert.equal(model.validationError, null);
838 assert.equal(boundError, undefined);
839 result = model.set({a: 200, admin: true}, {validate: true});
840 assert.equal(result, false);
841 assert.equal(model.get('a'), 100);
842 assert.equal(model.validationError, "Can't change admin status.");
843 assert.equal(boundError, true);
846 QUnit.test('defaults always extend attrs (#459)', function(assert) {
848 var Defaulted = Backbone.Model.extend({
850 initialize: function(attrs, opts) {
851 assert.equal(this.attributes.one, 1);
854 var providedattrs = new Defaulted({});
855 var emptyattrs = new Defaulted();
858 QUnit.test('Inherit class properties', function(assert) {
860 var Parent = Backbone.Model.extend({
861 instancePropSame: function() {},
862 instancePropDiff: function() {}
864 classProp: function() {}
866 var Child = Parent.extend({
867 instancePropDiff: function() {}
870 var adult = new Parent;
873 assert.equal(Child.classProp, Parent.classProp);
874 assert.notEqual(Child.classProp, undefined);
876 assert.equal(kid.instancePropSame, adult.instancePropSame);
877 assert.notEqual(kid.instancePropSame, undefined);
879 assert.notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
880 assert.notEqual(Child.prototype.instancePropDiff, undefined);
883 QUnit.test("Nested change events don't clobber previous attributes", function(assert) {
886 .on('change:state', function(m, newState) {
887 assert.equal(m.previous('state'), undefined);
888 assert.equal(newState, 'hello');
889 // Fire a nested change event.
890 m.set({other: 'whatever'});
892 .on('change:state', function(m, newState) {
893 assert.equal(m.previous('state'), undefined);
894 assert.equal(newState, 'hello');
896 .set({state: 'hello'});
899 QUnit.test('hasChanged/set should use same comparison', function(assert) {
901 var changed = 0, model = new Backbone.Model({a: null});
902 model.on('change', function() {
903 assert.ok(this.hasChanged('a'));
905 .on('change:a', function() {
908 .set({a: undefined});
909 assert.equal(changed, 1);
912 QUnit.test('#582, #425, change:attribute callbacks should fire after all changes have occurred', function(assert) {
914 var model = new Backbone.Model;
916 var assertion = function() {
917 assert.equal(model.get('a'), 'a');
918 assert.equal(model.get('b'), 'b');
919 assert.equal(model.get('c'), 'c');
922 model.on('change:a', assertion);
923 model.on('change:b', assertion);
924 model.on('change:c', assertion);
926 model.set({a: 'a', b: 'b', c: 'c'});
929 QUnit.test('#871, set with attributes property', function(assert) {
931 var model = new Backbone.Model();
932 model.set({attributes: true});
933 assert.ok(model.has('attributes'));
936 QUnit.test('set value regardless of equality/change', function(assert) {
938 var model = new Backbone.Model({x: []});
941 assert.ok(model.get('x') === a);
944 QUnit.test('set same value does not trigger change', function(assert) {
946 var model = new Backbone.Model({x: 1});
947 model.on('change change:x', function() { assert.ok(false); });
952 QUnit.test('unset does not fire a change for undefined attributes', function(assert) {
954 var model = new Backbone.Model({x: undefined});
955 model.on('change:x', function(){ assert.ok(false); });
959 QUnit.test('set: undefined values', function(assert) {
961 var model = new Backbone.Model({x: undefined});
962 assert.ok('x' in model.attributes);
965 QUnit.test('hasChanged works outside of change events, and true within', function(assert) {
967 var model = new Backbone.Model({x: 1});
968 model.on('change:x', function() {
969 assert.ok(model.hasChanged('x'));
970 assert.equal(model.get('x'), 1);
972 model.set({x: 2}, {silent: true});
973 assert.ok(model.hasChanged());
974 assert.equal(model.hasChanged('x'), true);
976 assert.ok(model.hasChanged());
977 assert.equal(model.hasChanged('x'), true);
980 QUnit.test('hasChanged gets cleared on the following set', function(assert) {
982 var model = new Backbone.Model;
984 assert.ok(model.hasChanged());
986 assert.ok(!model.hasChanged());
988 assert.ok(model.hasChanged());
990 assert.ok(!model.hasChanged());
993 QUnit.test('save with `wait` succeeds without `validate`', function(assert) {
995 var model = new Backbone.Model();
997 model.save({x: 1}, {wait: true});
998 assert.ok(this.syncArgs.model === model);
1001 QUnit.test("save without `wait` doesn't set invalid attributes", function(assert) {
1002 var model = new Backbone.Model();
1003 model.validate = function() { return 1; };
1005 assert.equal(model.get('a'), void 0);
1008 QUnit.test("save doesn't validate twice", function(assert) {
1009 var model = new Backbone.Model();
1011 model.sync = function() {};
1012 model.validate = function() { ++times; };
1014 assert.equal(times, 1);
1017 QUnit.test('`hasChanged` for falsey keys', function(assert) {
1019 var model = new Backbone.Model();
1020 model.set({x: true}, {silent: true});
1021 assert.ok(!model.hasChanged(0));
1022 assert.ok(!model.hasChanged(''));
1025 QUnit.test('`previous` for falsey keys', function(assert) {
1027 var model = new Backbone.Model({'0': true, '': true});
1028 model.set({'0': false, '': false}, {silent: true});
1029 assert.equal(model.previous(0), true);
1030 assert.equal(model.previous(''), true);
1033 QUnit.test('`save` with `wait` sends correct attributes', function(assert) {
1036 var model = new Backbone.Model({x: 1, y: 2});
1037 model.url = '/test';
1038 model.on('change:x', function() { changed++; });
1039 model.save({x: 3}, {wait: true});
1040 assert.deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
1041 assert.equal(model.get('x'), 1);
1042 assert.equal(changed, 0);
1043 this.syncArgs.options.success({});
1044 assert.equal(model.get('x'), 3);
1045 assert.equal(changed, 1);
1048 QUnit.test("a failed `save` with `wait` doesn't leave attributes behind", function(assert) {
1050 var model = new Backbone.Model;
1051 model.url = '/test';
1052 model.save({x: 1}, {wait: true});
1053 assert.equal(model.get('x'), void 0);
1056 QUnit.test('#1030 - `save` with `wait` results in correct attributes if success is called during sync', function(assert) {
1058 var model = new Backbone.Model({x: 1, y: 2});
1059 model.sync = function(method, m, options) {
1062 model.on('change:x', function() { assert.ok(true); });
1063 model.save({x: 3}, {wait: true});
1064 assert.equal(model.get('x'), 3);
1067 QUnit.test('save with wait validates attributes', function(assert) {
1068 var model = new Backbone.Model();
1069 model.url = '/test';
1070 model.validate = function() { assert.ok(true); };
1071 model.save({x: 1}, {wait: true});
1074 QUnit.test('save turns on parse flag', function(assert) {
1075 var Model = Backbone.Model.extend({
1076 sync: function(method, m, options) { assert.ok(options.parse); }
1081 QUnit.test("nested `set` during `'change:attr'`", function(assert) {
1084 var model = new Backbone.Model();
1085 model.on('all', function(event) { events.push(event); });
1086 model.on('change', function() {
1087 model.set({z: true}, {silent: true});
1089 model.on('change:x', function() {
1090 model.set({y: true});
1092 model.set({x: true});
1093 assert.deepEqual(events, ['change:y', 'change:x', 'change']);
1095 model.set({z: true});
1096 assert.deepEqual(events, []);
1099 QUnit.test('nested `change` only fires once', function(assert) {
1101 var model = new Backbone.Model();
1102 model.on('change', function() {
1104 model.set({x: true});
1106 model.set({x: true});
1109 QUnit.test("nested `set` during `'change'`", function(assert) {
1112 var model = new Backbone.Model();
1113 model.on('change', function() {
1116 assert.deepEqual(this.changedAttributes(), {x: true});
1117 assert.equal(model.previous('x'), undefined);
1118 model.set({y: true});
1121 assert.deepEqual(this.changedAttributes(), {x: true, y: true});
1122 assert.equal(model.previous('x'), undefined);
1123 model.set({z: true});
1126 assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
1127 assert.equal(model.previous('y'), undefined);
1133 model.set({x: true});
1136 QUnit.test('nested `change` with silent', function(assert) {
1139 var model = new Backbone.Model();
1140 model.on('change:y', function() { assert.ok(false); });
1141 model.on('change', function() {
1144 assert.deepEqual(this.changedAttributes(), {x: true});
1145 model.set({y: true}, {silent: true});
1146 model.set({z: true});
1149 assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
1152 assert.deepEqual(this.changedAttributes(), {z: false});
1158 model.set({x: true});
1159 model.set({z: false});
1162 QUnit.test('nested `change:attr` with silent', function(assert) {
1164 var model = new Backbone.Model();
1165 model.on('change:y', function(){ assert.ok(false); });
1166 model.on('change', function() {
1167 model.set({y: true}, {silent: true});
1168 model.set({z: true});
1170 model.set({x: true});
1173 QUnit.test('multiple nested changes with silent', function(assert) {
1175 var model = new Backbone.Model();
1176 model.on('change:x', function() {
1177 model.set({y: 1}, {silent: true});
1180 model.on('change:y', function(m, val) {
1181 assert.equal(val, 2);
1183 model.set({x: true});
1186 QUnit.test('multiple nested changes with silent', function(assert) {
1189 var model = new Backbone.Model();
1190 model.on('change:b', function(m, val) { changes.push(val); });
1191 model.on('change', function() {
1195 assert.deepEqual(changes, [0, 1]);
1198 QUnit.test('basic silent change semantics', function(assert) {
1200 var model = new Backbone.Model;
1202 model.on('change', function(){ assert.ok(true); });
1203 model.set({x: 2}, {silent: true});
1207 QUnit.test('nested set multiple times', function(assert) {
1209 var model = new Backbone.Model();
1210 model.on('change:b', function() {
1213 model.on('change:a', function() {
1214 model.set({b: true});
1215 model.set({b: true});
1217 model.set({a: true});
1220 QUnit.test('#1122 - clear does not alter options.', function(assert) {
1222 var model = new Backbone.Model();
1224 model.clear(options);
1225 assert.ok(!options.unset);
1228 QUnit.test('#1122 - unset does not alter options.', function(assert) {
1230 var model = new Backbone.Model();
1232 model.unset('x', options);
1233 assert.ok(!options.unset);
1236 QUnit.test('#1355 - `options` is passed to success callbacks', function(assert) {
1238 var model = new Backbone.Model();
1240 success: function( m, resp, options ) {
1244 model.sync = function(method, m, options) {
1247 model.save({id: 1}, opts);
1249 model.destroy(opts);
1252 QUnit.test("#1412 - Trigger 'sync' event.", function(assert) {
1254 var model = new Backbone.Model({id: 1});
1255 model.sync = function(method, m, options) { options.success(); };
1256 model.on('sync', function(){ assert.ok(true); });
1262 QUnit.test('#1365 - Destroy: New models execute success callback.', function(assert) {
1263 var done = assert.async();
1265 new Backbone.Model()
1266 .on('sync', function() { assert.ok(false); })
1267 .on('destroy', function(){ assert.ok(true); })
1268 .destroy({success: function(){
1274 QUnit.test('#1433 - Save: An invalid model cannot be persisted.', function(assert) {
1276 var model = new Backbone.Model;
1277 model.validate = function(){ return 'invalid'; };
1278 model.sync = function(){ assert.ok(false); };
1279 assert.strictEqual(model.save(), false);
1282 QUnit.test("#1377 - Save without attrs triggers 'error'.", function(assert) {
1284 var Model = Backbone.Model.extend({
1286 sync: function(method, m, options){ options.success(); },
1287 validate: function(){ return 'invalid'; }
1289 var model = new Model({id: 1});
1290 model.on('invalid', function(){ assert.ok(true); });
1294 QUnit.test('#1545 - `undefined` can be passed to a model constructor without coersion', function(assert) {
1295 var Model = Backbone.Model.extend({
1297 initialize: function(attrs, opts) {
1298 assert.equal(attrs, undefined);
1301 var emptyattrs = new Model();
1302 var undefinedattrs = new Model(undefined);
1305 QUnit.test('#1478 - Model `save` does not trigger change on unchanged attributes', function(assert) {
1306 var done = assert.async();
1308 var Model = Backbone.Model.extend({
1309 sync: function(method, m, options) {
1310 setTimeout(function(){
1316 new Model({x: true})
1317 .on('change:x', function(){ assert.ok(false); })
1318 .save(null, {wait: true});
1321 QUnit.test('#1664 - Changing from one value, silently to another, back to original triggers a change.', function(assert) {
1323 var model = new Backbone.Model({x: 1});
1324 model.on('change:x', function() { assert.ok(true); });
1325 model.set({x: 2}, {silent: true});
1326 model.set({x: 3}, {silent: true});
1330 QUnit.test('#1664 - multiple silent changes nested inside a change event', function(assert) {
1333 var model = new Backbone.Model();
1334 model.on('change', function() {
1335 model.set({a: 'c'}, {silent: true});
1336 model.set({b: 2}, {silent: true});
1337 model.unset('c', {silent: true});
1339 model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
1340 model.set({a: 'a', b: 1, c: 'item'});
1341 assert.deepEqual(changes, ['a', 1, 'item']);
1342 assert.deepEqual(model.attributes, {a: 'c', b: 2});
1345 QUnit.test('#1791 - `attributes` is available for `parse`', function(assert) {
1346 var Model = Backbone.Model.extend({
1347 parse: function() { this.has('a'); } // shouldn't throw an error
1349 var model = new Model(null, {parse: true});
1353 QUnit.test('silent changes in last `change` event back to original triggers change', function(assert) {
1356 var model = new Backbone.Model();
1357 model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
1358 model.on('change', function() {
1359 model.set({a: 'c'}, {silent: true});
1361 model.set({a: 'a'});
1362 assert.deepEqual(changes, ['a']);
1363 model.set({a: 'a'});
1364 assert.deepEqual(changes, ['a', 'a']);
1367 QUnit.test('#1943 change calculations should use _.isEqual', function(assert) {
1368 var model = new Backbone.Model({a: {key: 'value'}});
1369 model.set('a', {key: 'value'}, {silent: true});
1370 assert.equal(model.changedAttributes(), false);
1373 QUnit.test('#1964 - final `change` event is always fired, regardless of interim changes', function(assert) {
1375 var model = new Backbone.Model();
1376 model.on('change:property', function() {
1377 model.set('property', 'bar');
1379 model.on('change', function() {
1382 model.set('property', 'foo');
1385 QUnit.test('isValid', function(assert) {
1386 var model = new Backbone.Model({valid: true});
1387 model.validate = function(attrs) {
1388 if (!attrs.valid) return 'invalid';
1390 assert.equal(model.isValid(), true);
1391 assert.equal(model.set({valid: false}, {validate: true}), false);
1392 assert.equal(model.isValid(), true);
1393 model.set({valid: false});
1394 assert.equal(model.isValid(), false);
1395 assert.ok(!model.set('valid', false, {validate: true}));
1398 QUnit.test('#1179 - isValid returns true in the absence of validate.', function(assert) {
1400 var model = new Backbone.Model();
1401 model.validate = null;
1402 assert.ok(model.isValid());
1405 QUnit.test('#1961 - Creating a model with {validate:true} will call validate and use the error callback', function(assert) {
1406 var Model = Backbone.Model.extend({
1407 validate: function(attrs) {
1408 if (attrs.id === 1) return "This shouldn't happen";
1411 var model = new Model({id: 1}, {validate: true});
1412 assert.equal(model.validationError, "This shouldn't happen");
1415 QUnit.test('toJSON receives attrs during save(..., {wait: true})', function(assert) {
1417 var Model = Backbone.Model.extend({
1419 toJSON: function() {
1420 assert.strictEqual(this.attributes.x, 1);
1421 return _.clone(this.attributes);
1424 var model = new Model;
1425 model.save({x: 1}, {wait: true});
1428 QUnit.test('#2034 - nested set with silent only triggers one change', function(assert) {
1430 var model = new Backbone.Model();
1431 model.on('change', function() {
1432 model.set({b: true}, {silent: true});
1435 model.set({a: true});
1438 QUnit.test('#3778 - id will only be updated if it is set', function(assert) {
1440 var model = new Backbone.Model({id: 1});
1442 model.set({foo: 'bar'});
1443 assert.equal(model.id, 2);
1445 assert.equal(model.id, 3);