PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/test/model.js

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