PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/collection.js

https://github.com/NickGardner/backbone
JavaScript | 782 lines | 705 code | 77 blank | 0 comment | 20 complexity | a6fb3f4ba757b273d028e7dff720f625 MD5 | raw file
  1. $(document).ready(function() {
  2. var a, b, c, d, e, col, otherCol;
  3. module("Backbone.Collection", _.extend(new Environment, {
  4. setup: function() {
  5. Environment.prototype.setup.apply(this, arguments);
  6. a = new Backbone.Model({id: 3, label: 'a'});
  7. b = new Backbone.Model({id: 2, label: 'b'});
  8. c = new Backbone.Model({id: 1, label: 'c'});
  9. d = new Backbone.Model({id: 0, label: 'd'});
  10. e = null;
  11. col = new Backbone.Collection([a,b,c,d]);
  12. otherCol = new Backbone.Collection();
  13. }
  14. }));
  15. test("new and sort", 7, function() {
  16. equal(col.first(), a, "a should be first");
  17. equal(col.last(), d, "d should be last");
  18. col.comparator = function(a, b) {
  19. return a.id > b.id ? -1 : 1;
  20. };
  21. col.sort();
  22. equal(col.first(), a, "a should be first");
  23. equal(col.last(), d, "d should be last");
  24. col.comparator = function(model) { return model.id; };
  25. col.sort();
  26. equal(col.first(), d, "d should be first");
  27. equal(col.last(), a, "a should be last");
  28. equal(col.length, 4);
  29. });
  30. test("String comparator.", 1, function() {
  31. var collection = new Backbone.Collection([
  32. {id: 3},
  33. {id: 1},
  34. {id: 2}
  35. ], {comparator: 'id'});
  36. deepEqual(collection.pluck('id'), [1, 2, 3]);
  37. });
  38. test("new and parse", 3, function() {
  39. var Collection = Backbone.Collection.extend({
  40. parse : function(data) {
  41. return _.filter(data, function(datum) {
  42. return datum.a % 2 === 0;
  43. });
  44. }
  45. });
  46. var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
  47. var collection = new Collection(models, {parse: true});
  48. strictEqual(collection.length, 2);
  49. strictEqual(collection.first().get('a'), 2);
  50. strictEqual(collection.last().get('a'), 4);
  51. });
  52. test("get, getByCid", 3, function() {
  53. equal(col.get(0), d);
  54. equal(col.get(2), b);
  55. equal(col.getByCid(col.first().cid), col.first());
  56. });
  57. test("get with non-default ids", 2, function() {
  58. var col = new Backbone.Collection();
  59. var MongoModel = Backbone.Model.extend({
  60. idAttribute: '_id'
  61. });
  62. var model = new MongoModel({_id: 100});
  63. col.push(model);
  64. equal(col.get(100), model);
  65. model.set({_id: 101});
  66. equal(col.get(101), model);
  67. });
  68. test("update index when id changes", 3, function() {
  69. var col = new Backbone.Collection();
  70. col.add([
  71. {id : 0, name : 'one'},
  72. {id : 1, name : 'two'}
  73. ]);
  74. var one = col.get(0);
  75. equal(one.get('name'), 'one');
  76. one.set({id : 101});
  77. equal(col.get(0), null);
  78. equal(col.get(101).get('name'), 'one');
  79. });
  80. test("at", 1, function() {
  81. equal(col.at(2), c);
  82. });
  83. test("pluck", 1, function() {
  84. equal(col.pluck('label').join(' '), 'a b c d');
  85. });
  86. test("add", 10, function() {
  87. var added, opts, secondAdded;
  88. added = opts = secondAdded = null;
  89. e = new Backbone.Model({id: 10, label : 'e'});
  90. otherCol.add(e);
  91. otherCol.on('add', function() {
  92. secondAdded = true;
  93. });
  94. col.on('add', function(model, collection, options){
  95. added = model.get('label');
  96. opts = options;
  97. });
  98. col.add(e, {amazing: true});
  99. equal(added, 'e');
  100. equal(col.length, 5);
  101. equal(col.last(), e);
  102. equal(otherCol.length, 1);
  103. equal(secondAdded, null);
  104. ok(opts.amazing);
  105. var f = new Backbone.Model({id: 20, label : 'f'});
  106. var g = new Backbone.Model({id: 21, label : 'g'});
  107. var h = new Backbone.Model({id: 22, label : 'h'});
  108. var atCol = new Backbone.Collection([f, g, h]);
  109. equal(atCol.length, 3);
  110. atCol.add(e, {at: 1});
  111. equal(atCol.length, 4);
  112. equal(atCol.at(1), e);
  113. equal(atCol.last(), h);
  114. });
  115. test("add multiple models", 6, function() {
  116. var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
  117. col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
  118. for (var i = 0; i <= 5; i++) {
  119. equal(col.at(i).get('at'), i);
  120. }
  121. });
  122. test("add; at should have preference over comparator", 1, function() {
  123. var Col = Backbone.Collection.extend({
  124. comparator: function(a,b) {
  125. return a.id > b.id ? -1 : 1;
  126. }
  127. });
  128. var col = new Col([{id: 2}, {id: 3}]);
  129. col.add(new Backbone.Model({id: 1}), {at: 1});
  130. equal(col.pluck('id').join(' '), '3 1 2');
  131. });
  132. test("can't add model to collection twice", function() {
  133. var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
  134. equal(col.pluck('id').join(' '), '1 2 3');
  135. });
  136. test("can't add different model with same id to collection twice", 1, function() {
  137. var col = new Backbone.Collection;
  138. col.unshift({id: 101});
  139. col.add({id: 101});
  140. equal(col.length, 1);
  141. });
  142. test("merge in duplicate models with {merge: true}", 3, function() {
  143. var col = new Backbone.Collection;
  144. col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]);
  145. col.add({id: 1, name: 'Moses'});
  146. equal(col.first().get('name'), 'Moe');
  147. col.add({id: 1, name: 'Moses'}, {merge: true});
  148. equal(col.first().get('name'), 'Moses');
  149. col.add({id: 1, name: 'Tim'}, {merge: true, silent: true});
  150. equal(col.first().get('name'), 'Tim');
  151. });
  152. test("add model to multiple collections", 10, function() {
  153. var counter = 0;
  154. var e = new Backbone.Model({id: 10, label : 'e'});
  155. e.on('add', function(model, collection) {
  156. counter++;
  157. equal(e, model);
  158. if (counter > 1) {
  159. equal(collection, colF);
  160. } else {
  161. equal(collection, colE);
  162. }
  163. });
  164. var colE = new Backbone.Collection([]);
  165. colE.on('add', function(model, collection) {
  166. equal(e, model);
  167. equal(colE, collection);
  168. });
  169. var colF = new Backbone.Collection([]);
  170. colF.on('add', function(model, collection) {
  171. equal(e, model);
  172. equal(colF, collection);
  173. });
  174. colE.add(e);
  175. equal(e.collection, colE);
  176. colF.add(e);
  177. equal(e.collection, colE);
  178. });
  179. test("add model with parse", 1, function() {
  180. var Model = Backbone.Model.extend({
  181. parse: function(obj) {
  182. obj.value += 1;
  183. return obj;
  184. }
  185. });
  186. var Col = Backbone.Collection.extend({model: Model});
  187. var col = new Col;
  188. col.add({value: 1}, {parse: true});
  189. equal(col.at(0).get('value'), 2);
  190. });
  191. test("add model to collection with sort()-style comparator", 3, function() {
  192. var col = new Backbone.Collection;
  193. col.comparator = function(a, b) {
  194. return a.get('name') < b.get('name') ? -1 : 1;
  195. };
  196. var tom = new Backbone.Model({name: 'Tom'});
  197. var rob = new Backbone.Model({name: 'Rob'});
  198. var tim = new Backbone.Model({name: 'Tim'});
  199. col.add(tom);
  200. col.add(rob);
  201. col.add(tim);
  202. equal(col.indexOf(rob), 0);
  203. equal(col.indexOf(tim), 1);
  204. equal(col.indexOf(tom), 2);
  205. });
  206. test("comparator that depends on `this`", 2, function() {
  207. var col = new Backbone.Collection;
  208. col.negative = function(num) {
  209. return -num;
  210. };
  211. col.comparator = function(a) {
  212. return this.negative(a.id);
  213. };
  214. col.add([{id: 1}, {id: 2}, {id: 3}]);
  215. deepEqual(col.pluck('id'), [3, 2, 1]);
  216. col.comparator = function(a, b) {
  217. return this.negative(b.id) - this.negative(a.id);
  218. };
  219. col.sort();
  220. deepEqual(col.pluck('id'), [1, 2, 3]);
  221. });
  222. test("remove", 5, function() {
  223. var removed = null;
  224. var otherRemoved = null;
  225. col.on('remove', function(model, col, options) {
  226. removed = model.get('label');
  227. equal(options.index, 3);
  228. });
  229. otherCol.on('remove', function(model, col, options) {
  230. otherRemoved = true;
  231. });
  232. col.remove(d);
  233. equal(removed, 'd');
  234. equal(col.length, 3);
  235. equal(col.first(), a);
  236. equal(otherRemoved, null);
  237. });
  238. test("shift and pop", 2, function() {
  239. var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  240. equal(col.shift().get('a'), 'a');
  241. equal(col.pop().get('c'), 'c');
  242. });
  243. test("slice", 2, function() {
  244. var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  245. var array = col.slice(1, 3);
  246. equal(array.length, 2);
  247. equal(array[0].get('b'), 'b');
  248. });
  249. test("events are unbound on remove", 3, function() {
  250. var counter = 0;
  251. var dj = new Backbone.Model();
  252. var emcees = new Backbone.Collection([dj]);
  253. emcees.on('change', function(){ counter++; });
  254. dj.set({name : 'Kool'});
  255. equal(counter, 1);
  256. emcees.reset([]);
  257. equal(dj.collection, undefined);
  258. dj.set({name : 'Shadow'});
  259. equal(counter, 1);
  260. });
  261. test("remove in multiple collections", 7, function() {
  262. var modelData = {
  263. id : 5,
  264. title : 'Othello'
  265. };
  266. var passed = false;
  267. var e = new Backbone.Model(modelData);
  268. var f = new Backbone.Model(modelData);
  269. f.on('remove', function() {
  270. passed = true;
  271. });
  272. var colE = new Backbone.Collection([e]);
  273. var colF = new Backbone.Collection([f]);
  274. ok(e != f);
  275. ok(colE.length == 1);
  276. ok(colF.length == 1);
  277. colE.remove(e);
  278. equal(passed, false);
  279. ok(colE.length == 0);
  280. colF.remove(e);
  281. ok(colF.length == 0);
  282. equal(passed, true);
  283. });
  284. test("remove same model in multiple collection", 16, function() {
  285. var counter = 0;
  286. var e = new Backbone.Model({id: 5, title: 'Othello'});
  287. e.on('remove', function(model, collection) {
  288. counter++;
  289. equal(e, model);
  290. if (counter > 1) {
  291. equal(collection, colE);
  292. } else {
  293. equal(collection, colF);
  294. }
  295. });
  296. var colE = new Backbone.Collection([e]);
  297. colE.on('remove', function(model, collection) {
  298. equal(e, model);
  299. equal(colE, collection);
  300. });
  301. var colF = new Backbone.Collection([e]);
  302. colF.on('remove', function(model, collection) {
  303. equal(e, model);
  304. equal(colF, collection);
  305. });
  306. equal(colE, e.collection);
  307. colF.remove(e);
  308. ok(colF.length == 0);
  309. ok(colE.length == 1);
  310. equal(counter, 1);
  311. equal(colE, e.collection);
  312. colE.remove(e);
  313. equal(null, e.collection);
  314. ok(colE.length == 0);
  315. equal(counter, 2);
  316. });
  317. test("model destroy removes from all collections", 3, function() {
  318. var e = new Backbone.Model({id: 5, title: 'Othello'});
  319. e.sync = function(method, model, options) { options.success({}); };
  320. var colE = new Backbone.Collection([e]);
  321. var colF = new Backbone.Collection([e]);
  322. e.destroy();
  323. ok(colE.length == 0);
  324. ok(colF.length == 0);
  325. equal(undefined, e.collection);
  326. });
  327. test("Colllection: non-persisted model destroy removes from all collections", 3, function() {
  328. var e = new Backbone.Model({title: 'Othello'});
  329. e.sync = function(method, model, options) { throw "should not be called"; };
  330. var colE = new Backbone.Collection([e]);
  331. var colF = new Backbone.Collection([e]);
  332. e.destroy();
  333. ok(colE.length == 0);
  334. ok(colF.length == 0);
  335. equal(undefined, e.collection);
  336. });
  337. test("fetch", 4, function() {
  338. var collection = new Backbone.Collection;
  339. collection.url = '/test';
  340. collection.fetch();
  341. equal(this.syncArgs.method, 'read');
  342. equal(this.syncArgs.model, collection);
  343. equal(this.syncArgs.options.parse, true);
  344. collection.fetch({parse: false});
  345. equal(this.syncArgs.options.parse, false);
  346. });
  347. test("create", 4, function() {
  348. var collection = new Backbone.Collection;
  349. collection.url = '/test';
  350. var model = collection.create({label: 'f'}, {wait: true});
  351. equal(this.syncArgs.method, 'create');
  352. equal(this.syncArgs.model, model);
  353. equal(model.get('label'), 'f');
  354. equal(model.collection, collection);
  355. });
  356. test("create enforces validation", 1, function() {
  357. var ValidatingModel = Backbone.Model.extend({
  358. validate: function(attrs) {
  359. return "fail";
  360. }
  361. });
  362. var ValidatingCollection = Backbone.Collection.extend({
  363. model: ValidatingModel
  364. });
  365. var col = new ValidatingCollection();
  366. equal(col.create({"foo":"bar"}), false);
  367. });
  368. test("a failing create runs the error callback", 1, function() {
  369. var ValidatingModel = Backbone.Model.extend({
  370. validate: function(attrs) {
  371. return "fail";
  372. }
  373. });
  374. var ValidatingCollection = Backbone.Collection.extend({
  375. model: ValidatingModel
  376. });
  377. var flag = false;
  378. var callback = function(model, error) { flag = true; };
  379. var col = new ValidatingCollection();
  380. col.create({"foo":"bar"}, { error: callback });
  381. equal(flag, true);
  382. });
  383. test("initialize", 1, function() {
  384. var Collection = Backbone.Collection.extend({
  385. initialize: function() {
  386. this.one = 1;
  387. }
  388. });
  389. var coll = new Collection;
  390. equal(coll.one, 1);
  391. });
  392. test("toJSON", 1, function() {
  393. equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
  394. });
  395. test("where", 6, function() {
  396. var coll = new Backbone.Collection([
  397. {a: 1},
  398. {a: 1},
  399. {a: 1, b: 2},
  400. {a: 2, b: 2},
  401. {a: 3}
  402. ]);
  403. equal(coll.where({a: 1}).length, 3);
  404. equal(coll.where({a: 2}).length, 1);
  405. equal(coll.where({a: 3}).length, 1);
  406. equal(coll.where({b: 1}).length, 0);
  407. equal(coll.where({b: 2}).length, 2);
  408. equal(coll.where({a: 1, b: 2}).length, 1);
  409. });
  410. test("Underscore methods", 13, function() {
  411. equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
  412. equal(col.any(function(model){ return model.id === 100; }), false);
  413. equal(col.any(function(model){ return model.id === 0; }), true);
  414. equal(col.indexOf(b), 1);
  415. equal(col.size(), 4);
  416. equal(col.rest().length, 3);
  417. ok(!_.include(col.rest()), a);
  418. ok(!_.include(col.rest()), d);
  419. ok(!col.isEmpty());
  420. ok(!_.include(col.without(d)), d);
  421. equal(col.max(function(model){ return model.id; }).id, 3);
  422. equal(col.min(function(model){ return model.id; }).id, 0);
  423. deepEqual(col.chain()
  424. .filter(function(o){ return o.id % 2 === 0; })
  425. .map(function(o){ return o.id * 2; })
  426. .value(),
  427. [4, 0]);
  428. });
  429. test("reset", 10, function() {
  430. var resetCount = 0;
  431. var models = col.models;
  432. col.on('reset', function() { resetCount += 1; });
  433. col.reset([]);
  434. equal(resetCount, 1);
  435. equal(col.length, 0);
  436. equal(col.last(), null);
  437. col.reset(models);
  438. equal(resetCount, 2);
  439. equal(col.length, 4);
  440. equal(col.last(), d);
  441. col.reset(_.map(models, function(m){ return m.attributes; }));
  442. equal(resetCount, 3);
  443. equal(col.length, 4);
  444. ok(col.last() !== d);
  445. ok(_.isEqual(col.last().attributes, d.attributes));
  446. });
  447. test("reset passes caller options", 3, function() {
  448. var Model = Backbone.Model.extend({
  449. initialize: function(attrs, options) {
  450. this.model_parameter = options.model_parameter;
  451. }
  452. });
  453. var col = new (Backbone.Collection.extend({ model: Model }))();
  454. col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 }], { model_parameter: 'model parameter' });
  455. equal(col.length, 2);
  456. col.each(function(model) {
  457. equal(model.model_parameter, 'model parameter');
  458. });
  459. });
  460. test("trigger custom events on models", 1, function() {
  461. var fired = null;
  462. a.on("custom", function() { fired = true; });
  463. a.trigger("custom");
  464. equal(fired, true);
  465. });
  466. test("add does not alter arguments", 2, function(){
  467. var attrs = {};
  468. var models = [attrs];
  469. new Backbone.Collection().add(models);
  470. equal(models.length, 1);
  471. ok(attrs === models[0]);
  472. });
  473. test("#714: access `model.collection` in a brand new model.", 2, function() {
  474. var collection = new Backbone.Collection;
  475. collection.url = '/test';
  476. var Model = Backbone.Model.extend({
  477. set: function(attrs) {
  478. equal(attrs.prop, 'value');
  479. equal(this.collection, collection);
  480. return this;
  481. }
  482. });
  483. collection.model = Model;
  484. collection.create({prop: 'value'});
  485. });
  486. test("#574, remove its own reference to the .models array.", 2, function() {
  487. var col = new Backbone.Collection([
  488. {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
  489. ]);
  490. equal(col.length, 6);
  491. col.remove(col.models);
  492. equal(col.length, 0);
  493. });
  494. test("#861, adding models to a collection which do not pass validation", function() {
  495. var Model = Backbone.Model.extend({
  496. validate: function(attrs) {
  497. if (attrs.id == 3) return "id can't be 3";
  498. }
  499. });
  500. var Collection = Backbone.Collection.extend({
  501. model: Model
  502. });
  503. var collection = new Collection;
  504. collection.on("error", function() { ok(true); });
  505. collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
  506. deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
  507. });
  508. test("Invalid models are discarded.", 5, function() {
  509. var collection = new Backbone.Collection;
  510. collection.on('test', function() { ok(true); });
  511. collection.model = Backbone.Model.extend({
  512. validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
  513. });
  514. var model = new collection.model({id: 1, valid: true});
  515. collection.add([model, {id: 2}]);;
  516. model.trigger('test');
  517. ok(collection.getByCid(model.cid));
  518. ok(collection.get(1));
  519. ok(!collection.get(2));
  520. equal(collection.length, 1);
  521. });
  522. test("multiple copies of the same model", 3, function() {
  523. var col = new Backbone.Collection();
  524. var model = new Backbone.Model();
  525. col.add([model, model]);
  526. equal(col.length, 1);
  527. col.add([{id: 1}, {id: 1}]);
  528. equal(col.length, 2);
  529. equal(col.last().id, 1);
  530. });
  531. test("#964 - collection.get return inconsistent", 2, function() {
  532. var c = new Backbone.Collection();
  533. ok(c.get(null) === undefined);
  534. ok(c.get() === undefined);
  535. });
  536. test("#1112 - passing options.model sets collection.model", 2, function() {
  537. var Model = Backbone.Model.extend({});
  538. var c = new Backbone.Collection([{id: 1}], {model: Model});
  539. ok(c.model === Model);
  540. ok(c.at(0) instanceof Model);
  541. });
  542. test("null and undefined are invalid ids.", 2, function() {
  543. var model = new Backbone.Model({id: 1});
  544. var collection = new Backbone.Collection([model]);
  545. model.set({id: null});
  546. ok(!collection.get('null'));
  547. model.set({id: 1});
  548. model.set({id: undefined});
  549. ok(!collection.get('undefined'));
  550. });
  551. test("falsy comparator", 4, function(){
  552. var Col = Backbone.Collection.extend({
  553. comparator: function(model){ return model.id; }
  554. });
  555. var col = new Col();
  556. var colFalse = new Col(null, {comparator: false});
  557. var colNull = new Col(null, {comparator: null});
  558. var colUndefined = new Col(null, {comparator: undefined});
  559. ok(col.comparator);
  560. ok(!colFalse.comparator);
  561. ok(!colNull.comparator);
  562. ok(colUndefined.comparator);
  563. });
  564. test("#1355 - `options` is passed to success callbacks", 2, function(){
  565. var m = new Backbone.Model({x:1});
  566. var col = new Backbone.Collection();
  567. var opts = {
  568. success: function(collection, resp, options){
  569. ok(options);
  570. }
  571. };
  572. col.sync = m.sync = function( method, collection, options ){
  573. options.success();
  574. };
  575. col.fetch(opts);
  576. col.create(m, opts);
  577. });
  578. test("#1412 - Trigger 'sync' event.", 2, function() {
  579. var collection = new Backbone.Collection;
  580. collection.url = '/test';
  581. collection.on('sync', function() { ok(true); });
  582. Backbone.ajax = function(settings){ settings.success(); };
  583. collection.fetch();
  584. collection.create({id: 1});
  585. });
  586. test("#1447 - create with wait adds model.", 1, function() {
  587. var collection = new Backbone.Collection;
  588. var model = new Backbone.Model;
  589. model.sync = function(method, model, options){ options.success(); };
  590. collection.on('add', function(){ ok(true); });
  591. collection.create(model, {wait: true});
  592. });
  593. test("#1448 - add sorts collection after merge.", 1, function() {
  594. var collection = new Backbone.Collection([
  595. {id: 1, x: 1},
  596. {id: 2, x: 2}
  597. ]);
  598. collection.comparator = function(model){ return model.get('x'); };
  599. collection.add({id: 1, x: 3}, {merge: true});
  600. deepEqual(collection.pluck('id'), [2, 1]);
  601. });
  602. test("#1655 - groupBy can be used with a string argument.", 3, function() {
  603. var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
  604. var grouped = collection.groupBy('x');
  605. strictEqual(_.keys(grouped).length, 2);
  606. strictEqual(grouped[1][0].get('x'), 1);
  607. strictEqual(grouped[2][0].get('x'), 2);
  608. });
  609. test("#1655 - sortBy can be used with a string argument.", 1, function() {
  610. var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
  611. var values = _.map(collection.sortBy('x'), function(model) {
  612. return model.get('x');
  613. });
  614. deepEqual(values, [1, 2, 3]);
  615. });
  616. test("#1604 - Removal during iteration.", 0, function() {
  617. var collection = new Backbone.Collection([{}, {}]);
  618. collection.on('add', function() {
  619. collection.at(0).destroy();
  620. });
  621. collection.add({}, {at: 0});
  622. });
  623. test("#1638 - `sort` during `add` triggers correctly.", function() {
  624. var collection = new Backbone.Collection;
  625. collection.comparator = function(model) { return model.get('x'); };
  626. var added = [];
  627. collection.on('add', function(model) {
  628. model.set({x: 3});
  629. collection.sort();
  630. added.push(model.id);
  631. });
  632. collection.add([{id: 1, x: 1}, {id: 2, x: 2}]);
  633. deepEqual(added, [1, 2]);
  634. });
  635. test("fetch parses models by default", 1, function() {
  636. var model = {};
  637. var Collection = Backbone.Collection.extend({
  638. url: 'test',
  639. model: Backbone.Model.extend({
  640. parse: function(resp) {
  641. strictEqual(resp, model);
  642. }
  643. })
  644. });
  645. new Collection().fetch();
  646. this.ajaxSettings.success([model]);
  647. });
  648. test("`sort` shouldn't always fire on `add`", 1, function() {
  649. var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
  650. comparator: 'id'
  651. });
  652. c.sort = function(){ ok(true); };
  653. c.add([]);
  654. c.add({id: 1});
  655. c.add([{id: 2}, {id: 3}]);
  656. c.add({id: 4});
  657. });
  658. test("#1407 parse option on constructor parses collection and models", 2, function() {
  659. var model = {
  660. namespace : [{id: 1}, {id:2}]
  661. };
  662. var Collection = Backbone.Collection.extend({
  663. model: Backbone.Model.extend({
  664. parse: function(model) {
  665. model.name = 'test';
  666. return model;
  667. }
  668. }),
  669. parse: function(model) {
  670. return model.namespace;
  671. }
  672. });
  673. var c = new Collection(model, {parse:true});
  674. equal(c.length, 2);
  675. equal(c.at(0).get('name'), 'test');
  676. });
  677. test("#1407 parse option on reset parses collection and models", 2, function() {
  678. var model = {
  679. namespace : [{id: 1}, {id:2}]
  680. };
  681. var Collection = Backbone.Collection.extend({
  682. model: Backbone.Model.extend({
  683. parse: function(model) {
  684. model.name = 'test';
  685. return model;
  686. }
  687. }),
  688. parse: function(model) {
  689. return model.namespace;
  690. }
  691. });
  692. var c = new Collection();
  693. c.reset(model, {parse:true});
  694. equal(c.length, 2);
  695. equal(c.at(0).get('name'), 'test');
  696. });
  697. test("Reset includes previous models in triggered event.", 1, function() {
  698. var model = new Backbone.Model();
  699. var collection = new Backbone.Collection([model])
  700. .on('reset', function(collection, options) {
  701. deepEqual(options.previousModels, [model]);
  702. });
  703. collection.reset([]);
  704. });
  705. });