Skip to content

Commit c474f23

Browse files
dpp050700juzi214032
authored andcommitted
fix(Form):修复再次打开页面不能验证的问题
close #919
1 parent 7ca4e80 commit c474f23

File tree

4 files changed

+49
-71
lines changed

4 files changed

+49
-71
lines changed

dist/core/utils/event-bus.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/form/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/utils/event-bus.js

Lines changed: 39 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,48 @@
1-
//创建EventBus对象
2-
let EventBus = function () {};
3-
//准备数组容器
4-
var objBus = [], arrBus = [];
5-
//添加方法
6-
EventBus.prototype = {
7-
obj: {
8-
set: function (key, action) {
9-
if (key && action) {
10-
var map = {};
11-
map.k = key;
12-
map.v = action;
13-
//如果存在,则删除之前添加的事件
14-
for (var i = 0, busLength = objBus.length; i < busLength; i++) {
15-
var tempMap = objBus[i];
16-
if (tempMap.k === key) {
17-
objBus.splice(i, 1);
18-
}
19-
}
20-
objBus.push(map);
21-
}
22-
},
23-
get: function (key) {
24-
if (key) {
25-
for (var i = 0, busLength = objBus.length; i < busLength; i++) {
26-
var map = objBus[i];
27-
if (map.k === key) {
28-
return map.v();
29-
}
30-
}
1+
function EventBusClass() {
2+
this.msgQueues = {};
3+
}
4+
5+
EventBusClass.prototype = {
6+
// 将消息保存到当前的消息队列中
7+
on: function(msgName, func) {
8+
if (Object.prototype.hasOwnProperty.call(this.msgQueues, msgName)) {
9+
if (typeof this.msgQueues[msgName] === 'function') {
10+
this.msgQueues[msgName] = [this.msgQueues[msgName], func];
11+
} else {
12+
this.msgQueues[msgName] = [...this.msgQueues[msgName], func];
3113
}
14+
} else {
15+
this.msgQueues[msgName] = func;
3216
}
3317
},
34-
emit: function (key, data) {
35-
if (key) {
36-
for (var i = 0, busLength = arrBus.length; i < busLength; i++) {
37-
var map = arrBus[i];
38-
if (map.k === key) {
39-
return map.v(data);
40-
}
41-
}
42-
}
43-
return new Promise((resolve) => { resolve();});
18+
// 消息队列中仅保存一个消息
19+
one: function(msgName, func) {
20+
// 无需检查msgName是否存在
21+
this.msgQueues[msgName] = func;
4422
},
45-
on: function (key, action) {
46-
if (key && action) {
47-
var map = {};
48-
map.k = key;
49-
map.v = action;
50-
arrBus.push(map);
23+
// 发送消息
24+
emit: function(msgName, msg) {
25+
if (!Object.prototype.hasOwnProperty.call(this.msgQueues, msgName)) {
26+
return;
27+
}
28+
if (typeof this.msgQueues[msgName] === 'function') {
29+
this.msgQueues[msgName](msg);
30+
} else {
31+
this.msgQueues[msgName].map((fn) => {
32+
fn(msg);
33+
});
5134
}
5235
},
53-
arr: {
54-
push: function (key, action) {
55-
if (key && action) {
56-
var map = {};
57-
map.k = key;
58-
map.v = action;
59-
arrBus.push(map);
60-
}
61-
},
62-
pop: function (key) {
63-
if (key) {
64-
for (var i = 0, busLength = arrBus.length; i < busLength; i++) {
65-
var map = arrBus[i];
66-
if (map.k === key) {
67-
map.v();
68-
}
69-
}
70-
}
36+
// 移除消息
37+
off: function(msgName) {
38+
if (!Object.prototype.hasOwnProperty.call(this.msgQueues, msgName)) {
39+
return;
7140
}
41+
delete this.msgQueues[msgName];
7242
}
7343
};
74-
var eventBus = new EventBus();
75-
export default eventBus;
76-
// module.exports = {
77-
// eventBus: eventBus
78-
// }
44+
45+
// 将EventBus放到window对象中
46+
const EventBus = new EventBusClass();
47+
48+
export default EventBus;

src/form/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ Component({
3434
attached() {
3535
this._init();
3636
},
37+
detached() {
38+
for(let key in this._keys) {
39+
if (Object.prototype.hasOwnProperty.call(this._keys, key)) {
40+
eventBus.off(`lin-form-blur-${key}`);
41+
eventBus.off(`lin-form-change-${key}`);
42+
}
43+
}
44+
},
3745

3846
/**
3947
* 组件的初始数据

0 commit comments

Comments
 (0)