Skip to content

Commit

Permalink
fix: eventMixin.on once bug
Browse files Browse the repository at this point in the history
  • Loading branch information
06wj committed May 18, 2016
1 parent 31834ee commit fcaebfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/event/EventMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,19 @@ var EventMixin = {

var eventListeners = listeners[eventType];
if(eventListeners){
eventListeners = eventListeners.slice(0);
var eventListenersCopy = eventListeners.slice(0);
event = event || new EventObject(eventType, this, detail);
if(event._stopped) return false;

for(var i = 0; i < eventListeners.length; i++){
var el = eventListeners[i];
for(var i = 0; i < eventListenersCopy.length; i++){
var el = eventListenersCopy[i];
el.listener.call(this, event);
if(el.once) eventListeners.splice(i--, 1);
if(el.once) {
var index = eventListeners.indexOf(el);
if(index > -1){
eventListeners.splice(index, 1);
}
}
}

if(eventListeners.length == 0) delete listeners[eventType];
Expand Down
1 change: 1 addition & 0 deletions test/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('event', function(){

it('on once', function(){
var firedNum = 0;
eventTarget.on('hello', function(e){});
eventTarget.on('hello', function(e){
firedNum ++;
}, true);
Expand Down

0 comments on commit fcaebfd

Please sign in to comment.