PageRenderTime 66ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/views.js

https://github.com/d1b1/backbone.layoutmanager
JavaScript | 1169 lines | 848 code | 286 blank | 35 comment | 3 complexity | f0d1a1bccd3643be6de15a7328be8bd1 MD5 | raw file
  1. function isNode(obj) {
  2. if (obj && obj.nodeType != null) {
  3. return true;
  4. }
  5. }
  6. module("views", {
  7. setup: function() {
  8. var setup = this;
  9. // Custom View
  10. this.View = Backbone.LayoutView.extend({
  11. template: "#test",
  12. serialize: function() {
  13. return { text: this.msg };
  14. },
  15. initialize: function(opts) {
  16. this.msg = opts.msg;
  17. }
  18. });
  19. // Initialize View
  20. this.InitView = Backbone.LayoutView.extend({
  21. template: "#test",
  22. serialize: function() {
  23. return { text: this.msg };
  24. },
  25. initialize: function(opts) {
  26. this.msg = opts.msg;
  27. this.setViews({
  28. ".inner-right": new setup.SubView()
  29. });
  30. }
  31. });
  32. this.SubView = Backbone.LayoutView.extend({
  33. template: "#test-sub",
  34. serialize: function() {
  35. return { text: "Right" };
  36. }
  37. });
  38. this.EventedListView = Backbone.LayoutView.extend({
  39. template: "#list",
  40. initialize: function() {
  41. this.collection.on("reset", this.render, this);
  42. },
  43. cleanup: function() {
  44. this.collection.off(null, null, this);
  45. },
  46. beforeRender: function() {
  47. this.collection.each(function(model) {
  48. this.insertView("ul", new setup.ItemView({ model: model.toJSON() }));
  49. }, this);
  50. }
  51. });
  52. this.ListView = Backbone.LayoutView.extend({
  53. template: "#list",
  54. beforeRender: function() {
  55. // Iterate over the passed collection and insert into the view
  56. _.each(this.collection, function(model) {
  57. this.insertView("ul", new setup.ItemView({ model: model }));
  58. }, this);
  59. }
  60. });
  61. this.ItemView = Backbone.LayoutView.extend({
  62. template: "#test-sub",
  63. tagName: "li",
  64. serialize: function() {
  65. return this.model;
  66. }
  67. });
  68. }
  69. });
  70. asyncTest("render outside defined partial", 2, function() {
  71. var main = new Backbone.Layout({
  72. template: "#main"
  73. });
  74. var a = main.setView(".right", new this.View({ msg: "Right" }));
  75. main.render().then(function() {
  76. var trimmed = $.trim( $(this.el).find(".inner-left").html() );
  77. ok(isNode(this.el), "Contents is a DOM Node");
  78. equal(trimmed, "Right", "Correct render");
  79. start();
  80. });
  81. });
  82. asyncTest("render inside defined partial", function() {
  83. expect(2);
  84. var main = new Backbone.Layout({
  85. template: "#main",
  86. views: {
  87. ".right": new this.View({ msg: "Right" })
  88. }
  89. });
  90. main.render(function(el) {
  91. var trimmed = $.trim( $(el).find(".inner-left").html() );
  92. ok(isNode(el), "Contents is a DOM Node");
  93. equal(trimmed, "Right", "Correct render");
  94. start();
  95. });
  96. });
  97. asyncTest("re-render a view defined after initialization", function(){
  98. expect(2);
  99. var trimmed;
  100. var setup = this;
  101. var main = new Backbone.Layout({
  102. template: "#main"
  103. });
  104. main.setView(".right", new this.View({ msg: "Right" }));
  105. main.render(function(el) {
  106. $('#container').html(el);
  107. trimmed = $.trim( $("#container .inner-left").html() );
  108. equal(trimmed, "Right", "Correct re-render");
  109. main.setView(".right", new setup.View({
  110. msg: "Right Again"
  111. })).render().then(function() {
  112. trimmed = $.trim( $("#container .inner-left").html() );
  113. equal(trimmed, "Right Again", "Correct re-render");
  114. start();
  115. });
  116. });
  117. });
  118. asyncTest("nested views", function() {
  119. expect(2);
  120. var main = new Backbone.Layout({
  121. template: "#main",
  122. views: {
  123. ".right": new this.View({
  124. msg: "Left",
  125. views: {
  126. ".inner-right": new this.SubView({ lol: "hi" })
  127. }
  128. })
  129. }
  130. });
  131. main.render().then(function() {
  132. var view = this;
  133. var trimmed = $.trim(this.$(".inner-right div").html());
  134. ok(isNode(this.el), "Contents is a DOM Node");
  135. equal(trimmed, "Right", "Correct render");
  136. start();
  137. });
  138. });
  139. asyncTest("serialize on Layout is a function", function() {
  140. expect(1);
  141. var testText = "test text";
  142. var main = new Backbone.Layout({
  143. template: "#test-sub",
  144. serialize: { text: "test text" }
  145. });
  146. main.render(function(el) {
  147. equal($.trim( $(el).text() ), testText, "correct serialize");
  148. start();
  149. });
  150. });
  151. asyncTest("serialize on Layout is an object", function() {
  152. expect(1);
  153. var testText = "test text";
  154. var main = new Backbone.Layout({
  155. template: "#test-sub",
  156. serialize: { text: "test text" }
  157. });
  158. main.render(function(el) {
  159. equal($.trim( $(el).text() ), testText, "correct serialize");
  160. start();
  161. });
  162. });
  163. // TODO THIS TEST
  164. asyncTest("rendered event", function() {
  165. expect(4);
  166. var main = new Backbone.Layout({
  167. template: "#main",
  168. views: {
  169. ".right": new this.ListView({
  170. collection: [{ text: "one" }, { text: "two" }]
  171. })
  172. }
  173. });
  174. main.render(function(el) {
  175. ok(isNode(el), "Contents is a DOM Node");
  176. equal($(el).find("ul li").length, 2, "Correct number of nested li's");
  177. equal($.trim( $(el).find("ul li:eq(0)").html() ), "one",
  178. "Correct first li content");
  179. equal($.trim( $(el).find("ul li:eq(1)").html() ), "two",
  180. "Correct second li content");
  181. start();
  182. });
  183. });
  184. asyncTest("insert views", function() {
  185. expect(4);
  186. var main = new Backbone.Layout({
  187. template: "#main",
  188. views: {
  189. ".right": new this.ListView({
  190. collection: [{ text: "one" }, { text: "two" }]
  191. })
  192. }
  193. });
  194. main.render(function(el) {
  195. ok(isNode(el), "Contents is a DOM Node");
  196. equal($(el).find("ul li").length, 2, "Correct number of nested li's");
  197. equal($.trim( $(el).find("ul li:eq(0)").html() ), "one",
  198. "Correct first li content");
  199. equal($.trim( $(el).find("ul li:eq(1)").html() ), "two",
  200. "Correct second li content");
  201. start();
  202. });
  203. });
  204. asyncTest("using setViews", function() {
  205. expect(2);
  206. var main = new Backbone.Layout({
  207. template: "#main"
  208. });
  209. main.setViews({
  210. ".right": new this.View({
  211. msg: "Left",
  212. views: {
  213. ".inner-right": new this.SubView()
  214. }
  215. })
  216. });
  217. main.render().then(function() {
  218. var trimmed = $.trim(this.$(".inner-right div").html());
  219. ok(isNode(this.el), "Contents is a DOM Node");
  220. equal(trimmed, "Right", "Correct render");
  221. start();
  222. });
  223. });
  224. asyncTest("using setViews inside initialize", function() {
  225. expect(2);
  226. var main = new Backbone.Layout({
  227. template: "#main"
  228. });
  229. main.setViews({
  230. ".right": new this.InitView({
  231. msg: "Left"
  232. })
  233. });
  234. main.render(function(el) {
  235. var trimmed = $.trim( $(el).find(".inner-right div").html() );
  236. ok(isNode(el), "Contents is a DOM Node");
  237. equal(trimmed, "Right", "Correct render");
  238. start();
  239. });
  240. });
  241. asyncTest("extend layoutmanager", 1, function() {
  242. var testText = "test text";
  243. var BaseLayout = Backbone.Layout.extend({
  244. template: "#test-sub",
  245. serialize: { text: "test text" }
  246. });
  247. var main = new BaseLayout();
  248. main.render(function(el) {
  249. equal($.trim( $(el).text() ), testText, "correct serialize");
  250. start();
  251. });
  252. });
  253. asyncTest("appending views with array literal", 3, function() {
  254. var main = new Backbone.Layout({
  255. template: "#main"
  256. });
  257. main.setViews({
  258. ".right": [
  259. new this.View({
  260. msg: "One",
  261. keep: true
  262. }),
  263. new this.View({
  264. msg: "Two",
  265. keep: true
  266. })
  267. ]
  268. });
  269. main.render(function(el) {
  270. main.render().then(function(el) {
  271. equal($(el).find(".right").children().length, 2, "correct children length");
  272. equal($.trim( $(el).find(".right").children().eq(0).text() ), "One",
  273. "correct value set for the first child");
  274. equal($.trim( $(el).find(".right").children().eq(1).text() ), "Two",
  275. "correct value set for the second child");
  276. start();
  277. });
  278. });
  279. });
  280. asyncTest("use layout without a template property", function() {
  281. expect(1);
  282. var main = new Backbone.Layout({
  283. el: "#prefilled"
  284. });
  285. main.setViews({
  286. ".test": new this.SubView()
  287. });
  288. main.render(function(el) {
  289. equal($.trim( $(el).find(".test").text() ), "Right",
  290. "Able to use an existing DOM element");
  291. start();
  292. });
  293. });
  294. asyncTest("single render per view", function() {
  295. expect(1);
  296. var count = 0;
  297. var main = new Backbone.Layout({
  298. template: "#main"
  299. });
  300. var right = main.setView(".right", new this.View({
  301. msg: "1"
  302. }));
  303. // Level 1
  304. right.render(function() {
  305. count++;
  306. });
  307. // Level 2
  308. right.setView(".inner-right", new this.View({ msg: "2" })).render(function() {
  309. count++;
  310. });
  311. // Level 3
  312. var innerRight = right.views[".inner-right"];
  313. innerRight.setViews({
  314. ".inner-right": [ new this.SubView(), new this.SubView() ]
  315. });
  316. innerRight.views[".inner-right"][0].render(function() {
  317. count++;
  318. });
  319. innerRight.views[".inner-right"][1].render(function() {
  320. count++;
  321. });
  322. main.render(function(el) {
  323. equal(count, 4, "Render is only called once for each view");
  324. start();
  325. });
  326. });
  327. asyncTest("render callback and deferred context is view", function() {
  328. expect(6);
  329. var main = new Backbone.Layout({
  330. template: "#main",
  331. views: {
  332. ".right": new this.View({ msg: "Right" }),
  333. ".left": [
  334. new this.View({ keep: true, msg: "Left 1" }),
  335. new this.View({
  336. msg: "Left 2",
  337. keep: true,
  338. views: {
  339. ".inner-left": new this.SubView({ lol: "hi" })
  340. }
  341. })
  342. ]
  343. }
  344. });
  345. main.render(function(el) {
  346. equal(this, main, "Layout render callback context is Layout");
  347. start();
  348. }).then(function(el) {
  349. equal(this, main, "Layout render deferred context is Layout");
  350. start();
  351. });
  352. main.views[".right"].render(function(el) {
  353. equal(this, main.views[".right"], "View render callback context is View");
  354. start();
  355. }).then(function(el) {
  356. equal(this, main.views[".right"], "View render deferred context is View");
  357. start();
  358. });
  359. main.views[".left"][1].views[".inner-left"].render(function(el) {
  360. equal(this, main.views[".left"][1].views[".inner-left"],
  361. "Nested View render callback context is View");
  362. start();
  363. }).then(function() {
  364. equal(this, main.views[".left"][1].views[".inner-left"],
  365. "Nested View render deferred context is View");
  366. start();
  367. });
  368. });
  369. asyncTest("list items don't duplicate", 2, function() {
  370. var element;
  371. var main = new Backbone.Layout({
  372. template: "#main"
  373. });
  374. var view = main.setView(".right", new this.EventedListView({
  375. collection: new Backbone.Collection()
  376. }));
  377. view.collection.reset([ { text: 5 } ]);
  378. main.render().then(function() {
  379. view.collection.reset([ { text: 5 } ]);
  380. });
  381. view.collection.reset([ { text: 5 } ]);
  382. window.setTimeout(function() {
  383. view.collection.reset([
  384. { text: 1 },
  385. { text: 2 },
  386. { text: 3 },
  387. { text: 4 }
  388. ]);
  389. view.render().then(function() {
  390. equal(view.$("ul").children().length, 4, "Only four elements");
  391. equal(view.views.ul.length, 4, "Only four Views");
  392. start();
  393. });
  394. }, 5);
  395. });
  396. test("afterRender triggers for subViews", 1, function() {
  397. var triggered = false;
  398. var main = new Backbone.Layout({
  399. el: "#prefilled"
  400. });
  401. main.setViews({
  402. ".test": new this.SubView({
  403. serialize: { text: "Here" },
  404. afterRender: function() {
  405. triggered = true;
  406. }
  407. })
  408. });
  409. main.render().then(function() {
  410. ok(triggered === true, "afterRender is called");
  411. start();
  412. });
  413. });
  414. // Do this one without a custom render function as well.
  415. test("view render can be attached inside initalize", 1, function() {
  416. var main = new Backbone.Layout({
  417. template: "#main"
  418. });
  419. var TestRender = Backbone.View.extend({
  420. manage: true,
  421. initialize: function() {
  422. this.model.on("change", this.render, this);
  423. },
  424. beforeRender: function() {
  425. this.$el.html("This works now!");
  426. }
  427. });
  428. var testModel = new Backbone.Model();
  429. main.setView(".right", new TestRender({
  430. model: testModel
  431. }));
  432. testModel.trigger("change");
  433. main.render().then(function(el) {
  434. equal(this.$(".right").children().html(), "This works now!",
  435. "Content correctly set");
  436. start();
  437. });
  438. });
  439. test("Allow normal Views to co-exist with LM", 1, function() {
  440. var called = false;
  441. var View = Backbone.View.extend({
  442. render: function() {
  443. called = true;
  444. }
  445. });
  446. var view = new View();
  447. view.render();
  448. ok(called, "Render methods work without being in LM");
  449. });
  450. test("setView works going from append mode to normal", 1, function() {
  451. var main = new Backbone.Layout({
  452. template: "#main",
  453. views: {
  454. ".left": [
  455. new this.View({ keep: true, msg: "Left 1" }),
  456. new this.View({
  457. msg: "Left 2",
  458. keep: true,
  459. views: {
  460. ".inner-left": new this.SubView({ lol: "hi" })
  461. }
  462. })
  463. ]
  464. }
  465. });
  466. main.setView(".left", new this.View({ msg: "Right" }));
  467. ok(true, "setView does not crash");
  468. });
  469. test("setView and insertView not working after model change", function() {
  470. var setup = this;
  471. var m = new Backbone.Model();
  472. var View = Backbone.View.extend({
  473. manage: true,
  474. initialize: function() {
  475. this.model.on("change", function() {
  476. this.render();
  477. }, this);
  478. },
  479. beforeRender: function() {
  480. this.insertView(new setup.View({
  481. msg: "insert",
  482. // Need keep true.
  483. keep: true
  484. }));
  485. }
  486. });
  487. var view = new View({ model: m });
  488. var layout = new Backbone.Layout({
  489. template: "#main",
  490. views: {
  491. ".left": view
  492. }
  493. });
  494. m.set("some", "change");
  495. layout.render().then(function(el) {
  496. equal(this.$(".inner-left").length, 2, "rendered twice");
  497. });
  498. });
  499. asyncTest("Ensure afterRender can access element's parent.", 1, function() {
  500. var view = new Backbone.LayoutView({
  501. template: "#main",
  502. views: {
  503. ".left": new Backbone.LayoutView({
  504. afterRender: function() {
  505. var subView = this;
  506. ok($.contains(view.el, subView.el),
  507. "Parent can be found in afterRender");
  508. start();
  509. }
  510. })
  511. }
  512. });
  513. view.render();
  514. });
  515. // https://github.com/tbranyen/backbone.layoutmanager/issues/108
  516. test("render callback vs deferred resolve when called twice", 1, function() {
  517. // Create a new View.
  518. var view = new Backbone.View();
  519. // Set it up to work with LayoutManager.
  520. Backbone.LayoutManager.setupView(view);
  521. // Two renders using callback style.
  522. view.render(function() {
  523. view.render(function() {
  524. ok(true, "Two render's using callback style work.");
  525. });
  526. });
  527. });
  528. // https://github.com/tbranyen/backbone.layoutmanager/issues/115
  529. test("Uncaught RangeError: Maximum call stack size exceeded", 1, function() {
  530. var View = Backbone.View.extend({
  531. manage: true
  532. });
  533. new View({
  534. model: new Backbone.Model()
  535. }).render();
  536. ok(true, "No call stack exceeded error");
  537. });
  538. // https://github.com/tbranyen/backbone.layoutmanager/issues/117
  539. asyncTest("Views getting appended in the wrong order", 3, function() {
  540. var View = Backbone.View.extend({
  541. manage: true,
  542. template: "testing",
  543. fetch: function(name) {
  544. var done = this.async();
  545. setTimeout(function() {
  546. done( _.template(name));
  547. }, Math.random()*100);
  548. }
  549. });
  550. var view = new View({
  551. views: {
  552. "": [ new View({ order: 1 }), new View({ order: 2 }) ]
  553. }
  554. });
  555. view.render().then(function() {
  556. equal(this.views[""].length, 2, "There should be two views");
  557. equal(this.views[""][0].options.order, 1, "The first order should be 1");
  558. equal(this.views[""][1].options.order, 2, "The second order should be 2");
  559. start();
  560. });
  561. });
  562. // https://github.com/tbranyen/backbone.layoutmanager/issues/116
  563. test("Re-rendering of inserted views causes append at the end of the list", 1, function() {
  564. var ItemView = Backbone.LayoutView.extend({
  565. tagName: "tr",
  566. template: "<%= msg %>",
  567. fetch: function(name) {
  568. return _.template(name);
  569. },
  570. serialize: function() {
  571. return { msg: this.options.msg };
  572. }
  573. });
  574. var item1 = new ItemView({ msg: "hello" });
  575. var item2 = new ItemView({ msg: "goodbye" });
  576. var list = new Backbone.LayoutView({
  577. template: "<tbody></tbody>",
  578. fetch: function(name) {
  579. return _.template(name);
  580. },
  581. beforeRender: function() {
  582. this.insertView("tbody", item1);
  583. this.insertView("tbody", item2);
  584. }
  585. });
  586. var main = new Backbone.Layout({
  587. tagName: "table"
  588. });
  589. main.insertView(list);
  590. main.render().then(function() {
  591. var parent = this;
  592. list.views.tbody[0].render().then(function() {
  593. equal(parent.$("tbody:first tr").html(), "hello", "Correct tbody order.");
  594. });
  595. });
  596. });
  597. // https://github.com/tbranyen/backbone.layoutmanager/issues/118
  598. test("events not correctly bound", 1, function() {
  599. var hit = false;
  600. var m = new Backbone.Model();
  601. var EventView = Backbone.LayoutView.extend({
  602. events: {
  603. click: "myFunc"
  604. },
  605. myFunc: function() {
  606. hit = true;
  607. },
  608. cleanup: function() {
  609. this.model.off(null, null, this);
  610. },
  611. initialize: function() {
  612. this.model.on("change", this.render, this);
  613. }
  614. });
  615. var Layout = Backbone.LayoutView.extend({
  616. template: "<p></p>",
  617. fetch: function(name) {
  618. return _.template(name);
  619. },
  620. beforeRender: function() {
  621. var eventView = this.insertView("p", new EventView({
  622. model: m
  623. }));
  624. }
  625. });
  626. var view = new Layout();
  627. view.$el.appendTo("#container");
  628. view.render().then(function() {
  629. view.views.p[0].$el.click();
  630. ok(hit, "Event was fired");
  631. });
  632. });
  633. // https://github.com/tbranyen/backbone.layoutmanager/issues/122
  634. test("afterRender() not called on item added with insertView()", 2, function() {
  635. var hitAfter = 0;
  636. var hitBefore = 0;
  637. var m = new Backbone.Model();
  638. var Item = Backbone.LayoutView.extend({
  639. template: "",
  640. fetch: function(path) {
  641. return _.template(path);
  642. },
  643. tagName: "tr",
  644. beforeRender: function() {
  645. hitBefore = hitBefore + 1;
  646. },
  647. afterRender: function() {
  648. hitAfter = hitAfter + 1;
  649. },
  650. cleanup: function() {
  651. this.model.off(null, null, this);
  652. },
  653. initialize: function() {
  654. this.model.on("change", this.render, this);
  655. }
  656. });
  657. var List = Backbone.LayoutView.extend({
  658. template: "<tbody></tbody>",
  659. fetch: function(path) {
  660. return _.template(path);
  661. },
  662. beforeRender: function() {
  663. // Pass the model through.
  664. this.insertView("tbody", new Item({ model: m }));
  665. }
  666. });
  667. var list = new List({ model: m });
  668. list.render().then(function() {
  669. m.set("something", "changed");
  670. equal(hitBefore, 2, "beforeRender hit twice");
  671. equal(hitAfter, 2, "afterRender hit twice");
  672. });
  673. });
  674. // https://github.com/tbranyen/backbone.layoutmanager/issues/126
  675. test("render works when called late", 1, function() {
  676. var hit = false;
  677. var A = Backbone.Model.extend({});
  678. var ACollection = Backbone.Collection.extend({ model: A });
  679. var View = Backbone.LayoutView.extend({
  680. template: "<div>Click Here</div>",
  681. className: "hitMe",
  682. fetch: function(path) {
  683. return _.template(path);
  684. },
  685. events: {
  686. click: "onClick"
  687. },
  688. initialize: function() {
  689. this.collection.on("reset", function() {
  690. this.render();
  691. }, this);
  692. },
  693. onClick: function() {
  694. hit = true;
  695. }
  696. });
  697. var collection = new ACollection([]);
  698. var layout = new Backbone.Layout({
  699. template: "<div class='button'></div>",
  700. fetch: function(path) {
  701. return _.template(path);
  702. },
  703. views: {
  704. ".button": new View({ collection: collection })
  705. }
  706. });
  707. layout.render();
  708. collection.reset([]);
  709. // Simulate click.
  710. layout.$(".hitMe").click();
  711. ok(hit, "render works as expected when called late");
  712. });
  713. // https://github.com/tbranyen/backbone.layoutmanager/issues/126
  714. test("render works when assigned early", 1, function() {
  715. var hit = false;
  716. var A = Backbone.Model.extend({});
  717. var ACollection = Backbone.Collection.extend({ model: A });
  718. var View = Backbone.LayoutView.extend({
  719. template: "<div>Click Here</div>",
  720. className: "hitMe",
  721. fetch: function(path) {
  722. return _.template(path);
  723. },
  724. events: {
  725. click: "onClick"
  726. },
  727. initialize: function() {
  728. this.collection.on("reset", this.render, this);
  729. },
  730. onClick: function() {
  731. hit = true;
  732. }
  733. });
  734. var collection = new ACollection([]);
  735. var layout = new Backbone.Layout({
  736. template: "<div class='button'></div>",
  737. fetch: function(path) {
  738. return _.template(path);
  739. },
  740. views: {
  741. ".button": new View({ collection: collection })
  742. }
  743. });
  744. layout.render();
  745. collection.reset([]);
  746. // Simulate click.
  747. layout.$(".hitMe").click();
  748. ok(hit, "render works as expected when assigned early");
  749. });
  750. test("Render doesn't work inside insertView", 1, function() {
  751. var V = Backbone.LayoutView.extend({
  752. template: "<p class='inner'><%= lol %></p>",
  753. fetch: function(path) { return _.template(path); }
  754. });
  755. var n = new Backbone.LayoutView({
  756. template: "<p></p>",
  757. fetch: function(path) { return _.template(path); }
  758. });
  759. n.render();
  760. n.insertView("p", new V({ serialize: { lol: "hi" } })).render();
  761. equal(n.$("p.inner").html(), "hi", "Render works with insertView");
  762. });
  763. // https://github.com/tbranyen/backbone.layoutmanager/issues/134
  764. test("Ensure events are copied over properly", 1, function() {
  765. var hit = false;
  766. var layout = new Backbone.Layout({
  767. template: "<p></p>",
  768. fetch: function(path) { return _.template(path); },
  769. events: {
  770. "click p": "test"
  771. },
  772. test: function(ev) {
  773. hit = true;
  774. }
  775. });
  776. layout.render();
  777. layout.$("p").click();
  778. ok(hit, "Events were bound and triggered correctly");
  779. });
  780. // https://github.com/tbranyen/backbone.layoutmanager/issues/131
  781. test("Ensure global paths are adhered to", 1, function() {
  782. Backbone.LayoutManager.configure({
  783. paths: {
  784. template: "test/"
  785. }
  786. });
  787. var t = new Backbone.LayoutView({
  788. template: "here"
  789. });
  790. equal(t.__manager__.prefix, "test/", "Prefix properly hooked up");
  791. Backbone.LayoutManager.configure({
  792. paths: {}
  793. });
  794. });
  795. // https://github.com/tbranyen/backbone.layoutmanager/issues/137
  796. test("afterRender not firing", 1, function() {
  797. var hit = false;
  798. var l = new Backbone.Layout({
  799. template: "<p></p>",
  800. fetch: function(path) { return _.template(path); }
  801. });
  802. l.render();
  803. var V = Backbone.LayoutView.extend({
  804. template: "<span>hey</span>",
  805. fetch: function(path) { return _.template(path); },
  806. afterRender: function() {
  807. hit = true;
  808. }
  809. });
  810. l.setView("p", new V()).render();
  811. ok(hit, "afterRender was hit successfully");
  812. });
  813. // https://github.com/tbranyen/backbone.layoutmanager/issues/134
  814. // https://github.com/tbranyen/backbone.layoutmanager/issues/139
  815. asyncTest("events are bound correctly", 1, function() {
  816. var hit = 0;
  817. var l = new Backbone.Layout({
  818. template: "<p></p>",
  819. fetch: function(path) { return _.template(path); }
  820. });
  821. l.render();
  822. var V = Backbone.LayoutView.extend({
  823. keep: true,
  824. template: "<span>hey</span>",
  825. fetch: function(path) { return _.template(path); },
  826. events: {
  827. click: "hit"
  828. },
  829. hit: function(ev) {
  830. hit++;
  831. }
  832. });
  833. // Insert two views.
  834. l.insertView("p", new V());
  835. l.insertView("p", new V());
  836. // Render twice.
  837. l.render();
  838. l.render().then(function() {
  839. l.$("p div").trigger("click");
  840. equal(hit, 2, "Event handler is bound and fired correctly");
  841. start();
  842. });
  843. });
  844. asyncTest("more events issues", 1, function() {
  845. var hit = 0;
  846. var V = Backbone.LayoutView.extend({
  847. template: "<span>hey</span>",
  848. fetch: function(path) { return _.template(path); },
  849. events: {
  850. click: "hit"
  851. },
  852. hit: function(ev) {
  853. hit++;
  854. }
  855. });
  856. var S = Backbone.LayoutView.extend({
  857. template: "<p></p>",
  858. fetch: function(path) { return _.template(path); },
  859. beforeRender: function() {
  860. // Insert two views.
  861. this.insertView("p", new V());
  862. this.insertView("p", new V());
  863. },
  864. reset: function() {
  865. this.render();
  866. }
  867. });
  868. // Create a sub-layout.
  869. var s = new S();
  870. // Create a main layout.
  871. var l = new Backbone.Layout({
  872. views: {
  873. "": s
  874. }
  875. });
  876. // Render the layout.
  877. l.render();
  878. // Re-render.
  879. s.reset();
  880. l.$("p div").trigger("click");
  881. equal(hit, 2, "Event handler is bound and fired correctly");
  882. start();
  883. });