bind-event.js 519 B

1234567891011121314151617181920212223242526272829
  1. /**
  2. * Bind event when mounted or activated
  3. */
  4. import { on, off } from '../utils/dom/event';
  5. var uid = 0;
  6. export function BindEventMixin(handler) {
  7. var key = "binded_" + uid++;
  8. function bind() {
  9. if (!this[key]) {
  10. handler.call(this, on, true);
  11. this[key] = true;
  12. }
  13. }
  14. function unbind() {
  15. if (this[key]) {
  16. handler.call(this, off, false);
  17. this[key] = false;
  18. }
  19. }
  20. return {
  21. mounted: bind,
  22. activated: bind,
  23. deactivated: unbind,
  24. beforeDestroy: unbind
  25. };
  26. }