Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix view destruction inside outlets #11577

Merged
merged 2 commits into from
Jun 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/ember-htmlbars/lib/keywords/real_outlet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export default {
toRender.template = topLevelViewTemplate;
}

return { outletState: selectedOutletState, hasParentOutlet: env.hasParentOutlet };
return {
outletState: selectedOutletState,
hasParentOutlet: env.hasParentOutlet,
manager: state.manager
};
},

childEnv(state, env) {
Expand Down Expand Up @@ -68,6 +72,11 @@ export default {
Ember.Logger.info('Rendering ' + toRender.name + ' with ' + ViewClass, { fullName: 'view:' + toRender.name });
}

if (state.manager) {
state.manager.destroy();
state.manager = null;
}

var nodeManager = ViewNodeManager.create(renderNode, env, {}, options, parentView, null, null, template);
state.manager = nodeManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ ViewNodeManager.prototype.rerender = function(env, attrs, visitor) {
};

ViewNodeManager.prototype.destroy = function() {
this.component.destroy();
if (this.component) {
this.component.destroy();
this.component = null;
}
};

function getTemplate(componentOrView) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-metal-views/lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Renderer.prototype.renderElementRemoval =
if (view._willRemoveElement) {
view._willRemoveElement = false;

if (view._renderNode) {
if (view._renderNode && view.element && view.element.parentNode) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the especially hacky bit. Without this, views that are already out-of-DOM will delete unrelated subsequent content that has replaced them in the same render node.

view._renderNode.clear();
}
this.didDestroyElement(view);
Expand Down
25 changes: 25 additions & 0 deletions packages/ember-routing-htmlbars/tests/helpers/outlet_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,31 @@ QUnit.test('{{outlet}} should rerender when bound name changes', function() {
equal(top.$().text().trim(), 'second');
});

QUnit.test('views created by {{outlet}} should get destroyed', function() {
let inserted = 0;
let destroyed = 0;
var routerState = {
render: {
ViewClass: EmberView.extend({
didInsertElement() {
inserted++;
},
willDestroyElement() {
destroyed++;
}
})
},
outlets: {}
};
top.setOutletState(routerState);
runAppend(top);
equal(inserted, 1, 'expected to see view inserted');
run(function() {
top.setOutletState(withTemplate('hello world'));
});
equal(destroyed, 1, 'expected to see view destroyed');
});


function withTemplate(string) {
return {
Expand Down