-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.0.0.1.js
179 lines (135 loc) · 3.87 KB
/
driver.0.0.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Created by wd14931 on 2015/11/28.
* 1:实现事件驱动
* 2:实现一个数据驱动
*
* 最后结合起来
*/
function Dirver(){
var that = this;
/*if(obj && typeof obj === 'object'
&& Object.prototype.toString(obj).substring(1, 7) === 'object'){
that.obj = obj;
}
*/
// 事件类型的队列
var typeList = [];
var self = arguments.callee,
prop = self.prototype;
that.set= function(arg){
typeList.push(arg);
};
that.get = function(){
return typeList;
};
/*
* on: 绑定事件,支持字符串,或者多个事件可以按空格分开
* */
prop.on= function(type, fn){
var that = this;
var _aType = [],
_fn = null;
if(type && typeof type === 'string'){
_aType = type.split(' ');
}else{
console.error('event name does not exist!', 'on');
}
if(fn && typeof fn === 'function'){
_fn = fn;
}else{
console.error('callback function does not exist!', 'on');
}
// 绑定事件
var _bind = function(type, fn){
var _that = this;
for(var i = 0; type[i]; i++){
var t = type[i];
// 事件建立引用
var _obj = {};
_obj.type = t;
_obj.fn = fn;
// 存储到事件列表
_that.set(_obj);
}
};
return _bind.call(that, _aType, _fn);
};
/*
* trigger: 触发事件,支持字符串,或者多个事件可以按空格分开
* 第二个以后的都会默认为参数
*
* */
prop.trigger= function(type, arg){
var that = this,
_aType = [],
_arg = [];
if(type && typeof type === 'string'){
_aType = type.split(' ');
}else{
console.error('event name is not correct!', 'trigger');
}
if(arguments.length > 1){
for(var j= 1; arguments[j]; j++){
_arg[j-1] = arguments[j];
}
}
// 触发
var _trigger = function(type, arg){
var _that = this,
_list = _that.get();
for(var i = 0; type[i]; i++){
var t = type[i];
for(var m = 0; _list[m]; m++){
var _t = _list[m]['type'];
if(_t === t){
_list[m]['fn'].apply(_that, arg);
}
}
}
};
return _trigger.call(that, _aType, _arg.length > 0? _arg : []);
};
/*
* 根据type,移除某一事件
* */
prop.remove= function(type){
var that = this;
var _aType = [];
if(type && typeof type === 'string'){
_aType = type.split(' ');
}
return function(type){
var _that = this,
_list = _that.get();
if(!type.length){
return;
}
for(var i = 0; type[i]; i++){
var t = type[i];
for(var m = 0; _list[m]; m++){
var _t = _list[m]['type'];
if(_t === t){
_list.splice(m,1);
}
}
}
}.call(that, _aType);
};
/*
* 移除所有的事件
* */
prop.removeAll= function(){
var that = this;
return function(){
var _that = this,
_list = _that.get();
_list.splice(0,_list.length);
}.call(that);
};
}
var fire1 = new Dirver();
var fire2 = new Dirver();
fire1.on('fire f', function(ss, ddd){alert(ss+ddd)});
fire1.on('fire f1', function(ss, ddd){alert(ss+ddd+2)});
fire2.on('fire', function(ss){alert(ss)});
fire1.trigger('fire', 12, 13);