Skip to content

Commit dcd772d

Browse files
committed
fix events
1 parent fac560e commit dcd772d

File tree

3 files changed

+55
-43
lines changed

3 files changed

+55
-43
lines changed

common.blocks/i-bem-dom/__events/_type/i-bem-dom__events_type_bem.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ var EVENT_PREFIX = '__bem__',
3737
var event = EVENT_PREFIX + params.bindEntityCls.getEntityName() +
3838
(typeof e === 'object'?
3939
e instanceof events.Event?
40-
' ' + e.type :
40+
':' + e.type :
4141
bemInternal.buildModPostfix(e.modName, e.modVal) :
42-
' ' + e);
42+
':' + e);
4343

4444
eventMethod === 'on' && // TODO: count usage and remove on 'un'
4545
!eventsInUse[event] &&

common.blocks/i-bem-dom/__events/_type/i-bem-dom__events_type_dom.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('DOM events', function() {
4444
'js' : {
4545
'inited' : function() {
4646
this._domEvents()
47-
.on('click', spy1)
47+
.on('click keypress', spy1)
4848
.on('click', spy2)
4949
.on('click', data, wrapSpy(spy3))
5050
.once('click', spy4);

common.blocks/i-bem-dom/__events/i-bem-dom__events.js

+52-40
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,44 @@ var undef,
5252
* @returns {EventManager} this
5353
*/
5454
on : function(e, data, fn, _fnCtx, _isOnce) {
55-
var params = this._params,
56-
event = this._eventBuilder(e, params, 'on');
57-
5855
if(functions.isFunction(data)) {
5956
_isOnce = _fnCtx;
6057
_fnCtx = fn;
6158
fn = data;
6259
data = undef;
6360
}
6461

65-
var fnStorage = this._storage[event] || (this._storage[event] = {}),
66-
fnId = identify(fn, _fnCtx);
67-
68-
if(!fnStorage[fnId]) {
69-
var bindDomNodes = params.bindDomNodes,
70-
bindClassName = params.bindClassName,
71-
_this = this,
72-
handler = fnStorage[fnId] = this._handlerWrapper(
73-
_isOnce?
74-
function() {
75-
_this.un(e, fn, _fnCtx);
76-
fn.apply(this, arguments);
77-
} :
78-
fn,
79-
data,
80-
_fnCtx,
81-
fnId);
62+
var _this = this,
63+
params = this._params,
64+
bindDomNodes = params.bindDomNodes,
65+
bindClassName = params.bindClassName,
66+
fnId = identify(fn, _fnCtx),
67+
onEvent = function(eventName) {
68+
var event = this._eventBuilder(eventName, params, 'on'),
69+
fnStorage = this._storage[event] || (this._storage[event] = {});
70+
71+
if(!fnStorage[fnId]) {
72+
fnStorage[fnId] = handler? handler : handler = this._handlerWrapper(
73+
_isOnce?
74+
function() {
75+
_this.un(e, fn, _fnCtx);
76+
fn.apply(this, arguments);
77+
} :
78+
fn,
79+
data,
80+
_fnCtx,
81+
fnId);
82+
83+
bindDomNodes.forEach(function(domNode) {
84+
domNode.addEventListener(event, handler, false);
85+
});
86+
}
87+
}.bind(this),
88+
handler;
8289

83-
bindDomNodes.forEach(function(domNode) {
84-
domNode.addEventListener(event, handler, false);
85-
});
86-
}
90+
typeof e === 'string'?
91+
e.split(' ').forEach(onEvent, this) :
92+
onEvent(e);
8793

8894
return this;
8995
},
@@ -115,26 +121,32 @@ var undef,
115121
var argsLen = arguments.length;
116122
if(argsLen) {
117123
var params = this._params,
118-
event = this._eventBuilder(e, params, 'un');
124+
fnId = identify(fn, _fnCtx),
125+
bindDomNodes = params.bindDomNodes,
126+
bindClassName = params.bindClassName,
127+
onEvent = function(eventName) {
128+
var event = this._eventBuilder(eventName, params, 'un');
119129

120-
if(argsLen === 1) {
121-
this._unbindByEvent(this._storage[event], event);
122-
} else {
123-
var wrappedFn,
124-
fnId = identify(fn, _fnCtx),
125-
fnStorage = this._storage[event],
126-
bindDomNodes = params.bindDomNodes,
127-
bindClassName = params.bindClassName;
130+
if(argsLen === 1) {
131+
this._unbindByEvent(this._storage[event], event);
132+
} else {
133+
var wrappedFn,
134+
fnStorage = this._storage[event];
128135

129-
if(wrappedFn = fnStorage && fnStorage[fnId])
130-
delete fnStorage[fnId];
136+
if(wrappedFn = fnStorage && fnStorage[fnId])
137+
delete fnStorage[fnId];
131138

132-
var handler = wrappedFn || fn;
139+
var handler = wrappedFn || fn;
133140

134-
bindDomNodes.forEach(function(domNode) {
135-
domNode.removeEventListener(event, handler);
136-
});
137-
}
141+
bindDomNodes.forEach(function(domNode) {
142+
domNode.removeEventListener(event, handler);
143+
});
144+
}
145+
}.bind(this);
146+
147+
typeof e === 'string'?
148+
e.split(' ').forEach(onEvent):
149+
onEvent(e);
138150
} else {
139151
objects.each(this._storage, this._unbindByEvent, this);
140152
}

0 commit comments

Comments
 (0)