Skip to content

Commit

Permalink
[BUGFIX beta] Prevent willDestroyElement from being called twice.
Browse files Browse the repository at this point in the history
The node-managers ensure that `willDestroyElement` (and other hooks)
are called properly (by walking the much more precise `view._renderNode`
tree), having the renderer's `willDestroyElement` also traverse `childViews`
leads to `willDestroyElement` getting called twice in cases other than those
for `ContainerView`.
  • Loading branch information
rwjblue committed Jun 12, 2015
1 parent e651706 commit 0d38735
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import run from 'ember-metal/run_loop';
import Component from 'ember-views/views/component';
import compile from 'ember-template-compiler/system/compile';
import { runAppend, runDestroy } from "ember-runtime/tests/utils";

import { set } from 'ember-metal/property_set';

var component;

QUnit.module('ember-htmlbars: destroy-element-hook tests', {
teardown() {
runDestroy(component);
}
});

QUnit.test('willDestroyElement is only called once when a component leaves scope', function(assert) {
var done = assert.async();
var innerChild, innerChildDestroyed;

component = Component.create({
switch: true,

layout: compile(`
{{~#if switch~}}
{{~#view innerChild}}Truthy{{/view~}}
{{~/if~}}
`),

innerChild: Component.extend({
init() {
this._super(...arguments);
innerChild = this;
},

willDestroyElement() {
if (innerChildDestroyed) {
throw new Error('willDestroyElement has already been called!!');
} else {
innerChildDestroyed = true;
}
}
})
});

runAppend(component);

assert.equal(component.$().text(), 'Truthy', 'precond - truthy template is displayed');
assert.equal(component.get('childViews.length'), 1);

run.later(function() {
set(component, 'switch', false);

run.later(function() {
assert.equal(innerChild.get('isDestroyed'), true, 'the innerChild has been destroyed');
assert.equal(component.$().text(), '', 'truthy template is removed');

done();
});
});
});

0 comments on commit 0d38735

Please sign in to comment.