diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index b53851cec9..57c046ad2f 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -605,7 +605,9 @@ export default class Generator { if (templateProperties.oncreate && dom) { addDeclaration('oncreate', templateProperties.oncreate.value); } - + if (templateProperties.onunmount && dom) { + addDeclaration('onunmount', templateProperties.onunmount.value); + } if (templateProperties.onteardown) templateProperties.ondestroy = templateProperties.onteardown; // remove after v2 if (templateProperties.ondestroy && dom) { addDeclaration('ondestroy', templateProperties.ondestroy.value); diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 48e205f64c..53f2fc5978 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -121,6 +121,10 @@ export default class Block { if (this.key) this.aliases.set('key', this.getUniqueName('key')); this.hasUpdateMethod = false; // determined later + + if (this.generator.templateProperties.onunmount) { + this.builders.unmount.addLine(`%onunmount.call(#component${this.key ? `, ${this.getUniqueName('key')}` : ''})`); + } } addDependencies(dependencies: string[]) { diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index f02b013d02..aaf6bd8043 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -225,6 +225,11 @@ export default function dom( [templateProperties.ondestroy && `%ondestroy`, storeProps.length && `@removeFromStore`].filter(Boolean).join(', ') }];` )} + ${(templateProperties.onunmount || storeProps.length) && ( + `this._handlers.unmount = [${ + [templateProperties.onunmount && `%onunmount`, storeProps.length && `@removeFromStore`].filter(Boolean).join(', ') + }];` + )} ${generator.slots.size && `this._slotted = options.slots || {};`} diff --git a/src/validate/js/propValidators/index.ts b/src/validate/js/propValidators/index.ts index 05819b0837..02934c35de 100644 --- a/src/validate/js/propValidators/index.ts +++ b/src/validate/js/propValidators/index.ts @@ -1,6 +1,7 @@ import data from './data'; import computed from './computed'; import oncreate from './oncreate'; +import onunmount from './onunmount'; import ondestroy from './ondestroy'; import onrender from './onrender'; import onteardown from './onteardown'; @@ -21,6 +22,7 @@ export default { data, computed, oncreate, + onunmount, ondestroy, onrender, onteardown, diff --git a/src/validate/js/propValidators/onunmount.ts b/src/validate/js/propValidators/onunmount.ts new file mode 100644 index 0000000000..58dc5db18f --- /dev/null +++ b/src/validate/js/propValidators/onunmount.ts @@ -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 + ); + } + } +} diff --git a/test/runtime/samples/lifecycle-events/Nested.html b/test/runtime/samples/lifecycle-events/Nested.html new file mode 100644 index 0000000000..99edfa147e --- /dev/null +++ b/test/runtime/samples/lifecycle-events/Nested.html @@ -0,0 +1,17 @@ +
+ + \ No newline at end of file diff --git a/test/runtime/samples/lifecycle-events/_config.js b/test/runtime/samples/lifecycle-events/_config.js index 2a9d25564a..07af44c2dc 100644 --- a/test/runtime/samples/lifecycle-events/_config.js +++ b/test/runtime/samples/lifecycle-events/_config.js @@ -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 }); } }; diff --git a/test/runtime/samples/lifecycle-events/main.html b/test/runtime/samples/lifecycle-events/main.html index b39ce292d6..e07f634006 100644 --- a/test/runtime/samples/lifecycle-events/main.html +++ b/test/runtime/samples/lifecycle-events/main.html @@ -1,11 +1,19 @@ -
+