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('initialize with attributes and options', function(assert) {
39 var Model = Backbone.Model.extend({
40 initialize: function(attributes, options) {
41 this.one = options.one;
44 var model = new Model({}, {one: 1});
45 assert.equal(model.one, 1);
48 QUnit.test('initialize with parsed attributes', function(assert) {
50 var Model = Backbone.Model.extend({
51 parse: function(attrs) {
56 var model = new Model({value: 1}, {parse: true});
57 assert.equal(model.get('value'), 2);
60 QUnit.test('initialize with defaults', function(assert) {
62 var Model = Backbone.Model.extend({
68 var model = new Model({'firstName': 'John'});
69 assert.equal(model.get('firstName'), 'John');
70 assert.equal(model.get('lastName'), 'Unknown');
73 QUnit.test('parse can return null', function(assert) {
75 var Model = Backbone.Model.extend({
76 parse: function(attrs) {
81 var model = new Model({value: 1}, {parse: true});
82 assert.equal(JSON.stringify(model.toJSON()), '{}');
85 QUnit.test('url', function(assert) {
88 assert.equal(doc.url(), '/collection/1-the-tempest');
89 doc.collection.url = '/collection/';
90 assert.equal(doc.url(), '/collection/1-the-tempest');
91 doc.collection = null;
92 assert.raises(function() { doc.url(); });
93 doc.collection = collection;
96 QUnit.test('url when using urlRoot, and uri encoding', function(assert) {
98 var Model = Backbone.Model.extend({
99 urlRoot: '/collection'
101 var model = new Model();
102 assert.equal(model.url(), '/collection');
103 model.set({id: '+1+'});
104 assert.equal(model.url(), '/collection/%2B1%2B');
107 QUnit.test('url when using urlRoot as a function to determine urlRoot at runtime', function(assert) {
109 var Model = Backbone.Model.extend({
110 urlRoot: function() {
111 return '/nested/' + this.get('parentId') + '/collection';
115 var model = new Model({parentId: 1});
116 assert.equal(model.url(), '/nested/1/collection');
118 assert.equal(model.url(), '/nested/1/collection/2');
121 QUnit.test('underscore methods', function(assert) {
123 var model = new Backbone.Model({foo: 'a', bar: 'b', baz: 'c'});
124 var model2 = model.clone();
125 assert.deepEqual(model.keys(), ['foo', 'bar', 'baz']);
126 assert.deepEqual(model.values(), ['a', 'b', 'c']);
127 assert.deepEqual(model.invert(), {a: 'foo', b: 'bar', c: 'baz'});
128 assert.deepEqual(model.pick('foo', 'baz'), {foo: 'a', baz: 'c'});
129 assert.deepEqual(model.omit('foo', 'bar'), {baz: 'c'});
132 QUnit.test('chain', function(assert) {
133 var model = new Backbone.Model({a: 0, b: 1, c: 2});
134 assert.deepEqual(model.chain().pick('a', 'b', 'c').values().compact().value(), [1, 2]);
137 QUnit.test('clone', function(assert) {
139 var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
141 assert.equal(a.get('foo'), 1);
142 assert.equal(a.get('bar'), 2);
143 assert.equal(a.get('baz'), 3);
144 assert.equal(b.get('foo'), a.get('foo'), 'Foo should be the same on the clone.');
145 assert.equal(b.get('bar'), a.get('bar'), 'Bar should be the same on the clone.');
146 assert.equal(b.get('baz'), a.get('baz'), 'Baz should be the same on the clone.');
148 assert.equal(a.get('foo'), 100);
149 assert.equal(b.get('foo'), 1, 'Changing a parent attribute does not change the clone.');
151 var foo = new Backbone.Model({p: 1});
152 var bar = new Backbone.Model({p: 2});
153 bar.set(foo.clone().attributes, {unset: true});
154 assert.equal(foo.get('p'), 1);
155 assert.equal(bar.get('p'), undefined);
158 QUnit.test('isNew', function(assert) {
160 var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
161 assert.ok(a.isNew(), 'it should be new');
162 a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: -5});
163 assert.ok(!a.isNew(), 'any defined ID is legal, negative or positive');
164 a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: 0});
165 assert.ok(!a.isNew(), 'any defined ID is legal, including zero');
166 assert.ok(new Backbone.Model().isNew(), 'is true when there is no id');
167 assert.ok(!new Backbone.Model({id: 2}).isNew(), 'is false for a positive integer');
168 assert.ok(!new Backbone.Model({id: -5}).isNew(), 'is false for a negative integer');
171 QUnit.test('get', function(assert) {
173 assert.equal(doc.get('title'), 'The Tempest');
174 assert.equal(doc.get('author'), 'Bill Shakespeare');
177 QUnit.test('escape', function(assert) {
179 assert.equal(doc.escape('title'), 'The Tempest');
180 doc.set({audience: 'Bill & Bob'});
181 assert.equal(doc.escape('audience'), 'Bill & Bob');
182 doc.set({audience: 'Tim > Joan'});
183 assert.equal(doc.escape('audience'), 'Tim > Joan');
184 doc.set({audience: 10101});
185 assert.equal(doc.escape('audience'), '10101');
186 doc.unset('audience');
187 assert.equal(doc.escape('audience'), '');
190 QUnit.test('has', function(assert) {
192 var model = new Backbone.Model();
194 assert.strictEqual(model.has('name'), false);
204 'undefined': undefined
207 assert.strictEqual(model.has('0'), true);
208 assert.strictEqual(model.has('1'), true);
209 assert.strictEqual(model.has('true'), true);
210 assert.strictEqual(model.has('false'), true);
211 assert.strictEqual(model.has('empty'), true);
212 assert.strictEqual(model.has('name'), true);
216 assert.strictEqual(model.has('name'), false);
217 assert.strictEqual(model.has('null'), false);
218 assert.strictEqual(model.has('undefined'), false);
221 QUnit.test('matches', function(assert) {
223 var model = new Backbone.Model();
225 assert.strictEqual(model.matches({name: 'Jonas', cool: true}), false);
227 model.set({name: 'Jonas', cool: true});
229 assert.strictEqual(model.matches({name: 'Jonas'}), true);
230 assert.strictEqual(model.matches({name: 'Jonas', cool: true}), true);
231 assert.strictEqual(model.matches({name: 'Jonas', cool: false}), false);
234 QUnit.test('matches with predicate', function(assert) {
235 var model = new Backbone.Model({a: 0});
237 assert.strictEqual(model.matches(function(attr) {
238 return attr.a > 1 && attr.b != null;
241 model.set({a: 3, b: true});
243 assert.strictEqual(model.matches(function(attr) {
244 return attr.a > 1 && attr.b != null;
248 QUnit.test('set and unset', function(assert) {
250 var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
252 a.on('change:foo', function() { changeCount += 1; });
254 assert.equal(a.get('foo'), 2, 'Foo should have changed.');
255 assert.equal(changeCount, 1, 'Change count should have incremented.');
256 // set with value that is not new shouldn't fire change event
258 assert.equal(a.get('foo'), 2, 'Foo should NOT have changed, still 2');
259 assert.equal(changeCount, 1, 'Change count should NOT have incremented.');
261 a.validate = function(attrs) {
262 assert.equal(attrs.foo, void 0, 'validate:true passed while unsetting');
264 a.unset('foo', {validate: true});
265 assert.equal(a.get('foo'), void 0, 'Foo should have changed');
267 assert.equal(changeCount, 2, 'Change count should have incremented for unset.');
270 assert.equal(a.id, undefined, 'Unsetting the id should remove the id property.');
273 QUnit.test('#2030 - set with failed validate, followed by another set triggers change', function(assert) {
274 var attr = 0, main = 0, error = 0;
275 var Model = Backbone.Model.extend({
276 validate: function(attrs) {
279 return 'this is an error';
283 var model = new Model({x: 0});
284 model.on('change:x', function() { attr++; });
285 model.on('change', function() { main++; });
286 model.set({x: 2}, {validate: true});
287 model.set({x: 1}, {validate: true});
288 assert.deepEqual([attr, main, error], [1, 1, 1]);
291 QUnit.test('set triggers changes in the correct order', function(assert) {
293 var model = new Backbone.Model;
294 model.on('last', function(){ value = 'last'; });
295 model.on('first', function(){ value = 'first'; });
296 model.trigger('first');
297 model.trigger('last');
298 assert.equal(value, 'last');
301 QUnit.test('set falsy values in the correct order', function(assert) {
303 var model = new Backbone.Model({result: 'result'});
304 model.on('change', function() {
305 assert.equal(model.changed.result, void 0);
306 assert.equal(model.previous('result'), false);
308 model.set({result: void 0}, {silent: true});
309 model.set({result: null}, {silent: true});
310 model.set({result: false}, {silent: true});
311 model.set({result: void 0});
314 QUnit.test('nested set triggers with the correct options', function(assert) {
315 var model = new Backbone.Model();
319 model.on('change', function(__, options) {
320 switch (model.get('a')) {
322 assert.equal(options, o1);
323 return model.set('a', 2, o2);
325 assert.equal(options, o2);
326 return model.set('a', 3, o3);
328 assert.equal(options, o3);
331 model.set('a', 1, o1);
334 QUnit.test('multiple unsets', function(assert) {
337 var counter = function(){ i++; };
338 var model = new Backbone.Model({a: 1});
339 model.on('change:a', counter);
343 assert.equal(i, 2, 'Unset does not fire an event for missing attributes.');
346 QUnit.test('unset and changedAttributes', function(assert) {
348 var model = new Backbone.Model({a: 1});
349 model.on('change', function() {
350 assert.ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties');
355 QUnit.test('using a non-default id attribute.', function(assert) {
357 var MongoModel = Backbone.Model.extend({idAttribute: '_id'});
358 var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
359 assert.equal(model.get('id'), 'eye-dee');
360 assert.equal(model.id, 25);
361 assert.equal(model.isNew(), false);
363 assert.equal(model.id, undefined);
364 assert.equal(model.isNew(), true);
367 QUnit.test('setting an alternative cid prefix', function(assert) {
369 var Model = Backbone.Model.extend({
372 var model = new Model();
374 assert.equal(model.cid.charAt(0), 'm');
376 model = new Backbone.Model();
377 assert.equal(model.cid.charAt(0), 'c');
379 var Collection = Backbone.Collection.extend({
382 var col = new Collection([{id: 'c5'}, {id: 'c6'}, {id: 'c7'}]);
384 assert.equal(col.get('c6').cid.charAt(0), 'm');
385 col.set([{id: 'c6', value: 'test'}], {
390 assert.ok(col.get('c6').has('value'));
393 QUnit.test('set an empty string', function(assert) {
395 var model = new Backbone.Model({name: 'Model'});
396 model.set({name: ''});
397 assert.equal(model.get('name'), '');
400 QUnit.test('setting an object', function(assert) {
402 var model = new Backbone.Model({
405 model.on('change', function() {
409 custom: {foo: 1} // no change should be fired
412 custom: {foo: 2} // change event should be fired
416 QUnit.test('clear', function(assert) {
419 var model = new Backbone.Model({id: 1, name: 'Model'});
420 model.on('change:name', function(){ changed = true; });
421 model.on('change', function() {
422 var changedAttrs = model.changedAttributes();
423 assert.ok('name' in changedAttrs);
426 assert.equal(changed, true);
427 assert.equal(model.get('name'), undefined);
430 QUnit.test('defaults', function(assert) {
432 var Defaulted = Backbone.Model.extend({
438 var model = new Defaulted({two: undefined});
439 assert.equal(model.get('one'), 1);
440 assert.equal(model.get('two'), 2);
441 Defaulted = Backbone.Model.extend({
442 defaults: function() {
449 model = new Defaulted({two: undefined});
450 assert.equal(model.get('one'), 3);
451 assert.equal(model.get('two'), 4);
454 QUnit.test('change, hasChanged, changedAttributes, previous, previousAttributes', function(assert) {
456 var model = new Backbone.Model({name: 'Tim', age: 10});
457 assert.deepEqual(model.changedAttributes(), false);
458 model.on('change', function() {
459 assert.ok(model.hasChanged('name'), 'name changed');
460 assert.ok(!model.hasChanged('age'), 'age did not');
461 assert.ok(_.isEqual(model.changedAttributes(), {name: 'Rob'}), 'changedAttributes returns the changed attrs');
462 assert.equal(model.previous('name'), 'Tim');
463 assert.ok(_.isEqual(model.previousAttributes(), {name: 'Tim', age: 10}), 'previousAttributes is correct');
465 assert.equal(model.hasChanged(), false);
466 assert.equal(model.hasChanged(undefined), false);
467 model.set({name: 'Rob'});
468 assert.equal(model.get('name'), 'Rob');
471 QUnit.test('changedAttributes', function(assert) {
473 var model = new Backbone.Model({a: 'a', b: 'b'});
474 assert.deepEqual(model.changedAttributes(), false);
475 assert.equal(model.changedAttributes({a: 'a'}), false);
476 assert.equal(model.changedAttributes({a: 'b'}).a, 'b');
479 QUnit.test('change with options', function(assert) {
482 var model = new Backbone.Model({name: 'Rob'});
483 model.on('change', function(m, options) {
484 value = options.prefix + m.get('name');
486 model.set({name: 'Bob'}, {prefix: 'Mr. '});
487 assert.equal(value, 'Mr. Bob');
488 model.set({name: 'Sue'}, {prefix: 'Ms. '});
489 assert.equal(value, 'Ms. Sue');
492 QUnit.test('change after initialize', function(assert) {
495 var attrs = {id: 1, label: 'c'};
496 var obj = new Backbone.Model(attrs);
497 obj.on('change', function() { changed += 1; });
499 assert.equal(changed, 0);
502 QUnit.test('save within change event', function(assert) {
505 var model = new Backbone.Model({firstName: 'Taylor', lastName: 'Swift'});
507 model.on('change', function() {
509 assert.ok(_.isEqual(env.syncArgs.model, model));
511 model.set({lastName: 'Hicks'});
514 QUnit.test('validate after save', function(assert) {
516 var lastError, model = new Backbone.Model();
517 model.validate = function(attrs) {
518 if (attrs.admin) return "Can't change admin status.";
520 model.sync = function(method, m, options) {
521 options.success.call(this, {admin: true});
523 model.on('invalid', function(m, error) {
528 assert.equal(lastError, "Can't change admin status.");
529 assert.equal(model.validationError, "Can't change admin status.");
532 QUnit.test('save', function(assert) {
534 doc.save({title: 'Henry V'});
535 assert.equal(this.syncArgs.method, 'update');
536 assert.ok(_.isEqual(this.syncArgs.model, doc));
539 QUnit.test('save, fetch, destroy triggers error event when an error occurs', function(assert) {
541 var model = new Backbone.Model();
542 model.on('error', function() {
545 model.sync = function(method, m, options) {
548 model.save({data: 2, id: 1});
553 QUnit.test('#3283 - save, fetch, destroy calls success with context', function(assert) {
555 var model = new Backbone.Model();
559 success: function() {
560 assert.equal(this, obj);
563 model.sync = function(method, m, opts) {
564 opts.success.call(opts.context);
566 model.save({data: 2, id: 1}, options);
567 model.fetch(options);
568 model.destroy(options);
571 QUnit.test('#3283 - save, fetch, destroy calls error with context', function(assert) {
573 var model = new Backbone.Model();
578 assert.equal(this, obj);
581 model.sync = function(method, m, opts) {
582 opts.error.call(opts.context);
584 model.save({data: 2, id: 1}, options);
585 model.fetch(options);
586 model.destroy(options);
589 QUnit.test('#3470 - save and fetch with parse false', function(assert) {
592 var model = new Backbone.Model();
593 model.parse = function() {
596 model.sync = function(method, m, options) {
597 options.success({i: ++i});
599 model.fetch({parse: false});
600 assert.equal(model.get('i'), i);
601 model.save(null, {parse: false});
602 assert.equal(model.get('i'), i);
605 QUnit.test('save with PATCH', function(assert) {
606 doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
608 assert.equal(this.syncArgs.method, 'update');
609 assert.equal(this.syncArgs.options.attrs, undefined);
611 doc.save({b: 2, d: 4}, {patch: true});
612 assert.equal(this.syncArgs.method, 'patch');
613 assert.equal(_.size(this.syncArgs.options.attrs), 2);
614 assert.equal(this.syncArgs.options.attrs.d, 4);
615 assert.equal(this.syncArgs.options.attrs.a, undefined);
616 assert.equal(this.ajaxSettings.data, '{"b":2,"d":4}');
619 QUnit.test('save with PATCH and different attrs', function(assert) {
620 doc.clear().save({b: 2, d: 4}, {patch: true, attrs: {B: 1, D: 3}});
621 assert.equal(this.syncArgs.options.attrs.D, 3);
622 assert.equal(this.syncArgs.options.attrs.d, undefined);
623 assert.equal(this.ajaxSettings.data, '{"B":1,"D":3}');
624 assert.deepEqual(doc.attributes, {b: 2, d: 4});
627 QUnit.test('save in positional style', function(assert) {
629 var model = new Backbone.Model();
630 model.sync = function(method, m, options) {
633 model.save('title', 'Twelfth Night');
634 assert.equal(model.get('title'), 'Twelfth Night');
637 QUnit.test('save with non-object success response', function(assert) {
639 var model = new Backbone.Model();
640 model.sync = function(method, m, options) {
641 options.success('', options);
642 options.success(null, options);
644 model.save({testing: 'empty'}, {
645 success: function(m) {
646 assert.deepEqual(m.attributes, {testing: 'empty'});
651 QUnit.test('save with wait and supplied id', function(assert) {
652 var Model = Backbone.Model.extend({
653 urlRoot: '/collection'
655 var model = new Model();
656 model.save({id: 42}, {wait: true});
657 assert.equal(this.ajaxSettings.url, '/collection/42');
660 QUnit.test('save will pass extra options to success callback', function(assert) {
662 var SpecialSyncModel = Backbone.Model.extend({
663 sync: function(method, m, options) {
664 _.extend(options, {specialSync: true});
665 return Backbone.Model.prototype.sync.call(this, method, m, options);
670 var model = new SpecialSyncModel();
672 var onSuccess = function(m, response, options) {
673 assert.ok(options.specialSync, 'Options were passed correctly to callback');
676 model.save(null, {success: onSuccess});
677 this.ajaxSettings.success();
680 QUnit.test('fetch', function(assert) {
683 assert.equal(this.syncArgs.method, 'read');
684 assert.ok(_.isEqual(this.syncArgs.model, doc));
687 QUnit.test('fetch will pass extra options to success callback', function(assert) {
689 var SpecialSyncModel = Backbone.Model.extend({
690 sync: function(method, m, options) {
691 _.extend(options, {specialSync: true});
692 return Backbone.Model.prototype.sync.call(this, method, m, options);
697 var model = new SpecialSyncModel();
699 var onSuccess = function(m, response, options) {
700 assert.ok(options.specialSync, 'Options were passed correctly to callback');
703 model.fetch({success: onSuccess});
704 this.ajaxSettings.success();
707 QUnit.test('destroy', function(assert) {
710 assert.equal(this.syncArgs.method, 'delete');
711 assert.ok(_.isEqual(this.syncArgs.model, doc));
713 var newModel = new Backbone.Model;
714 assert.equal(newModel.destroy(), false);
717 QUnit.test('destroy will pass extra options to success callback', function(assert) {
719 var SpecialSyncModel = Backbone.Model.extend({
720 sync: function(method, m, options) {
721 _.extend(options, {specialSync: true});
722 return Backbone.Model.prototype.sync.call(this, method, m, options);
727 var model = new SpecialSyncModel({id: 'id'});
729 var onSuccess = function(m, response, options) {
730 assert.ok(options.specialSync, 'Options were passed correctly to callback');
733 model.destroy({success: onSuccess});
734 this.ajaxSettings.success();
737 QUnit.test('non-persisted destroy', function(assert) {
739 var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
740 a.sync = function() { throw 'should not be called'; };
742 assert.ok(true, 'non-persisted model should not call sync');
745 QUnit.test('validate', function(assert) {
747 var model = new Backbone.Model();
748 model.validate = function(attrs) {
749 if (attrs.admin !== this.get('admin')) return "Can't change admin status.";
751 model.on('invalid', function(m, error) {
754 var result = model.set({a: 100});
755 assert.equal(result, model);
756 assert.equal(model.get('a'), 100);
757 assert.equal(lastError, undefined);
758 result = model.set({admin: true});
759 assert.equal(model.get('admin'), true);
760 result = model.set({a: 200, admin: false}, {validate: true});
761 assert.equal(lastError, "Can't change admin status.");
762 assert.equal(result, false);
763 assert.equal(model.get('a'), 100);
766 QUnit.test('validate on unset and clear', function(assert) {
769 var model = new Backbone.Model({name: 'One'});
770 model.validate = function(attrs) {
776 model.set({name: 'Two'});
777 assert.equal(model.get('name'), 'Two');
778 assert.equal(error, undefined);
779 model.unset('name', {validate: true});
780 assert.equal(error, true);
781 assert.equal(model.get('name'), 'Two');
782 model.clear({validate: true});
783 assert.equal(model.get('name'), 'Two');
784 delete model.validate;
786 assert.equal(model.get('name'), undefined);
789 QUnit.test('validate with error callback', function(assert) {
791 var lastError, boundError;
792 var model = new Backbone.Model();
793 model.validate = function(attrs) {
794 if (attrs.admin) return "Can't change admin status.";
796 model.on('invalid', function(m, error) {
799 var result = model.set({a: 100}, {validate: true});
800 assert.equal(result, model);
801 assert.equal(model.get('a'), 100);
802 assert.equal(model.validationError, null);
803 assert.equal(boundError, undefined);
804 result = model.set({a: 200, admin: true}, {validate: true});
805 assert.equal(result, false);
806 assert.equal(model.get('a'), 100);
807 assert.equal(model.validationError, "Can't change admin status.");
808 assert.equal(boundError, true);
811 QUnit.test('defaults always extend attrs (#459)', function(assert) {
813 var Defaulted = Backbone.Model.extend({
815 initialize: function(attrs, opts) {
816 assert.equal(this.attributes.one, 1);
819 var providedattrs = new Defaulted({});
820 var emptyattrs = new Defaulted();
823 QUnit.test('Inherit class properties', function(assert) {
825 var Parent = Backbone.Model.extend({
826 instancePropSame: function() {},
827 instancePropDiff: function() {}
829 classProp: function() {}
831 var Child = Parent.extend({
832 instancePropDiff: function() {}
835 var adult = new Parent;
838 assert.equal(Child.classProp, Parent.classProp);
839 assert.notEqual(Child.classProp, undefined);
841 assert.equal(kid.instancePropSame, adult.instancePropSame);
842 assert.notEqual(kid.instancePropSame, undefined);
844 assert.notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
845 assert.notEqual(Child.prototype.instancePropDiff, undefined);
848 QUnit.test("Nested change events don't clobber previous attributes", function(assert) {
851 .on('change:state', function(m, newState) {
852 assert.equal(m.previous('state'), undefined);
853 assert.equal(newState, 'hello');
854 // Fire a nested change event.
855 m.set({other: 'whatever'});
857 .on('change:state', function(m, newState) {
858 assert.equal(m.previous('state'), undefined);
859 assert.equal(newState, 'hello');
861 .set({state: 'hello'});
864 QUnit.test('hasChanged/set should use same comparison', function(assert) {
866 var changed = 0, model = new Backbone.Model({a: null});
867 model.on('change', function() {
868 assert.ok(this.hasChanged('a'));
870 .on('change:a', function() {
873 .set({a: undefined});
874 assert.equal(changed, 1);
877 QUnit.test('#582, #425, change:attribute callbacks should fire after all changes have occurred', function(assert) {
879 var model = new Backbone.Model;
881 var assertion = function() {
882 assert.equal(model.get('a'), 'a');
883 assert.equal(model.get('b'), 'b');
884 assert.equal(model.get('c'), 'c');
887 model.on('change:a', assertion);
888 model.on('change:b', assertion);
889 model.on('change:c', assertion);
891 model.set({a: 'a', b: 'b', c: 'c'});
894 QUnit.test('#871, set with attributes property', function(assert) {
896 var model = new Backbone.Model();
897 model.set({attributes: true});
898 assert.ok(model.has('attributes'));
901 QUnit.test('set value regardless of equality/change', function(assert) {
903 var model = new Backbone.Model({x: []});
906 assert.ok(model.get('x') === a);
909 QUnit.test('set same value does not trigger change', function(assert) {
911 var model = new Backbone.Model({x: 1});
912 model.on('change change:x', function() { assert.ok(false); });
917 QUnit.test('unset does not fire a change for undefined attributes', function(assert) {
919 var model = new Backbone.Model({x: undefined});
920 model.on('change:x', function(){ assert.ok(false); });
924 QUnit.test('set: undefined values', function(assert) {
926 var model = new Backbone.Model({x: undefined});
927 assert.ok('x' in model.attributes);
930 QUnit.test('hasChanged works outside of change events, and true within', function(assert) {
932 var model = new Backbone.Model({x: 1});
933 model.on('change:x', function() {
934 assert.ok(model.hasChanged('x'));
935 assert.equal(model.get('x'), 1);
937 model.set({x: 2}, {silent: true});
938 assert.ok(model.hasChanged());
939 assert.equal(model.hasChanged('x'), true);
941 assert.ok(model.hasChanged());
942 assert.equal(model.hasChanged('x'), true);
945 QUnit.test('hasChanged gets cleared on the following set', function(assert) {
947 var model = new Backbone.Model;
949 assert.ok(model.hasChanged());
951 assert.ok(!model.hasChanged());
953 assert.ok(model.hasChanged());
955 assert.ok(!model.hasChanged());
958 QUnit.test('save with `wait` succeeds without `validate`', function(assert) {
960 var model = new Backbone.Model();
962 model.save({x: 1}, {wait: true});
963 assert.ok(this.syncArgs.model === model);
966 QUnit.test("save without `wait` doesn't set invalid attributes", function(assert) {
967 var model = new Backbone.Model();
968 model.validate = function() { return 1; };
970 assert.equal(model.get('a'), void 0);
973 QUnit.test("save doesn't validate twice", function(assert) {
974 var model = new Backbone.Model();
976 model.sync = function() {};
977 model.validate = function() { ++times; };
979 assert.equal(times, 1);
982 QUnit.test('`hasChanged` for falsey keys', function(assert) {
984 var model = new Backbone.Model();
985 model.set({x: true}, {silent: true});
986 assert.ok(!model.hasChanged(0));
987 assert.ok(!model.hasChanged(''));
990 QUnit.test('`previous` for falsey keys', function(assert) {
992 var model = new Backbone.Model({0: true, '': true});
993 model.set({0: false, '': false}, {silent: true});
994 assert.equal(model.previous(0), true);
995 assert.equal(model.previous(''), true);
998 QUnit.test('`save` with `wait` sends correct attributes', function(assert) {
1001 var model = new Backbone.Model({x: 1, y: 2});
1002 model.url = '/test';
1003 model.on('change:x', function() { changed++; });
1004 model.save({x: 3}, {wait: true});
1005 assert.deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
1006 assert.equal(model.get('x'), 1);
1007 assert.equal(changed, 0);
1008 this.syncArgs.options.success({});
1009 assert.equal(model.get('x'), 3);
1010 assert.equal(changed, 1);
1013 QUnit.test("a failed `save` with `wait` doesn't leave attributes behind", function(assert) {
1015 var model = new Backbone.Model;
1016 model.url = '/test';
1017 model.save({x: 1}, {wait: true});
1018 assert.equal(model.get('x'), void 0);
1021 QUnit.test('#1030 - `save` with `wait` results in correct attributes if success is called during sync', function(assert) {
1023 var model = new Backbone.Model({x: 1, y: 2});
1024 model.sync = function(method, m, options) {
1027 model.on('change:x', function() { assert.ok(true); });
1028 model.save({x: 3}, {wait: true});
1029 assert.equal(model.get('x'), 3);
1032 QUnit.test('save with wait validates attributes', function(assert) {
1033 var model = new Backbone.Model();
1034 model.url = '/test';
1035 model.validate = function() { assert.ok(true); };
1036 model.save({x: 1}, {wait: true});
1039 QUnit.test('save turns on parse flag', function(assert) {
1040 var Model = Backbone.Model.extend({
1041 sync: function(method, m, options) { assert.ok(options.parse); }
1046 QUnit.test("nested `set` during `'change:attr'`", function(assert) {
1049 var model = new Backbone.Model();
1050 model.on('all', function(event) { events.push(event); });
1051 model.on('change', function() {
1052 model.set({z: true}, {silent: true});
1054 model.on('change:x', function() {
1055 model.set({y: true});
1057 model.set({x: true});
1058 assert.deepEqual(events, ['change:y', 'change:x', 'change']);
1060 model.set({z: true});
1061 assert.deepEqual(events, []);
1064 QUnit.test('nested `change` only fires once', function(assert) {
1066 var model = new Backbone.Model();
1067 model.on('change', function() {
1069 model.set({x: true});
1071 model.set({x: true});
1074 QUnit.test("nested `set` during `'change'`", function(assert) {
1077 var model = new Backbone.Model();
1078 model.on('change', function() {
1081 assert.deepEqual(this.changedAttributes(), {x: true});
1082 assert.equal(model.previous('x'), undefined);
1083 model.set({y: true});
1086 assert.deepEqual(this.changedAttributes(), {x: true, y: true});
1087 assert.equal(model.previous('x'), undefined);
1088 model.set({z: true});
1091 assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
1092 assert.equal(model.previous('y'), undefined);
1098 model.set({x: true});
1101 QUnit.test('nested `change` with silent', function(assert) {
1104 var model = new Backbone.Model();
1105 model.on('change:y', function() { assert.ok(false); });
1106 model.on('change', function() {
1109 assert.deepEqual(this.changedAttributes(), {x: true});
1110 model.set({y: true}, {silent: true});
1111 model.set({z: true});
1114 assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
1117 assert.deepEqual(this.changedAttributes(), {z: false});
1123 model.set({x: true});
1124 model.set({z: false});
1127 QUnit.test('nested `change:attr` with silent', function(assert) {
1129 var model = new Backbone.Model();
1130 model.on('change:y', function(){ assert.ok(false); });
1131 model.on('change', function() {
1132 model.set({y: true}, {silent: true});
1133 model.set({z: true});
1135 model.set({x: true});
1138 QUnit.test('multiple nested changes with silent', function(assert) {
1140 var model = new Backbone.Model();
1141 model.on('change:x', function() {
1142 model.set({y: 1}, {silent: true});
1145 model.on('change:y', function(m, val) {
1146 assert.equal(val, 2);
1148 model.set({x: true});
1151 QUnit.test('multiple nested changes with silent', function(assert) {
1154 var model = new Backbone.Model();
1155 model.on('change:b', function(m, val) { changes.push(val); });
1156 model.on('change', function() {
1160 assert.deepEqual(changes, [0, 1]);
1163 QUnit.test('basic silent change semantics', function(assert) {
1165 var model = new Backbone.Model;
1167 model.on('change', function(){ assert.ok(true); });
1168 model.set({x: 2}, {silent: true});
1172 QUnit.test('nested set multiple times', function(assert) {
1174 var model = new Backbone.Model();
1175 model.on('change:b', function() {
1178 model.on('change:a', function() {
1179 model.set({b: true});
1180 model.set({b: true});
1182 model.set({a: true});
1185 QUnit.test('#1122 - clear does not alter options.', function(assert) {
1187 var model = new Backbone.Model();
1189 model.clear(options);
1190 assert.ok(!options.unset);
1193 QUnit.test('#1122 - unset does not alter options.', function(assert) {
1195 var model = new Backbone.Model();
1197 model.unset('x', options);
1198 assert.ok(!options.unset);
1201 QUnit.test('#1355 - `options` is passed to success callbacks', function(assert) {
1203 var model = new Backbone.Model();
1205 success: function( m, resp, options ) {
1209 model.sync = function(method, m, options) {
1212 model.save({id: 1}, opts);
1214 model.destroy(opts);
1217 QUnit.test("#1412 - Trigger 'sync' event.", function(assert) {
1219 var model = new Backbone.Model({id: 1});
1220 model.sync = function(method, m, options) { options.success(); };
1221 model.on('sync', function(){ assert.ok(true); });
1227 QUnit.test('#1365 - Destroy: New models execute success callback.', function(assert) {
1228 var done = assert.async();
1230 new Backbone.Model()
1231 .on('sync', function() { assert.ok(false); })
1232 .on('destroy', function(){ assert.ok(true); })
1233 .destroy({success: function(){
1239 QUnit.test('#1433 - Save: An invalid model cannot be persisted.', function(assert) {
1241 var model = new Backbone.Model;
1242 model.validate = function(){ return 'invalid'; };
1243 model.sync = function(){ assert.ok(false); };
1244 assert.strictEqual(model.save(), false);
1247 QUnit.test("#1377 - Save without attrs triggers 'error'.", function(assert) {
1249 var Model = Backbone.Model.extend({
1251 sync: function(method, m, options){ options.success(); },
1252 validate: function(){ return 'invalid'; }
1254 var model = new Model({id: 1});
1255 model.on('invalid', function(){ assert.ok(true); });
1259 QUnit.test('#1545 - `undefined` can be passed to a model constructor without coersion', function(assert) {
1260 var Model = Backbone.Model.extend({
1262 initialize: function(attrs, opts) {
1263 assert.equal(attrs, undefined);
1266 var emptyattrs = new Model();
1267 var undefinedattrs = new Model(undefined);
1270 QUnit.test('#1478 - Model `save` does not trigger change on unchanged attributes', function(assert) {
1271 var done = assert.async();
1273 var Model = Backbone.Model.extend({
1274 sync: function(method, m, options) {
1275 setTimeout(function(){
1281 new Model({x: true})
1282 .on('change:x', function(){ assert.ok(false); })
1283 .save(null, {wait: true});
1286 QUnit.test('#1664 - Changing from one value, silently to another, back to original triggers a change.', function(assert) {
1288 var model = new Backbone.Model({x: 1});
1289 model.on('change:x', function() { assert.ok(true); });
1290 model.set({x: 2}, {silent: true});
1291 model.set({x: 3}, {silent: true});
1295 QUnit.test('#1664 - multiple silent changes nested inside a change event', function(assert) {
1298 var model = new Backbone.Model();
1299 model.on('change', function() {
1300 model.set({a: 'c'}, {silent: true});
1301 model.set({b: 2}, {silent: true});
1302 model.unset('c', {silent: true});
1304 model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
1305 model.set({a: 'a', b: 1, c: 'item'});
1306 assert.deepEqual(changes, ['a', 1, 'item']);
1307 assert.deepEqual(model.attributes, {a: 'c', b: 2});
1310 QUnit.test('#1791 - `attributes` is available for `parse`', function(assert) {
1311 var Model = Backbone.Model.extend({
1312 parse: function() { this.has('a'); } // shouldn't throw an error
1314 var model = new Model(null, {parse: true});
1318 QUnit.test('silent changes in last `change` event back to original triggers change', function(assert) {
1321 var model = new Backbone.Model();
1322 model.on('change:a change:b change:c', function(m, val) { changes.push(val); });
1323 model.on('change', function() {
1324 model.set({a: 'c'}, {silent: true});
1326 model.set({a: 'a'});
1327 assert.deepEqual(changes, ['a']);
1328 model.set({a: 'a'});
1329 assert.deepEqual(changes, ['a', 'a']);
1332 QUnit.test('#1943 change calculations should use _.isEqual', function(assert) {
1333 var model = new Backbone.Model({a: {key: 'value'}});
1334 model.set('a', {key: 'value'}, {silent: true});
1335 assert.equal(model.changedAttributes(), false);
1338 QUnit.test('#1964 - final `change` event is always fired, regardless of interim changes', function(assert) {
1340 var model = new Backbone.Model();
1341 model.on('change:property', function() {
1342 model.set('property', 'bar');
1344 model.on('change', function() {
1347 model.set('property', 'foo');
1350 QUnit.test('isValid', function(assert) {
1351 var model = new Backbone.Model({valid: true});
1352 model.validate = function(attrs) {
1353 if (!attrs.valid) return 'invalid';
1355 assert.equal(model.isValid(), true);
1356 assert.equal(model.set({valid: false}, {validate: true}), false);
1357 assert.equal(model.isValid(), true);
1358 model.set({valid: false});
1359 assert.equal(model.isValid(), false);
1360 assert.ok(!model.set('valid', false, {validate: true}));
1363 QUnit.test('#1179 - isValid returns true in the absence of validate.', function(assert) {
1365 var model = new Backbone.Model();
1366 model.validate = null;
1367 assert.ok(model.isValid());
1370 QUnit.test('#1961 - Creating a model with {validate:true} will call validate and use the error callback', function(assert) {
1371 var Model = Backbone.Model.extend({
1372 validate: function(attrs) {
1373 if (attrs.id === 1) return "This shouldn't happen";
1376 var model = new Model({id: 1}, {validate: true});
1377 assert.equal(model.validationError, "This shouldn't happen");
1380 QUnit.test('toJSON receives attrs during save(..., {wait: true})', function(assert) {
1382 var Model = Backbone.Model.extend({
1384 toJSON: function() {
1385 assert.strictEqual(this.attributes.x, 1);
1386 return _.clone(this.attributes);
1389 var model = new Model;
1390 model.save({x: 1}, {wait: true});
1393 QUnit.test('#2034 - nested set with silent only triggers one change', function(assert) {
1395 var model = new Backbone.Model();
1396 model.on('change', function() {
1397 model.set({b: true}, {silent: true});
1400 model.set({a: true});
1403 QUnit.test('#3778 - id will only be updated if it is set', function(assert) {
1405 var model = new Backbone.Model({id: 1});
1407 model.set({foo: 'bar'});
1408 assert.equal(model.id, 2);
1410 assert.equal(model.id, 3);