From df66f486abf3e3a6c7fb01f591241f68ca6622ff Mon Sep 17 00:00:00 2001 From: Bryan Spears Date: Wed, 1 Jul 2015 15:00:50 -0700 Subject: [PATCH] Fix issue #87 - Subviews hash should append el --- ampersand-view.js | 11 +++++++---- test/main.js | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/ampersand-view.js b/ampersand-view.js index ab529c8..dc8d860 100644 --- a/ampersand-view.js +++ b/ampersand-view.js @@ -287,9 +287,8 @@ assign(View.prototype, { var opts = { selector: subview.selector || '[data-hook="' + subview.hook + '"]', waitFor: subview.waitFor || '', - prepareView: subview.prepareView || function (el) { + prepareView: subview.prepareView || function () { return new subview.constructor({ - el: el, parent: self }); } @@ -300,8 +299,12 @@ assign(View.prototype, { if (!this.el || !(el = this.query(opts.selector))) return; if (!opts.waitFor || getPath(this, opts.waitFor)) { subview = this[name] = opts.prepareView.call(this, el); - subview.render(); - this.registerSubview(subview); + if (!subview.el) { + this.renderSubview(subview, el); + } else { + subview.render(); + this.registerSubview(subview); + } this.off('change', action); } } diff --git a/test/main.js b/test/main.js index f2ecd0a..4c4ce64 100644 --- a/test/main.js +++ b/test/main.js @@ -775,6 +775,33 @@ test('declarative subViews basics', function (t) { }); var view = new View(); + t.equal(view.el.innerHTML, '
'); + + t.end(); +}); + +test('declarative subViews basics (with prepareView which tests pre-issue#87 functionality)', function (t) { + var Sub = AmpersandView.extend({ + template: '' + }); + + var View = AmpersandView.extend({ + template: '
', + autoRender: true, + subviews: { + sub1: { + selector: '.container', + constructor: Sub, + prepareView: function (el) { + return new Sub({ + el: el + }); + } + } + } + }); + var view = new View(); + t.equal(view.el.innerHTML, ''); t.end(); @@ -797,7 +824,7 @@ test('subViews declaraction can accept a CSS selector string via `container` pro }); var view = new View(); - t.equal(view.el.innerHTML, ''); + t.equal(view.el.innerHTML, '
'); t.end(); }); @@ -819,7 +846,7 @@ test('subview hook can include special characters', function (t) { }); var view = new View(); - t.equal(view.el.innerHTML, ''); + t.equal(view.el.innerHTML, '
'); t.end(); }); @@ -853,9 +880,9 @@ test('make sure subviews dont fire until their `waitFor` is done', function (t) t.equal(view.el.outerHTML, '
'); view.model = new Model(); t.equal(view._events.change.length, 1); - t.equal(view.el.outerHTML, '
yes
'); + t.equal(view.el.outerHTML, '
yes
'); view.model2 = new Model(); - t.equal(view.el.outerHTML, '
yesyes
'); + t.equal(view.el.outerHTML, '
yesyes
'); t.notOk(view._events.change); t.end();