PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/model.js

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