PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/TheMailer/Public/components/backbone/test/collection.js

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