mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-08-29 02:31:20 +08:00
32 lines
525 B
JavaScript
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,
|
|
};
|
|
}
|