-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX beta] Prevent
willDestroyElement
from being called twice.
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
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
packages/ember-htmlbars/tests/integration/will-destroy-element-hook-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |