vant/src/mixins/bind-event.js
neverland 3748ab646c
feat(ImagePreview): fit window resize (#6760)
* feat(ImagePreview): fit window resize

* fix(ImagePreview): incorrect max move
2020-07-11 11:25:57 +08:00

32 lines
525 B
JavaScript

/**
* Bind event when mounted or activated
*/
import { on, off } from '../utils/dom/event';
let uid = 0;
export function BindEventMixin(handler) {
const key = `binded_${uid++}`;
function bind() {
if (!this[key]) {
handler.call(this, on, true);
this[key] = true;
}
}
function unbind() {
if (this[key]) {
handler.call(this, off, false);
this[key] = false;
}
}
return {
mounted: bind,
activated: bind,
deactivated: unbind,
beforeDestroy: unbind,
};
}