PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bwr/components/backbone/test/collection.js

https://gitlab.com/Rockyspade/grunt-bower-concat
JavaScript | 1016 lines | 907 code | 102 blank | 7 comment | 10 complexity | 6b24045533c9430c3e5b6f1ad53d19ee 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", 9, function() {
  16. var counter = 0;
  17. col.on('sort', function(){ counter++; });
  18. equal(col.first(), a, "a should be first");
  19. equal(col.last(), d, "d should be last");
  20. col.comparator = function(a, b) {
  21. return a.id > b.id ? -1 : 1;
  22. };
  23. col.sort();
  24. equal(counter, 1);
  25. equal(col.first(), a, "a should be first");
  26. equal(col.last(), d, "d should be last");
  27. col.comparator = function(model) { return model.id; };
  28. col.sort();
  29. equal(counter, 2);
  30. equal(col.first(), d, "d should be first");
  31. equal(col.last(), a, "a should be last");
  32. equal(col.length, 4);
  33. });
  34. test("String comparator.", 1, function() {
  35. var collection = new Backbone.Collection([
  36. {id: 3},
  37. {id: 1},
  38. {id: 2}
  39. ], {comparator: 'id'});
  40. deepEqual(collection.pluck('id'), [1, 2, 3]);
  41. });
  42. test("new and parse", 3, function() {
  43. var Collection = Backbone.Collection.extend({
  44. parse : function(data) {
  45. return _.filter(data, function(datum) {
  46. return datum.a % 2 === 0;
  47. });
  48. }
  49. });
  50. var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
  51. var collection = new Collection(models, {parse: true});
  52. strictEqual(collection.length, 2);
  53. strictEqual(collection.first().get('a'), 2);
  54. strictEqual(collection.last().get('a'), 4);
  55. });
  56. test("get", 5, function() {
  57. equal(col.get(0), d);
  58. equal(col.get(2), b);
  59. equal(col.get({id: 1}), c);
  60. equal(col.get(c.clone()), c);
  61. equal(col.get(col.first().cid), col.first());
  62. });
  63. test("get with non-default ids", 4, function() {
  64. var col = new Backbone.Collection();
  65. var MongoModel = Backbone.Model.extend({
  66. idAttribute: '_id'
  67. });
  68. var model = new MongoModel({_id: 100});
  69. col.push(model);
  70. equal(col.get(100), model);
  71. model.set({_id: 101});
  72. equal(col.get(101), model);
  73. var Col2 = Backbone.Collection.extend({ model: MongoModel });
  74. var col2 = new Col2();
  75. col2.push(model);
  76. equal(col2.get({_id: 101}), model);
  77. equal(col2.get(model.clone()), model);
  78. });
  79. test("update index when id changes", 3, function() {
  80. var col = new Backbone.Collection();
  81. col.add([
  82. {id : 0, name : 'one'},
  83. {id : 1, name : 'two'}
  84. ]);
  85. var one = col.get(0);
  86. equal(one.get('name'), 'one');
  87. one.set({id : 101});
  88. equal(col.get(0), null);
  89. equal(col.get(101).get('name'), 'one');
  90. });
  91. test("at", 1, function() {
  92. equal(col.at(2), c);
  93. });
  94. test("pluck", 1, function() {
  95. equal(col.pluck('label').join(' '), 'a b c d');
  96. });
  97. test("add", 10, function() {
  98. var added, opts, secondAdded;
  99. added = opts = secondAdded = null;
  100. e = new Backbone.Model({id: 10, label : 'e'});
  101. otherCol.add(e);
  102. otherCol.on('add', function() {
  103. secondAdded = true;
  104. });
  105. col.on('add', function(model, collection, options){
  106. added = model.get('label');
  107. opts = options;
  108. });
  109. col.add(e, {amazing: true});
  110. equal(added, 'e');
  111. equal(col.length, 5);
  112. equal(col.last(), e);
  113. equal(otherCol.length, 1);
  114. equal(secondAdded, null);
  115. ok(opts.amazing);
  116. var f = new Backbone.Model({id: 20, label : 'f'});
  117. var g = new Backbone.Model({id: 21, label : 'g'});
  118. var h = new Backbone.Model({id: 22, label : 'h'});
  119. var atCol = new Backbone.Collection([f, g, h]);
  120. equal(atCol.length, 3);
  121. atCol.add(e, {at: 1});
  122. equal(atCol.length, 4);
  123. equal(atCol.at(1), e);
  124. equal(atCol.last(), h);
  125. });
  126. test("add multiple models", 6, function() {
  127. var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
  128. col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
  129. for (var i = 0; i <= 5; i++) {
  130. equal(col.at(i).get('at'), i);
  131. }
  132. });
  133. test("add; at should have preference over comparator", 1, function() {
  134. var Col = Backbone.Collection.extend({
  135. comparator: function(a,b) {
  136. return a.id > b.id ? -1 : 1;
  137. }
  138. });
  139. var col = new Col([{id: 2}, {id: 3}]);
  140. col.add(new Backbone.Model({id: 1}), {at: 1});
  141. equal(col.pluck('id').join(' '), '3 1 2');
  142. });
  143. test("can't add model to collection twice", function() {
  144. var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
  145. equal(col.pluck('id').join(' '), '1 2 3');
  146. });
  147. test("can't add different model with same id to collection twice", 1, function() {
  148. var col = new Backbone.Collection;
  149. col.unshift({id: 101});
  150. col.add({id: 101});
  151. equal(col.length, 1);
  152. });
  153. test("merge in duplicate models with {merge: true}", 3, function() {
  154. var col = new Backbone.Collection;
  155. col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]);
  156. col.add({id: 1, name: 'Moses'});
  157. equal(col.first().get('name'), 'Moe');
  158. col.add({id: 1, name: 'Moses'}, {merge: true});
  159. equal(col.first().get('name'), 'Moses');
  160. col.add({id: 1, name: 'Tim'}, {merge: true, silent: true});
  161. equal(col.first().get('name'), 'Tim');
  162. });
  163. test("add model to multiple collections", 10, function() {
  164. var counter = 0;
  165. var e = new Backbone.Model({id: 10, label : 'e'});
  166. e.on('add', function(model, collection) {
  167. counter++;
  168. equal(e, model);
  169. if (counter > 1) {
  170. equal(collection, colF);
  171. } else {
  172. equal(collection, colE);
  173. }
  174. });
  175. var colE = new Backbone.Collection([]);
  176. colE.on('add', function(model, collection) {
  177. equal(e, model);
  178. equal(colE, collection);
  179. });
  180. var colF = new Backbone.Collection([]);
  181. colF.on('add', function(model, collection) {
  182. equal(e, model);
  183. equal(colF, collection);
  184. });
  185. colE.add(e);
  186. equal(e.collection, colE);
  187. colF.add(e);
  188. equal(e.collection, colE);
  189. });
  190. test("add model with parse", 1, function() {
  191. var Model = Backbone.Model.extend({
  192. parse: function(obj) {
  193. obj.value += 1;
  194. return obj;
  195. }
  196. });
  197. var Col = Backbone.Collection.extend({model: Model});
  198. var col = new Col;
  199. col.add({value: 1}, {parse: true});
  200. equal(col.at(0).get('value'), 2);
  201. });
  202. test("add model to collection with sort()-style comparator", 3, function() {
  203. var col = new Backbone.Collection;
  204. col.comparator = function(a, b) {
  205. return a.get('name') < b.get('name') ? -1 : 1;
  206. };
  207. var tom = new Backbone.Model({name: 'Tom'});
  208. var rob = new Backbone.Model({name: 'Rob'});
  209. var tim = new Backbone.Model({name: 'Tim'});
  210. col.add(tom);
  211. col.add(rob);
  212. col.add(tim);
  213. equal(col.indexOf(rob), 0);
  214. equal(col.indexOf(tim), 1);
  215. equal(col.indexOf(tom), 2);
  216. });
  217. test("comparator that depends on `this`", 2, function() {
  218. var col = new Backbone.Collection;
  219. col.negative = function(num) {
  220. return -num;
  221. };
  222. col.comparator = function(a) {
  223. return this.negative(a.id);
  224. };
  225. col.add([{id: 1}, {id: 2}, {id: 3}]);
  226. deepEqual(col.pluck('id'), [3, 2, 1]);
  227. col.comparator = function(a, b) {
  228. return this.negative(b.id) - this.negative(a.id);
  229. };
  230. col.sort();
  231. deepEqual(col.pluck('id'), [1, 2, 3]);
  232. });
  233. test("remove", 5, function() {
  234. var removed = null;
  235. var otherRemoved = null;
  236. col.on('remove', function(model, col, options) {
  237. removed = model.get('label');
  238. equal(options.index, 3);
  239. });
  240. otherCol.on('remove', function(model, col, options) {
  241. otherRemoved = true;
  242. });
  243. col.remove(d);
  244. equal(removed, 'd');
  245. equal(col.length, 3);
  246. equal(col.first(), a);
  247. equal(otherRemoved, null);
  248. });
  249. test("shift and pop", 2, function() {
  250. var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  251. equal(col.shift().get('a'), 'a');
  252. equal(col.pop().get('c'), 'c');
  253. });
  254. test("slice", 2, function() {
  255. var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  256. var array = col.slice(1, 3);
  257. equal(array.length, 2);
  258. equal(array[0].get('b'), 'b');
  259. });
  260. test("events are unbound on remove", 3, function() {
  261. var counter = 0;
  262. var dj = new Backbone.Model();
  263. var emcees = new Backbone.Collection([dj]);
  264. emcees.on('change', function(){ counter++; });
  265. dj.set({name : 'Kool'});
  266. equal(counter, 1);
  267. emcees.reset([]);
  268. equal(dj.collection, undefined);
  269. dj.set({name : 'Shadow'});
  270. equal(counter, 1);
  271. });
  272. test("remove in multiple collections", 7, function() {
  273. var modelData = {
  274. id : 5,
  275. title : 'Othello'
  276. };
  277. var passed = false;
  278. var e = new Backbone.Model(modelData);
  279. var f = new Backbone.Model(modelData);
  280. f.on('remove', function() {
  281. passed = true;
  282. });
  283. var colE = new Backbone.Collection([e]);
  284. var colF = new Backbone.Collection([f]);
  285. ok(e != f);
  286. ok(colE.length === 1);
  287. ok(colF.length === 1);
  288. colE.remove(e);
  289. equal(passed, false);
  290. ok(colE.length === 0);
  291. colF.remove(e);
  292. ok(colF.length === 0);
  293. equal(passed, true);
  294. });
  295. test("remove same model in multiple collection", 16, function() {
  296. var counter = 0;
  297. var e = new Backbone.Model({id: 5, title: 'Othello'});
  298. e.on('remove', function(model, collection) {
  299. counter++;
  300. equal(e, model);
  301. if (counter > 1) {
  302. equal(collection, colE);
  303. } else {
  304. equal(collection, colF);
  305. }
  306. });
  307. var colE = new Backbone.Collection([e]);
  308. colE.on('remove', function(model, collection) {
  309. equal(e, model);
  310. equal(colE, collection);
  311. });
  312. var colF = new Backbone.Collection([e]);
  313. colF.on('remove', function(model, collection) {
  314. equal(e, model);
  315. equal(colF, collection);
  316. });
  317. equal(colE, e.collection);
  318. colF.remove(e);
  319. ok(colF.length === 0);
  320. ok(colE.length === 1);
  321. equal(counter, 1);
  322. equal(colE, e.collection);
  323. colE.remove(e);
  324. equal(null, e.collection);
  325. ok(colE.length === 0);
  326. equal(counter, 2);
  327. });
  328. test("model destroy removes from all collections", 3, function() {
  329. var e = new Backbone.Model({id: 5, title: 'Othello'});
  330. e.sync = function(method, model, options) {
  331. options.success(model, [], options);
  332. };
  333. var colE = new Backbone.Collection([e]);
  334. var colF = new Backbone.Collection([e]);
  335. e.destroy();
  336. ok(colE.length === 0);
  337. ok(colF.length === 0);
  338. equal(undefined, e.collection);
  339. });
  340. test("Colllection: non-persisted model destroy removes from all collections", 3, function() {
  341. var e = new Backbone.Model({title: 'Othello'});
  342. e.sync = function(method, model, options) { throw "should not be called"; };
  343. var colE = new Backbone.Collection([e]);
  344. var colF = new Backbone.Collection([e]);
  345. e.destroy();
  346. ok(colE.length === 0);
  347. ok(colF.length === 0);
  348. equal(undefined, e.collection);
  349. });
  350. test("fetch", 4, function() {
  351. var collection = new Backbone.Collection;
  352. collection.url = '/test';
  353. collection.fetch();
  354. equal(this.syncArgs.method, 'read');
  355. equal(this.syncArgs.model, collection);
  356. equal(this.syncArgs.options.parse, true);
  357. collection.fetch({parse: false});
  358. equal(this.syncArgs.options.parse, false);
  359. });
  360. test("ensure fetch only parses once", 1, function() {
  361. var collection = new Backbone.Collection;
  362. var counter = 0;
  363. collection.parse = function(models) {
  364. counter++;
  365. return models;
  366. };
  367. collection.url = '/test';
  368. collection.fetch();
  369. this.syncArgs.options.success([]);
  370. equal(counter, 1);
  371. });
  372. test("create", 4, function() {
  373. var collection = new Backbone.Collection;
  374. collection.url = '/test';
  375. var model = collection.create({label: 'f'}, {wait: true});
  376. equal(this.syncArgs.method, 'create');
  377. equal(this.syncArgs.model, model);
  378. equal(model.get('label'), 'f');
  379. equal(model.collection, collection);
  380. });
  381. test("create with validate:true enforces validation", 1, function() {
  382. var ValidatingModel = Backbone.Model.extend({
  383. validate: function(attrs) {
  384. return "fail";
  385. }
  386. });
  387. var ValidatingCollection = Backbone.Collection.extend({
  388. model: ValidatingModel
  389. });
  390. var col = new ValidatingCollection();
  391. equal(col.create({"foo":"bar"}, {validate:true}), false);
  392. });
  393. test("a failing create returns model with errors", function() {
  394. var ValidatingModel = Backbone.Model.extend({
  395. validate: function(attrs) {
  396. return "fail";
  397. }
  398. });
  399. var ValidatingCollection = Backbone.Collection.extend({
  400. model: ValidatingModel
  401. });
  402. var col = new ValidatingCollection();
  403. var m = col.create({"foo":"bar"});
  404. equal(m.validationError, 'fail');
  405. equal(col.length, 1);
  406. });
  407. test("initialize", 1, function() {
  408. var Collection = Backbone.Collection.extend({
  409. initialize: function() {
  410. this.one = 1;
  411. }
  412. });
  413. var coll = new Collection;
  414. equal(coll.one, 1);
  415. });
  416. test("toJSON", 1, function() {
  417. equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
  418. });
  419. test("where", 6, function() {
  420. var coll = new Backbone.Collection([
  421. {a: 1},
  422. {a: 1},
  423. {a: 1, b: 2},
  424. {a: 2, b: 2},
  425. {a: 3}
  426. ]);
  427. equal(coll.where({a: 1}).length, 3);
  428. equal(coll.where({a: 2}).length, 1);
  429. equal(coll.where({a: 3}).length, 1);
  430. equal(coll.where({b: 1}).length, 0);
  431. equal(coll.where({b: 2}).length, 2);
  432. equal(coll.where({a: 1, b: 2}).length, 1);
  433. });
  434. test("Underscore methods", 13, function() {
  435. equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
  436. equal(col.any(function(model){ return model.id === 100; }), false);
  437. equal(col.any(function(model){ return model.id === 0; }), true);
  438. equal(col.indexOf(b), 1);
  439. equal(col.size(), 4);
  440. equal(col.rest().length, 3);
  441. ok(!_.include(col.rest()), a);
  442. ok(!_.include(col.rest()), d);
  443. ok(!col.isEmpty());
  444. ok(!_.include(col.without(d)), d);
  445. equal(col.max(function(model){ return model.id; }).id, 3);
  446. equal(col.min(function(model){ return model.id; }).id, 0);
  447. deepEqual(col.chain()
  448. .filter(function(o){ return o.id % 2 === 0; })
  449. .map(function(o){ return o.id * 2; })
  450. .value(),
  451. [4, 0]);
  452. });
  453. test("sortedIndex", function () {
  454. var model = new Backbone.Model({key: 2});
  455. var collection = new (Backbone.Collection.extend({
  456. comparator: 'key'
  457. }))([model, {key: 1}]);
  458. equal(collection.sortedIndex(model), 1);
  459. equal(collection.sortedIndex(model, 'key'), 1);
  460. equal(collection.sortedIndex(model, function (model) {
  461. return model.get('key');
  462. }), 1);
  463. });
  464. test("reset", 10, function() {
  465. var resetCount = 0;
  466. var models = col.models.slice();
  467. col.on('reset', function() { resetCount += 1; });
  468. col.reset([]);
  469. equal(resetCount, 1);
  470. equal(col.length, 0);
  471. equal(col.last(), null);
  472. col.reset(models);
  473. equal(resetCount, 2);
  474. equal(col.length, 4);
  475. equal(col.last(), d);
  476. col.reset(_.map(models, function(m){ return m.attributes; }));
  477. equal(resetCount, 3);
  478. equal(col.length, 4);
  479. ok(col.last() !== d);
  480. ok(_.isEqual(col.last().attributes, d.attributes));
  481. });
  482. test("reset passes caller options", 3, function() {
  483. var Model = Backbone.Model.extend({
  484. initialize: function(attrs, options) {
  485. this.model_parameter = options.model_parameter;
  486. }
  487. });
  488. var col = new (Backbone.Collection.extend({ model: Model }))();
  489. col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 }], { model_parameter: 'model parameter' });
  490. equal(col.length, 2);
  491. col.each(function(model) {
  492. equal(model.model_parameter, 'model parameter');
  493. });
  494. });
  495. test("trigger custom events on models", 1, function() {
  496. var fired = null;
  497. a.on("custom", function() { fired = true; });
  498. a.trigger("custom");
  499. equal(fired, true);
  500. });
  501. test("add does not alter arguments", 2, function(){
  502. var attrs = {};
  503. var models = [attrs];
  504. new Backbone.Collection().add(models);
  505. equal(models.length, 1);
  506. ok(attrs === models[0]);
  507. });
  508. test("#714: access `model.collection` in a brand new model.", 2, function() {
  509. var collection = new Backbone.Collection;
  510. collection.url = '/test';
  511. var Model = Backbone.Model.extend({
  512. set: function(attrs) {
  513. equal(attrs.prop, 'value');
  514. equal(this.collection, collection);
  515. return this;
  516. }
  517. });
  518. collection.model = Model;
  519. collection.create({prop: 'value'});
  520. });
  521. test("#574, remove its own reference to the .models array.", 2, function() {
  522. var col = new Backbone.Collection([
  523. {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
  524. ]);
  525. equal(col.length, 6);
  526. col.remove(col.models);
  527. equal(col.length, 0);
  528. });
  529. test("#861, adding models to a collection which do not pass validation, with validate:true", function() {
  530. var Model = Backbone.Model.extend({
  531. validate: function(attrs) {
  532. if (attrs.id == 3) return "id can't be 3";
  533. }
  534. });
  535. var Collection = Backbone.Collection.extend({
  536. model: Model
  537. });
  538. var collection = new Collection;
  539. collection.on("error", function() { ok(true); });
  540. collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {validate:true});
  541. deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
  542. });
  543. test("Invalid models are discarded with validate:true.", 5, function() {
  544. var collection = new Backbone.Collection;
  545. collection.on('test', function() { ok(true); });
  546. collection.model = Backbone.Model.extend({
  547. validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
  548. });
  549. var model = new collection.model({id: 1, valid: true});
  550. collection.add([model, {id: 2}], {validate:true});
  551. model.trigger('test');
  552. ok(collection.get(model.cid));
  553. ok(collection.get(1));
  554. ok(!collection.get(2));
  555. equal(collection.length, 1);
  556. });
  557. test("multiple copies of the same model", 3, function() {
  558. var col = new Backbone.Collection();
  559. var model = new Backbone.Model();
  560. col.add([model, model]);
  561. equal(col.length, 1);
  562. col.add([{id: 1}, {id: 1}]);
  563. equal(col.length, 2);
  564. equal(col.last().id, 1);
  565. });
  566. test("#964 - collection.get return inconsistent", 2, function() {
  567. var c = new Backbone.Collection();
  568. ok(c.get(null) === undefined);
  569. ok(c.get() === undefined);
  570. });
  571. test("#1112 - passing options.model sets collection.model", 2, function() {
  572. var Model = Backbone.Model.extend({});
  573. var c = new Backbone.Collection([{id: 1}], {model: Model});
  574. ok(c.model === Model);
  575. ok(c.at(0) instanceof Model);
  576. });
  577. test("null and undefined are invalid ids.", 2, function() {
  578. var model = new Backbone.Model({id: 1});
  579. var collection = new Backbone.Collection([model]);
  580. model.set({id: null});
  581. ok(!collection.get('null'));
  582. model.set({id: 1});
  583. model.set({id: undefined});
  584. ok(!collection.get('undefined'));
  585. });
  586. test("falsy comparator", 4, function(){
  587. var Col = Backbone.Collection.extend({
  588. comparator: function(model){ return model.id; }
  589. });
  590. var col = new Col();
  591. var colFalse = new Col(null, {comparator: false});
  592. var colNull = new Col(null, {comparator: null});
  593. var colUndefined = new Col(null, {comparator: undefined});
  594. ok(col.comparator);
  595. ok(!colFalse.comparator);
  596. ok(!colNull.comparator);
  597. ok(colUndefined.comparator);
  598. });
  599. test("#1355 - `options` is passed to success callbacks", 2, function(){
  600. var m = new Backbone.Model({x:1});
  601. var col = new Backbone.Collection();
  602. var opts = {
  603. success: function(collection, resp, options){
  604. ok(options);
  605. }
  606. };
  607. col.sync = m.sync = function( method, collection, options ){
  608. options.success(collection, [], options);
  609. };
  610. col.fetch(opts);
  611. col.create(m, opts);
  612. });
  613. test("#1412 - Trigger 'request' and 'sync' events.", 4, function() {
  614. var collection = new Backbone.Collection;
  615. collection.url = '/test';
  616. Backbone.ajax = function(settings){ settings.success(); };
  617. collection.on('request', function(obj, xhr, options) {
  618. ok(obj === collection, "collection has correct 'request' event after fetching");
  619. });
  620. collection.on('sync', function(obj, response, options) {
  621. ok(obj === collection, "collection has correct 'sync' event after fetching");
  622. });
  623. collection.fetch();
  624. collection.off();
  625. collection.on('request', function(obj, xhr, options) {
  626. ok(obj === collection.get(1), "collection has correct 'request' event after one of its models save");
  627. });
  628. collection.on('sync', function(obj, response, options) {
  629. ok(obj === collection.get(1), "collection has correct 'sync' event after one of its models save");
  630. });
  631. collection.create({id: 1});
  632. collection.off();
  633. });
  634. test("#1447 - create with wait adds model.", 1, function() {
  635. var collection = new Backbone.Collection;
  636. var model = new Backbone.Model;
  637. model.sync = function(method, model, options){
  638. options.success(model, [], options);
  639. };
  640. collection.on('add', function(){ ok(true); });
  641. collection.create(model, {wait: true});
  642. });
  643. test("#1448 - add sorts collection after merge.", 1, function() {
  644. var collection = new Backbone.Collection([
  645. {id: 1, x: 1},
  646. {id: 2, x: 2}
  647. ]);
  648. collection.comparator = function(model){ return model.get('x'); };
  649. collection.add({id: 1, x: 3}, {merge: true});
  650. deepEqual(collection.pluck('id'), [2, 1]);
  651. });
  652. test("#1655 - groupBy can be used with a string argument.", 3, function() {
  653. var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
  654. var grouped = collection.groupBy('x');
  655. strictEqual(_.keys(grouped).length, 2);
  656. strictEqual(grouped[1][0].get('x'), 1);
  657. strictEqual(grouped[2][0].get('x'), 2);
  658. });
  659. test("#1655 - sortBy can be used with a string argument.", 1, function() {
  660. var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
  661. var values = _.map(collection.sortBy('x'), function(model) {
  662. return model.get('x');
  663. });
  664. deepEqual(values, [1, 2, 3]);
  665. });
  666. test("#1604 - Removal during iteration.", 0, function() {
  667. var collection = new Backbone.Collection([{}, {}]);
  668. collection.on('add', function() {
  669. collection.at(0).destroy();
  670. });
  671. collection.add({}, {at: 0});
  672. });
  673. test("#1638 - `sort` during `add` triggers correctly.", function() {
  674. var collection = new Backbone.Collection;
  675. collection.comparator = function(model) { return model.get('x'); };
  676. var added = [];
  677. collection.on('add', function(model) {
  678. model.set({x: 3});
  679. collection.sort();
  680. added.push(model.id);
  681. });
  682. collection.add([{id: 1, x: 1}, {id: 2, x: 2}]);
  683. deepEqual(added, [1, 2]);
  684. });
  685. test("fetch parses models by default", 1, function() {
  686. var model = {};
  687. var Collection = Backbone.Collection.extend({
  688. url: 'test',
  689. model: Backbone.Model.extend({
  690. parse: function(resp) {
  691. strictEqual(resp, model);
  692. }
  693. })
  694. });
  695. new Collection().fetch();
  696. this.ajaxSettings.success([model]);
  697. });
  698. test("`sort` shouldn't always fire on `add`", 1, function() {
  699. var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
  700. comparator: 'id'
  701. });
  702. c.sort = function(){ ok(true); };
  703. c.add([]);
  704. c.add({id: 1});
  705. c.add([{id: 2}, {id: 3}]);
  706. c.add({id: 4});
  707. });
  708. test("#1407 parse option on constructor parses collection and models", 2, function() {
  709. var model = {
  710. namespace : [{id: 1}, {id:2}]
  711. };
  712. var Collection = Backbone.Collection.extend({
  713. model: Backbone.Model.extend({
  714. parse: function(model) {
  715. model.name = 'test';
  716. return model;
  717. }
  718. }),
  719. parse: function(model) {
  720. return model.namespace;
  721. }
  722. });
  723. var c = new Collection(model, {parse:true});
  724. equal(c.length, 2);
  725. equal(c.at(0).get('name'), 'test');
  726. });
  727. test("#1407 parse option on reset parses collection and models", 2, function() {
  728. var model = {
  729. namespace : [{id: 1}, {id:2}]
  730. };
  731. var Collection = Backbone.Collection.extend({
  732. model: Backbone.Model.extend({
  733. parse: function(model) {
  734. model.name = 'test';
  735. return model;
  736. }
  737. }),
  738. parse: function(model) {
  739. return model.namespace;
  740. }
  741. });
  742. var c = new Collection();
  743. c.reset(model, {parse:true});
  744. equal(c.length, 2);
  745. equal(c.at(0).get('name'), 'test');
  746. });
  747. test("Reset includes previous models in triggered event.", 1, function() {
  748. var model = new Backbone.Model();
  749. var collection = new Backbone.Collection([model])
  750. .on('reset', function(collection, options) {
  751. deepEqual(options.previousModels, [model]);
  752. });
  753. collection.reset([]);
  754. });
  755. test("update", function() {
  756. var m1 = new Backbone.Model();
  757. var m2 = new Backbone.Model({id: 2});
  758. var m3 = new Backbone.Model();
  759. var c = new Backbone.Collection([m1, m2]);
  760. // Test add/change/remove events
  761. c.on('add', function(model) {
  762. strictEqual(model, m3);
  763. });
  764. c.on('change', function(model) {
  765. strictEqual(model, m2);
  766. });
  767. c.on('remove', function(model) {
  768. strictEqual(model, m1);
  769. });
  770. // remove: false doesn't remove any models
  771. c.update([], {remove: false});
  772. strictEqual(c.length, 2);
  773. // add: false doesn't add any models
  774. c.update([m1, m2, m3], {add: false});
  775. strictEqual(c.length, 2);
  776. // merge: false doesn't change any models
  777. c.update([m1, {id: 2, a: 1}], {merge: false});
  778. strictEqual(m2.get('a'), void 0);
  779. // add: false, remove: false only merges existing models
  780. c.update([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false});
  781. strictEqual(c.length, 2);
  782. strictEqual(m2.get('a'), 0);
  783. // default options add/remove/merge as appropriate
  784. c.update([{id: 2, a: 1}, m3]);
  785. strictEqual(c.length, 2);
  786. strictEqual(m2.get('a'), 1);
  787. // Test removing models not passing an argument
  788. c.off('remove').on('remove', function(model) {
  789. ok(model === m2 || model === m3);
  790. });
  791. c.update([]);
  792. strictEqual(c.length, 0);
  793. });
  794. test("update with only cids", 3, function() {
  795. var m1 = new Backbone.Model;
  796. var m2 = new Backbone.Model;
  797. var c = new Backbone.Collection;
  798. c.update([m1, m2]);
  799. equal(c.length, 2);
  800. c.update([m1]);
  801. equal(c.length, 1);
  802. c.update([m1, m1, m1, m2, m2], {remove: false});
  803. equal(c.length, 2);
  804. });
  805. test("update with only idAttribute", 3, function() {
  806. var m1 = { _id: 1 };
  807. var m2 = { _id: 2 };
  808. var col = Backbone.Collection.extend({
  809. model: Backbone.Model.extend({
  810. idAttribute: '_id'
  811. })
  812. });
  813. var c = new col;
  814. c.update([m1, m2]);
  815. equal(c.length, 2);
  816. c.update([m1]);
  817. equal(c.length, 1);
  818. c.update([m1, m1, m1, m2, m2], {remove: false});
  819. equal(c.length, 2);
  820. });
  821. test("update + merge with default values defined", function() {
  822. var Model = Backbone.Model.extend({
  823. defaults: {
  824. key: 'value'
  825. }
  826. });
  827. var m = new Model({id: 1});
  828. var col = new Backbone.Collection([m], {model: Model});
  829. equal(col.first().get('key'), 'value');
  830. col.update({id: 1, key: 'other'});
  831. equal(col.first().get('key'), 'other');
  832. col.update({id: 1, other: 'value'});
  833. equal(col.first().get('key'), 'other');
  834. equal(col.length, 1);
  835. });
  836. test("#1894 - Push should not trigger a sort", 0, function() {
  837. var Collection = Backbone.Collection.extend({
  838. comparator: 'id',
  839. sort: function() {
  840. ok(false);
  841. }
  842. });
  843. new Collection().push({id: 1});
  844. });
  845. test("`update` with non-normal id", function() {
  846. var Collection = Backbone.Collection.extend({
  847. model: Backbone.Model.extend({idAttribute: '_id'})
  848. });
  849. var collection = new Collection({_id: 1});
  850. collection.update([{_id: 1, a: 1}], {add: false});
  851. equal(collection.first().get('a'), 1);
  852. });
  853. test("#1894 - `sort` can optionally be turned off", 0, function() {
  854. var Collection = Backbone.Collection.extend({
  855. comparator: 'id',
  856. sort: function() { ok(true); }
  857. });
  858. new Collection().add({id: 1}, {sort: false});
  859. });
  860. test("#1915 - `parse` data in the right order in `update`", function() {
  861. var collection = new (Backbone.Collection.extend({
  862. parse: function (data) {
  863. strictEqual(data.status, 'ok');
  864. return data.data;
  865. }
  866. }));
  867. var res = {status: 'ok', data:[{id: 1}]};
  868. collection.update(res, {parse: true});
  869. });
  870. asyncTest("#1939 - `parse` is passed `options`", 1, function () {
  871. var collection = new (Backbone.Collection.extend({
  872. url: '/',
  873. parse: function (data, options) {
  874. strictEqual(options.xhr.someHeader, 'headerValue');
  875. return data;
  876. }
  877. }));
  878. var ajax = Backbone.ajax;
  879. Backbone.ajax = function (params) {
  880. _.defer(params.success);
  881. return {someHeader: 'headerValue'};
  882. };
  883. collection.fetch({
  884. success: function () { start(); }
  885. });
  886. Backbone.ajax = ajax;
  887. });
  888. test("`add` only `sort`s when necessary", 2, function () {
  889. var collection = new (Backbone.Collection.extend({
  890. comparator: 'a'
  891. }))([{id: 1}, {id: 2}, {id: 3}]);
  892. collection.on('sort', function () { ok(true); });
  893. collection.add({id: 4}); // do sort, new model
  894. collection.add({id: 1, a: 1}, {merge: true}); // do sort, comparator change
  895. collection.add({id: 1, b: 1}, {merge: true}); // don't sort, no comparator change
  896. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no comparator change
  897. collection.add(collection.models); // don't sort, nothing new
  898. collection.add(collection.models, {merge: true}); // don't sort
  899. });
  900. test("`add` only `sort`s when necessary with comparator function", 3, function () {
  901. var collection = new (Backbone.Collection.extend({
  902. comparator: function(a, b) {
  903. a.get('a') > b.get('a') ? 1 : (a.get('a') < b.get('a') ? -1 : 0);
  904. }
  905. }))([{id: 1}, {id: 2}, {id: 3}]);
  906. collection.on('sort', function () { ok(true); });
  907. collection.add({id: 4}); // do sort, new model
  908. collection.add({id: 1, a: 1}, {merge: true}); // do sort, model change
  909. collection.add({id: 1, b: 1}, {merge: true}); // do sort, model change
  910. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no model change
  911. collection.add(collection.models); // don't sort, nothing new
  912. collection.add(collection.models, {merge: true}); // don't sort
  913. });
  914. });