[improvement] mixins typescript (#3690)

This commit is contained in:
neverland
2019-06-29 11:31:53 +08:00
committed by GitHub
parent 0a79536a05
commit abe3e59bed
17 changed files with 135 additions and 82 deletions
+31
View File
@@ -0,0 +1,31 @@
/**
* Bind event when mounted or activated
*/
import { on, off } from '../utils/dom/event';
type BindEventMixinThis = {
binded: boolean;
};
export function BindEventMixin(handler: Function) {
function bind(this: BindEventMixinThis) {
if (!this.binded) {
handler.call(this, on, true);
this.binded = true;
}
}
function unbind(this: BindEventMixinThis) {
if (this.binded) {
handler.call(this, off, false);
this.binded = false;
}
}
return {
mounted: bind,
activated: bind,
deactivated: unbind,
beforeDestroy: unbind
};
}