Skip to content

Commit

Permalink
[Bugfix beta] Component.prototype.layout is now settable.
Browse files Browse the repository at this point in the history
Previously setting `layout` would replace the underlying CP. This would prevent `layoutName` from working when a component had an existing template.
  • Loading branch information
stefanpenner committed Apr 22, 2015
1 parent a8400e6 commit 5f0d067
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/ember-views/lib/views/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,20 @@ var View = CoreView.extend(
@property layout
@type Function
*/
layout: computed('layoutName', function(key) {
var layoutName = get(this, 'layoutName');
var layout = this.templateForName(layoutName, 'layout');
*/
layout: computed('layoutName', {
get(key) {
var layoutName = get(this, 'layoutName');
var layout = this.templateForName(layoutName, 'layout');

Ember.assert("You specified the layoutName " + layoutName + " for " + this + ", but it did not exist.", !layoutName || !!layout);

Ember.assert("You specified the layoutName " + layoutName + " for " + this + ", but it did not exist.", !layoutName || !!layout);
return layout || get(this, 'defaultLayout');
},

return layout || get(this, 'defaultLayout');
set(key, value) {
return value;
}
}),

_yield(context, options, morph) {
Expand Down
53 changes: 53 additions & 0 deletions packages/ember-views/tests/views/view/layout_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,59 @@ QUnit.test("should call the function of the associated layout", function() {
equal(layoutCalled, 1, "layout is called when layout is present");
});

QUnit.test("changing layoutName after setting layoutName continous to work", function() {
var layoutCalled = 0;
var otherLayoutCalled = 0;

registry.register('template:layout', function() { layoutCalled++; });
registry.register('template:other-layout', function() { otherLayoutCalled++; });

view = EmberView.create({
container: container,
layoutName: 'layout'
});

run(view, 'createElement');
equal(layoutCalled, 1, "layout is called when layout is present");
equal(otherLayoutCalled, 0, "otherLayout is not yet called");

run(() => {
view.set('layoutName', 'other-layout');
view.rerender();
});

equal(layoutCalled, 1, "layout is called when layout is present");
equal(otherLayoutCalled, 1, "otherLayoutis called when layoutName changes, and explicit rerender occurs");
});

QUnit.test("changing layoutName after setting layout CP continous to work", function() {
var layoutCalled = 0;
var otherLayoutCalled = 0;
function otherLayout() {
otherLayoutCalled++;
}

registry.register('template:other-layout', otherLayout);

view = EmberView.create({
container: container,
layout() {
layoutCalled++;
}
});

run(view, 'createElement');
run(() => {
view.set('layoutName', 'other-layout');
view.rerender();
});

equal(view.get('layout'), otherLayout);

equal(layoutCalled, 1, "layout is called when layout is present");
equal(otherLayoutCalled, 1, "otherLayoutis called when layoutName changes, and explicit rerender occurs");
});

QUnit.test("should call the function of the associated template with itself as the context", function() {
registry.register('template:testTemplate', function(dataSource) {
return "<h1 id='twas-called'>template was called for " + get(dataSource, 'personName') + "</h1>";
Expand Down

0 comments on commit 5f0d067

Please sign in to comment.