UNPKG

1.4 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('./Base');
7
8module.exports = class Behavior extends Base {
9
10 _handlers = {};
11
12 attach (owner) {
13 this.owner = owner;
14 this.module = owner.module;
15 for (const name of Object.keys(this._handlers)) {
16 this.owner.on(name, this._handlers[name]);
17 }
18 }
19
20 detach () {
21 if (this.owner) {
22 for (const name of Object.keys(this._handlers)) {
23 this.owner.off(name, this._handlers[name]);
24 }
25 this.owner = null;
26 }
27 }
28
29 hasHandler (eventName) {
30 return Object.prototype.hasOwnProperty.call(this._handlers, eventName);
31 }
32
33 setHandler (eventName, method) {
34 this._handlers[eventName] = method.bind(this);
35 }
36
37 attachHandler (eventName) {
38 if (this.owner && this.hasHandler(eventName)) {
39 this.owner.on(eventName, this._handlers[eventName]);
40 }
41 }
42
43 detachHandler (eventName) {
44 if (this.owner && this.hasHandler(eventName)) {
45 this.owner.off(eventName, this._handlers[eventName]);
46 }
47 }
48
49 log () {
50 CommonHelper.log(this.owner, this.constructor.name, ...arguments);
51 }
52};
53
54const CommonHelper = require('../helper/CommonHelper');
\No newline at end of file