PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test/model.js

https://github.com/cdata/backbone-nested
JavaScript | 797 lines | 702 code | 89 blank | 6 comment | 12 complexity | 061bc7dcd0d39806ef23e0c2d65970b8 MD5 | raw file
  1. // yanked from Backbone 0.9.2 test suite
  2. $(document).ready(function() {
  3. // test Backbone.NestedModel instead of Backbone.Model - reset at end of function
  4. var oldModel = Backbone.Model;
  5. Backbone.Model = Backbone.NestedModel;
  6. // Variable to catch the last request.
  7. var lastRequest = null;
  8. // Variable to catch ajax params.
  9. var ajaxParams = null;
  10. var sync = Backbone.sync;
  11. var ajax = $.ajax;
  12. var urlRoot = null;
  13. var proxy = Backbone.Model.extend();
  14. var klass = Backbone.Collection.extend({
  15. url : function() { return '/collection'; }
  16. });
  17. var doc, collection;
  18. module("Backbone.Model", {
  19. setup: function() {
  20. doc = new proxy({
  21. id : '1-the-tempest',
  22. title : "The Tempest",
  23. author : "Bill Shakespeare",
  24. length : 123
  25. });
  26. collection = new klass();
  27. collection.add(doc);
  28. Backbone.sync = function(method, model, options) {
  29. lastRequest = {
  30. method: method,
  31. model: model,
  32. options: options
  33. };
  34. sync.apply(this, arguments);
  35. };
  36. $.ajax = function(params) { ajaxParams = params; };
  37. urlRoot = Backbone.Model.prototype.urlRoot;
  38. Backbone.Model.prototype.urlRoot = '/';
  39. },
  40. teardown: function() {
  41. Backbone.sync = sync;
  42. $.ajax = ajax;
  43. Backbone.Model.prototype.urlRoot = urlRoot;
  44. }
  45. });
  46. test("Model: initialize", function() {
  47. var Model = Backbone.Model.extend({
  48. initialize: function() {
  49. this.one = 1;
  50. equal(this.collection, collection);
  51. }
  52. });
  53. var model = new Model({}, {collection: collection});
  54. equal(model.one, 1);
  55. equal(model.collection, collection);
  56. });
  57. test("Model: initialize with attributes and options", function() {
  58. var Model = Backbone.Model.extend({
  59. initialize: function(attributes, options) {
  60. this.one = options.one;
  61. }
  62. });
  63. var model = new Model({}, {one: 1});
  64. equal(model.one, 1);
  65. });
  66. test("Model: initialize with parsed attributes", function() {
  67. var Model = Backbone.Model.extend({
  68. parse: function(obj) {
  69. obj.value += 1;
  70. return obj;
  71. }
  72. });
  73. var model = new Model({value: 1}, {parse: true});
  74. equal(model.get('value'), 2);
  75. });
  76. test("Model: url", function() {
  77. doc.urlRoot = null;
  78. equal(doc.url(), '/collection/1-the-tempest');
  79. doc.collection.url = '/collection/';
  80. equal(doc.url(), '/collection/1-the-tempest');
  81. doc.collection = null;
  82. raises(function() { doc.url(); });
  83. doc.collection = collection;
  84. });
  85. test("Model: url when using urlRoot, and uri encoding", function() {
  86. var Model = Backbone.Model.extend({
  87. urlRoot: '/collection'
  88. });
  89. var model = new Model();
  90. equal(model.url(), '/collection');
  91. model.set({id: '+1+'});
  92. equal(model.url(), '/collection/%2B1%2B');
  93. });
  94. test("Model: url when using urlRoot as a function to determine urlRoot at runtime", function() {
  95. var Model = Backbone.Model.extend({
  96. urlRoot: function() {
  97. return '/nested/' + this.get('parent_id') + '/collection';
  98. }
  99. });
  100. var model = new Model({parent_id: 1});
  101. equal(model.url(), '/nested/1/collection');
  102. model.set({id: 2});
  103. equal(model.url(), '/nested/1/collection/2');
  104. });
  105. test("Model: clone", function() {
  106. var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
  107. var b = a.clone();
  108. equal(a.get('foo'), 1);
  109. equal(a.get('bar'), 2);
  110. equal(a.get('baz'), 3);
  111. equal(b.get('foo'), a.get('foo'), "Foo should be the same on the clone.");
  112. equal(b.get('bar'), a.get('bar'), "Bar should be the same on the clone.");
  113. equal(b.get('baz'), a.get('baz'), "Baz should be the same on the clone.");
  114. a.set({foo : 100});
  115. equal(a.get('foo'), 100);
  116. equal(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
  117. });
  118. test("Model: isNew", function() {
  119. var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
  120. ok(a.isNew(), "it should be new");
  121. a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 });
  122. ok(!a.isNew(), "any defined ID is legal, negative or positive");
  123. a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 });
  124. ok(!a.isNew(), "any defined ID is legal, including zero");
  125. ok( new Backbone.Model({ }).isNew(), "is true when there is no id");
  126. ok(!new Backbone.Model({ 'id': 2 }).isNew(), "is false for a positive integer");
  127. ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integer");
  128. });
  129. test("Model: get", function() {
  130. equal(doc.get('title'), 'The Tempest');
  131. equal(doc.get('author'), 'Bill Shakespeare');
  132. });
  133. test("Model: escape", function() {
  134. equal(doc.escape('title'), 'The Tempest');
  135. doc.set({audience: 'Bill & Bob'});
  136. equal(doc.escape('audience'), 'Bill & Bob');
  137. doc.set({audience: 'Tim > Joan'});
  138. equal(doc.escape('audience'), 'Tim > Joan');
  139. doc.set({audience: 10101});
  140. equal(doc.escape('audience'), '10101');
  141. doc.unset('audience');
  142. equal(doc.escape('audience'), '');
  143. });
  144. test("Model: has", function() {
  145. var a = new Backbone.Model();
  146. equal(a.has("name"), false);
  147. _([true, "Truth!", 1, false, '', 0]).each(function(value) {
  148. a.set({'name': value});
  149. equal(a.has("name"), true);
  150. });
  151. a.unset('name');
  152. equal(a.has('name'), false);
  153. _([null, undefined]).each(function(value) {
  154. a.set({'name': value});
  155. equal(a.has("name"), false);
  156. });
  157. });
  158. test("Model: set and unset", function() {
  159. expect(8);
  160. var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
  161. var changeCount = 0;
  162. a.on("change:foo", function() { changeCount += 1; });
  163. a.set({'foo': 2});
  164. ok(a.get('foo') == 2, "Foo should have changed.");
  165. ok(changeCount == 1, "Change count should have incremented.");
  166. a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
  167. ok(a.get('foo') == 2, "Foo should NOT have changed, still 2");
  168. ok(changeCount == 1, "Change count should NOT have incremented.");
  169. a.validate = function(attrs) {
  170. equal(attrs.foo, void 0, "don't ignore values when unsetting");
  171. };
  172. a.unset('foo');
  173. equal(a.get('foo'), void 0, "Foo should have changed");
  174. delete a.validate;
  175. ok(changeCount == 2, "Change count should have incremented for unset.");
  176. a.unset('id');
  177. equal(a.id, undefined, "Unsetting the id should remove the id property.");
  178. });
  179. test("Model: multiple unsets", function() {
  180. var i = 0;
  181. var counter = function(){ i++; };
  182. var model = new Backbone.Model({a: 1});
  183. model.on("change:a", counter);
  184. model.set({a: 2});
  185. model.unset('a');
  186. model.unset('a');
  187. equal(i, 2, 'Unset does not fire an event for missing attributes.');
  188. });
  189. test("Model: unset and changedAttributes", function() {
  190. var model = new Backbone.Model({a: 1});
  191. model.unset('a', {silent: true});
  192. var changedAttributes = model.changedAttributes();
  193. ok('a' in changedAttributes, 'changedAttributes should contain unset properties');
  194. changedAttributes = model.changedAttributes();
  195. ok('a' in changedAttributes, 'changedAttributes should contain unset properties when running changedAttributes again after an unset.');
  196. });
  197. test("Model: using a non-default id attribute.", function() {
  198. var MongoModel = Backbone.Model.extend({idAttribute : '_id'});
  199. var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
  200. equal(model.get('id'), 'eye-dee');
  201. equal(model.id, 25);
  202. equal(model.isNew(), false);
  203. model.unset('_id');
  204. equal(model.id, undefined);
  205. equal(model.isNew(), true);
  206. });
  207. test("Model: set an empty string", function() {
  208. var model = new Backbone.Model({name : "Model"});
  209. model.set({name : ''});
  210. equal(model.get('name'), '');
  211. });
  212. test("Model: clear", function() {
  213. var changed;
  214. var model = new Backbone.Model({id: 1, name : "Model"});
  215. model.on("change:name", function(){ changed = true; });
  216. model.on("change", function() {
  217. var changedAttrs = model.changedAttributes();
  218. ok('name' in changedAttrs);
  219. });
  220. model.clear();
  221. equal(changed, true);
  222. equal(model.get('name'), undefined);
  223. });
  224. test("Model: defaults", function() {
  225. var Defaulted = Backbone.Model.extend({
  226. defaults: {
  227. "one": 1,
  228. "two": 2
  229. }
  230. });
  231. var model = new Defaulted({two: null});
  232. equal(model.get('one'), 1);
  233. equal(model.get('two'), null);
  234. Defaulted = Backbone.Model.extend({
  235. defaults: function() {
  236. return {
  237. "one": 3,
  238. "two": 4
  239. };
  240. }
  241. });
  242. var model = new Defaulted({two: null});
  243. equal(model.get('one'), 3);
  244. equal(model.get('two'), null);
  245. });
  246. test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
  247. var model = new Backbone.Model({name : "Tim", age : 10});
  248. equal(model.changedAttributes(), false);
  249. model.on('change', function() {
  250. ok(model.hasChanged('name'), 'name changed');
  251. ok(!model.hasChanged('age'), 'age did not');
  252. ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
  253. equal(model.previous('name'), 'Tim');
  254. ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
  255. });
  256. model.set({name : 'Rob'}, {silent : true});
  257. equal(model.hasChanged(), true);
  258. equal(model.hasChanged('name'), true);
  259. model.change();
  260. equal(model.get('name'), 'Rob');
  261. });
  262. test("Model: changedAttributes", function() {
  263. var model = new Backbone.Model({a: 'a', b: 'b'});
  264. equal(model.changedAttributes(), false);
  265. equal(model.changedAttributes({a: 'a'}), false);
  266. equal(model.changedAttributes({a: 'b'}).a, 'b');
  267. });
  268. test("Model: change with options", function() {
  269. var value;
  270. var model = new Backbone.Model({name: 'Rob'});
  271. model.on('change', function(model, options) {
  272. value = options.prefix + model.get('name');
  273. });
  274. model.set({name: 'Bob'}, {silent: true});
  275. model.change({prefix: 'Mr. '});
  276. equal(value, 'Mr. Bob');
  277. model.set({name: 'Sue'}, {prefix: 'Ms. '});
  278. equal(value, 'Ms. Sue');
  279. });
  280. test("Model: change after initialize", function () {
  281. var changed = 0;
  282. var attrs = {id: 1, label: 'c'};
  283. var obj = new Backbone.Model(attrs);
  284. obj.on('change', function() { changed += 1; });
  285. obj.set(attrs);
  286. equal(changed, 0);
  287. });
  288. test("Model: save within change event", function () {
  289. var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
  290. model.on('change', function () {
  291. model.save();
  292. ok(_.isEqual(lastRequest.model, model));
  293. });
  294. model.set({lastName: 'Hicks'});
  295. });
  296. test("Model: validate after save", function() {
  297. var lastError, model = new Backbone.Model();
  298. model.validate = function(attrs) {
  299. if (attrs.admin) return "Can't change admin status.";
  300. };
  301. model.sync = function(method, model, options) {
  302. options.success.call(this, {admin: true});
  303. };
  304. model.save(null, {error: function(model, error) {
  305. lastError = error;
  306. }});
  307. equal(lastError, "Can't change admin status.");
  308. });
  309. test("Model: isValid", function() {
  310. var model = new Backbone.Model({valid: true});
  311. model.validate = function(attrs) {
  312. if (!attrs.valid) return "invalid";
  313. };
  314. equal(model.isValid(), true);
  315. equal(model.set({valid: false}), false);
  316. equal(model.isValid(), true);
  317. ok(model.set('valid', false, {silent: true}));
  318. equal(model.isValid(), false);
  319. });
  320. test("Model: save", function() {
  321. doc.save({title : "Henry V"});
  322. equal(lastRequest.method, 'update');
  323. ok(_.isEqual(lastRequest.model, doc));
  324. });
  325. test("Model: save in positional style", function() {
  326. var model = new Backbone.Model();
  327. model.sync = function(method, model, options) {
  328. options.success();
  329. };
  330. model.save('title', 'Twelfth Night');
  331. equal(model.get('title'), 'Twelfth Night');
  332. });
  333. test("Model: fetch", function() {
  334. doc.fetch();
  335. equal(lastRequest.method, 'read');
  336. ok(_.isEqual(lastRequest.model, doc));
  337. });
  338. test("Model: destroy", function() {
  339. doc.destroy();
  340. equal(lastRequest.method, 'delete');
  341. ok(_.isEqual(lastRequest.model, doc));
  342. var newModel = new Backbone.Model;
  343. equal(newModel.destroy(), false);
  344. });
  345. test("Model: non-persisted destroy", function() {
  346. var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
  347. a.sync = function() { throw "should not be called"; };
  348. a.destroy();
  349. ok(true, "non-persisted model should not call sync");
  350. });
  351. test("Model: validate", function() {
  352. var lastError;
  353. var model = new Backbone.Model();
  354. model.validate = function(attrs) {
  355. if (attrs.admin != this.get('admin')) return "Can't change admin status.";
  356. };
  357. model.on('error', function(model, error) {
  358. lastError = error;
  359. });
  360. var result = model.set({a: 100});
  361. equal(result, model);
  362. equal(model.get('a'), 100);
  363. equal(lastError, undefined);
  364. result = model.set({admin: true}, {silent: true});
  365. equal(model.get('admin'), true);
  366. result = model.set({a: 200, admin: false});
  367. equal(lastError, "Can't change admin status.");
  368. equal(result, false);
  369. equal(model.get('a'), 100);
  370. });
  371. test("Model: validate on unset and clear", function() {
  372. var error;
  373. var model = new Backbone.Model({name: "One"});
  374. model.validate = function(attrs) {
  375. if (!attrs.name) {
  376. error = true;
  377. return "No thanks.";
  378. }
  379. };
  380. model.set({name: "Two"});
  381. equal(model.get('name'), 'Two');
  382. equal(error, undefined);
  383. model.unset('name');
  384. equal(error, true);
  385. equal(model.get('name'), 'Two');
  386. model.clear();
  387. equal(model.get('name'), 'Two');
  388. delete model.validate;
  389. model.clear();
  390. equal(model.get('name'), undefined);
  391. });
  392. test("Model: validate with error callback", function() {
  393. var lastError, boundError;
  394. var model = new Backbone.Model();
  395. model.validate = function(attrs) {
  396. if (attrs.admin) return "Can't change admin status.";
  397. };
  398. var callback = function(model, error) {
  399. lastError = error;
  400. };
  401. model.on('error', function(model, error) {
  402. boundError = true;
  403. });
  404. var result = model.set({a: 100}, {error: callback});
  405. equal(result, model);
  406. equal(model.get('a'), 100);
  407. equal(lastError, undefined);
  408. equal(boundError, undefined);
  409. result = model.set({a: 200, admin: true}, {error: callback});
  410. equal(result, false);
  411. equal(model.get('a'), 100);
  412. equal(lastError, "Can't change admin status.");
  413. equal(boundError, undefined);
  414. });
  415. test("Model: defaults always extend attrs (#459)", function() {
  416. var Defaulted = Backbone.Model.extend({
  417. defaults: {one: 1},
  418. initialize : function(attrs, opts) {
  419. equal(this.attributes.one, 1);
  420. }
  421. });
  422. var providedattrs = new Defaulted({});
  423. var emptyattrs = new Defaulted();
  424. });
  425. test("Model: Inherit class properties", function() {
  426. var Parent = Backbone.Model.extend({
  427. instancePropSame: function() {},
  428. instancePropDiff: function() {}
  429. }, {
  430. classProp: function() {}
  431. });
  432. var Child = Parent.extend({
  433. instancePropDiff: function() {}
  434. });
  435. var adult = new Parent;
  436. var kid = new Child;
  437. equal(Child.classProp, Parent.classProp);
  438. notEqual(Child.classProp, undefined);
  439. equal(kid.instancePropSame, adult.instancePropSame);
  440. notEqual(kid.instancePropSame, undefined);
  441. notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
  442. notEqual(Child.prototype.instancePropDiff, undefined);
  443. });
  444. test("Model: Nested change events don't clobber previous attributes", function() {
  445. var A = Backbone.Model.extend({
  446. initialize: function() {
  447. this.on("change:state", function(a, newState) {
  448. equal(a.previous('state'), undefined);
  449. equal(newState, 'hello');
  450. // Fire a nested change event.
  451. this.set({ other: "whatever" });
  452. });
  453. }
  454. });
  455. var B = Backbone.Model.extend({
  456. initialize: function() {
  457. this.get("a").on("change:state", function(a, newState) {
  458. equal(a.previous('state'), undefined);
  459. equal(newState, 'hello');
  460. });
  461. }
  462. });
  463. var a = new A();
  464. var b = new B({a: a});
  465. a.set({state: 'hello'});
  466. });
  467. test("hasChanged/set should use same comparison", function() {
  468. expect(2);
  469. var changed = 0, model = new Backbone.Model({a: null});
  470. model.on('change', function() {
  471. ok(this.hasChanged('a'));
  472. })
  473. .on('change:a', function() {
  474. changed++;
  475. })
  476. .set({a: undefined});
  477. equal(changed, 1);
  478. });
  479. test("#582, #425, change:attribute callbacks should fire after all changes have occurred", 9, function() {
  480. var model = new Backbone.Model;
  481. var assertion = function() {
  482. equal(model.get('a'), 'a');
  483. equal(model.get('b'), 'b');
  484. equal(model.get('c'), 'c');
  485. };
  486. model.on('change:a', assertion);
  487. model.on('change:b', assertion);
  488. model.on('change:c', assertion);
  489. model.set({a: 'a', b: 'b', c: 'c'});
  490. });
  491. test("#871, set with attributes property", function() {
  492. var model = new Backbone.Model();
  493. model.set({attributes: true});
  494. ok(model.has('attributes'));
  495. });
  496. test("set value regardless of equality/change", function() {
  497. var model = new Backbone.Model({x: []});
  498. var a = [];
  499. model.set({x: a});
  500. ok(model.get('x') === a);
  501. });
  502. test("unset fires change for undefined attributes", 1, function() {
  503. var model = new Backbone.Model({x: undefined});
  504. model.on('change:x', function(){ ok(true); });
  505. model.unset('x');
  506. });
  507. test("set: undefined values", function() {
  508. var model = new Backbone.Model({x: undefined});
  509. ok('x' in model.attributes);
  510. });
  511. test("change fires change:attr", 1, function() {
  512. var model = new Backbone.Model({x: 1});
  513. model.set({x: 2}, {silent: true});
  514. model.on('change:x', function(){ ok(true); });
  515. model.change();
  516. });
  517. test("hasChanged is false after original values are set", function() {
  518. var model = new Backbone.Model({x: 1});
  519. model.on('change:x', function(){ ok(false); });
  520. model.set({x: 2}, {silent: true});
  521. ok(model.hasChanged());
  522. model.set({x: 1}, {silent: true});
  523. ok(!model.hasChanged());
  524. });
  525. test("save with `wait` succeeds without `validate`", function() {
  526. var model = new Backbone.Model();
  527. model.save({x: 1}, {wait: true});
  528. ok(lastRequest.model === model);
  529. });
  530. test("`hasChanged` for falsey keys", function() {
  531. var model = new Backbone.Model();
  532. model.set({x: true}, {silent: true});
  533. ok(!model.hasChanged(0));
  534. ok(!model.hasChanged(''));
  535. });
  536. test("`previous` for falsey keys", function() {
  537. var model = new Backbone.Model({0: true, '': true});
  538. model.set({0: false, '': false}, {silent: true});
  539. equal(model.previous(0), true);
  540. equal(model.previous(''), true);
  541. });
  542. test("`save` with `wait` sends correct attributes", function() {
  543. var changed = 0;
  544. var model = new Backbone.Model({x: 1, y: 2});
  545. model.on('change:x', function() { changed++; });
  546. model.save({x: 3}, {wait: true});
  547. deepEqual(JSON.parse(ajaxParams.data), {x: 3, y: 2});
  548. equal(model.get('x'), 1);
  549. equal(changed, 0);
  550. lastRequest.options.success({});
  551. equal(model.get('x'), 3);
  552. equal(changed, 1);
  553. });
  554. test("`save` with `wait` results in correct attributes if success is called during sync", function() {
  555. var changed = 0;
  556. var model = new Backbone.Model({x: 1, y: 2});
  557. model.sync = function(method, model, options) {
  558. options.success();
  559. };
  560. model.on("change:x", function() { changed++; });
  561. model.save({x: 3}, {wait: true});
  562. equal(model.get('x'), 3);
  563. equal(changed, 1);
  564. });
  565. test("save with wait validates attributes", 1, function() {
  566. var model = new Backbone.Model();
  567. model.validate = function() { ok(true); };
  568. model.save({x: 1}, {wait: true});
  569. });
  570. test("nested `set` during `'change:attr'`", function() {
  571. var events = [];
  572. var model = new Backbone.Model();
  573. model.on('all', function(event) { events.push(event); });
  574. model.on('change', function() {
  575. model.set({z: true}, {silent:true});
  576. });
  577. model.on('change:x', function() {
  578. model.set({y: true});
  579. });
  580. model.set({x: true});
  581. deepEqual(events, ['change:y', 'change:x', 'change']);
  582. events = [];
  583. model.change();
  584. deepEqual(events, ['change:z', 'change']);
  585. });
  586. test("nested `change` only fires once", 1, function() {
  587. var model = new Backbone.Model();
  588. model.on('change', function() {
  589. ok(true);
  590. model.change();
  591. });
  592. model.set({x: true});
  593. });
  594. test("no `'change'` event if no changes", function() {
  595. var model = new Backbone.Model();
  596. model.on('change', function() { ok(false); });
  597. model.change();
  598. });
  599. test("nested `set` during `'change'`", 6, function() {
  600. var count = 0;
  601. var model = new Backbone.Model();
  602. model.on('change', function() {
  603. switch(count++) {
  604. case 0:
  605. deepEqual(this.changedAttributes(), {x: true});
  606. equal(model.previous('x'), undefined);
  607. model.set({y: true});
  608. break;
  609. case 1:
  610. deepEqual(this.changedAttributes(), {y: true});
  611. equal(model.previous('x'), true);
  612. model.set({z: true});
  613. break;
  614. case 2:
  615. deepEqual(this.changedAttributes(), {z: true});
  616. equal(model.previous('y'), true);
  617. break;
  618. default:
  619. ok(false);
  620. }
  621. });
  622. model.set({x: true});
  623. });
  624. test("nested `'change'` with silent", 3, function() {
  625. var count = 0;
  626. var model = new Backbone.Model();
  627. model.on('change:y', function() { ok(true); });
  628. model.on('change', function() {
  629. switch(count++) {
  630. case 0:
  631. deepEqual(this.changedAttributes(), {x: true});
  632. model.set({y: true}, {silent: true});
  633. break;
  634. case 1:
  635. deepEqual(this.changedAttributes(), {y: true, z: true});
  636. break;
  637. default:
  638. ok(false);
  639. }
  640. });
  641. model.set({x: true});
  642. model.set({z: true});
  643. });
  644. test("nested `'change:attr'` with silent", 1, function() {
  645. var model = new Backbone.Model();
  646. model.on('change:y', function(){ ok(true); });
  647. model.on('change', function() {
  648. model.set({y: true}, {silent: true});
  649. model.set({z: true});
  650. });
  651. model.set({x: true});
  652. });
  653. test("multiple nested changes with silent", 1, function() {
  654. var model = new Backbone.Model();
  655. model.on('change:x', function() {
  656. model.set({y: 1}, {silent: true});
  657. model.set({y: 2});
  658. });
  659. model.on('change:y', function(model, val) {
  660. equal(val, 2);
  661. });
  662. model.set({x: true});
  663. model.change();
  664. });
  665. test("multiple nested changes with silent", function() {
  666. var changes = [];
  667. var model = new Backbone.Model();
  668. model.on('change:b', function(model, val) { changes.push(val); });
  669. model.on('change', function() {
  670. model.set({b: 1});
  671. model.set({b: 2}, {silent: true});
  672. });
  673. model.set({b: 0});
  674. deepEqual(changes, [0, 1, 1]);
  675. model.change();
  676. deepEqual(changes, [0, 1, 1, 2, 1]);
  677. });
  678. test("nested set multiple times", 1, function() {
  679. var model = new Backbone.Model();
  680. model.on('change:b', function() {
  681. ok(true);
  682. });
  683. model.on('change:a', function() {
  684. model.set({b: true});
  685. model.set({b: true});
  686. });
  687. model.set({a: true});
  688. });
  689. test("Backbone.wrapError triggers `'error'`", 12, function() {
  690. var resp = {};
  691. var options = {};
  692. var model = new Backbone.Model();
  693. model.on('error', error);
  694. var callback = Backbone.wrapError(null, model, options);
  695. callback(model, resp);
  696. callback(resp);
  697. callback = Backbone.wrapError(error, model, options);
  698. callback(model, resp);
  699. callback(resp);
  700. function error(_model, _resp, _options) {
  701. ok(model === _model);
  702. ok(resp === _resp);
  703. ok(options === _options);
  704. }
  705. });
  706. // reset the Model
  707. Backbone.Model = oldModel;
  708. });