Skip to content

Commit

Permalink
* Fixing removeEvent and fireEvent to behave like dom events. See htt…
Browse files Browse the repository at this point in the history
…p://mooshell.net/9CX5f/ for more information. This also removes the *ugly* (sorry Scott) way to use unshift for Array.include and makes it possible to use eventsOf everywhere again!
  • Loading branch information
cpojer committed Sep 4, 2009
1 parent 13ccb06 commit 7d8bdb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions Source/Class/Mixins.js
Expand Up @@ -23,16 +23,17 @@ var eventsOf = function(object, type){
};

var removeEventsOfType = function(object, type){
var events = eventsOf(object, type);
for (var i = events.length; i--; ) object.removeEvent(type, events[i]);
eventsOf(object, type).each(function(fn){
object.removeEvent(type, fn);
});
};

this.Events = new Class({

$events: {},

addEvent: function(type, fn){
eventsOf(this, type).include(fn, true);
eventsOf(this, type).include(fn);
return this;
},

Expand All @@ -43,8 +44,9 @@ this.Events = new Class({

fireEvent: function(type, args){
args = Array.from(args);
var events = eventsOf(this, type);
for (var i = events.length; i--; ) events[i].apply(this, args);
eventsOf(this, type).each(function(fn){
fn.apply(this, args);
}, this);
return this;
},

Expand All @@ -54,7 +56,12 @@ this.Events = new Class({
}.overload(Function.overloadList),

removeEvent: function(type, fn){
if (!fn.$protected) eventsOf(this, type).erase(fn);
if (!fn.$protected){
var events = eventsOf(this, type);
var index = events.indexOf(fn);
if (index != -1) delete events[index];
}

return this;
},

Expand Down
4 changes: 2 additions & 2 deletions Source/Types/Array.js
Expand Up @@ -80,8 +80,8 @@ Array.implement({
return (this.length) ? this[Number.random(0, this.length - 1)] : null;
},

include: function(item, unshift){
if (!this.contains(item)) this[unshift ? 'unshift' : 'push'](item);
include: function(item){
if (!this.contains(item)) this.push(item);
return this;
},

Expand Down

0 comments on commit 7d8bdb7

Please sign in to comment.