mirror of https://github.com/sveltejs/svelte
Add onunmount hook (#1212)
parent
0ac77019d0
commit
e769787c50
@ -0,0 +1,14 @@
|
||||
import usesThisOrArguments from '../utils/usesThisOrArguments';
|
||||
import { Validator } from '../../';
|
||||
import { Node } from '../../../interfaces';
|
||||
|
||||
export default function onunmont(validator: Validator, prop: Node) {
|
||||
if (prop.value.type === 'ArrowFunctionExpression') {
|
||||
if (usesThisOrArguments(prop.value.body)) {
|
||||
validator.error(
|
||||
`'onunmont' should be a function expression, not an arrow function expression`,
|
||||
prop.start
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<div ref:div></div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
oncreate() {
|
||||
this.events = {create: this.refs.div.parentNode === null};
|
||||
},
|
||||
|
||||
onunmount() {
|
||||
this.events.unmount = this.refs.div.parentNode === null;
|
||||
},
|
||||
|
||||
ondestroy() {
|
||||
this.events.destroy = this.refs.div.parentNode === null;
|
||||
}
|
||||
}
|
||||
</script>
|
@ -1,7 +1,11 @@
|
||||
export default {
|
||||
test(assert, component) {
|
||||
const nested = component.refs.nested;
|
||||
|
||||
assert.deepEqual(component.events, ['create']);
|
||||
assert.deepEqual(nested.events, { create: false });
|
||||
component.destroy();
|
||||
assert.deepEqual(component.events, ['create', 'destroy']);
|
||||
assert.deepEqual(component.events, ['create', 'destroy', 'unmount']);
|
||||
assert.deepEqual(nested.events, { create: false, unmount: false, destroy: true });
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in new issue