Skip to content

Commit 314a620

Browse files
authored
Merge pull request #34 from boxcc/master
修复动态加载新Link不绑定scroll事件的问题
2 parents b984c9e + a64a692 commit 314a620

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/EventDispatcher.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ EventDispatcher.prototype = {
1010
const types = type.split('.');
1111
const _type = types[0];
1212
const namespaces = types[1];
13-
let list = this._listeners[_type];
13+
const listName = `${_type}${target ? target.getAttribute('id') : ''}`;
14+
let list = this._listeners[listName];
1415
let index = 0;
1516
let listener;
1617
let i;
1718
if (!list) {
18-
this._listeners[_type] = list = [];
19+
this._listeners[listName] = list = [];
1920
}
2021
i = list.length;
2122

@@ -29,12 +30,12 @@ EventDispatcher.prototype = {
2930
}
3031

3132
list.splice(index, 0, { c: callback, n: namespaces, t: _type });
32-
if (!this._listFun[_type]) {
33-
this._listFun[_type] = this._listFun[_type] || this.dispatchEvent.bind(this, _type);
33+
if (!this._listFun[listName]) {
34+
this._listFun[listName] = this._listFun[listName] || this.dispatchEvent.bind(this, _type, target);
3435
if (this._eventTarget.addEventListener) {
35-
(target || this._eventTarget).addEventListener(_type, this._listFun[_type], false);
36+
(target || this._eventTarget).addEventListener(_type, this._listFun[listName], false);
3637
} else if (this._eventTarget.attachEvent) {
37-
(target || this._eventTarget).attachEvent(`on${_type}`, this._listFun[_type]);
38+
(target || this._eventTarget).attachEvent(`on${_type}`, this._listFun[listName]);
3839
}
3940
}
4041
},
@@ -43,7 +44,8 @@ EventDispatcher.prototype = {
4344
const types = type.split('.');
4445
const _type = types[0];
4546
const namespaces = types[1];
46-
const list = this._listeners[_type];
47+
const listName = `${_type}${target ? target.getAttribute('id') : ''}`;
48+
const list = this._listeners[listName];
4749
let i;
4850
let _force = force;
4951
if (!namespaces) {
@@ -55,9 +57,9 @@ EventDispatcher.prototype = {
5557
if (list[i].c === callback && (_force || list[i].n === namespaces)) {
5658
list.splice(i, 1);
5759
if (!list.length) {
58-
const func = this._listFun[_type];
59-
delete this._listeners[_type];
60-
delete this._listFun[_type];
60+
const func = this._listFun[listName];
61+
delete this._listeners[listName];
62+
delete this._listFun[listName];
6163
if (this._eventTarget.removeEventListener) {
6264
(target || this._eventTarget).removeEventListener(_type, func);
6365
} else if (this._eventTarget.detachEvent) {
@@ -72,8 +74,9 @@ EventDispatcher.prototype = {
7274
}
7375
},
7476

75-
dispatchEvent(type, e) {
76-
const list = this._listeners[type];
77+
dispatchEvent(type, target, e) {
78+
const listName = `${type}${target ? target.getAttribute('id') : ''}`;
79+
const list = this._listeners[listName];
7780
let i;
7881
let t;
7982
let listener;
@@ -93,7 +96,8 @@ EventDispatcher.prototype = {
9396
const types = type.split('.');
9497
const _type = types[0];
9598
const namespaces = types[1];
96-
const list = this._listeners[_type];
99+
const listName = `${type}${target ? target.getAttribute('id') : ''}`;
100+
const list = this._listeners[listName];
97101
this.recoverLists = this.recoverLists.concat(dataToArray(list).filter(item =>
98102
item.n && item.n.match(namespaces)
99103
));

0 commit comments

Comments
 (0)