Skip to content

Commit

Permalink
Merge pull request #23 from platogo/fix-unbind-with-undefined-callback
Browse files Browse the repository at this point in the history
Do not remove event listener on unbind without callback
  • Loading branch information
mkuklis committed Oct 1, 2015
2 parents edbf433 + 0e0850a commit fcd9758
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion asevented.js
Expand Up @@ -55,7 +55,7 @@
parts = event.split(/\s+/);
for (i = 0, num = parts.length; i < num; i++) {
if ((eventName = parts[i]) in events !== false) {
index = (fn) ? _indexOf(events[eventName], fn) : 0;
index = (fn) ? _indexOf(events[eventName], fn) : -1;
if (index !== -1) {
events[eventName].splice(index, 1);
}
Expand Down
18 changes: 15 additions & 3 deletions test/asevented_test.js
Expand Up @@ -36,7 +36,7 @@ $(function() {
var callback = function () { obj.counter += 1; };
obj.bind('event', callback);
obj.trigger('event');
obj.unbind('event');
obj.unbind('event', callback);
obj.trigger('event');
equals(obj.counter, 1, 'counter should have only been incremented once.');
});
Expand Down Expand Up @@ -226,7 +226,7 @@ $(function() {
asEvented.call(obj);

obj.bind('load ready whatever', callback);
obj.unbind('ready');
obj.unbind('ready', callback);

obj.trigger('load');
obj.trigger('ready');
Expand All @@ -241,7 +241,7 @@ $(function() {
asEvented.call(obj);

obj.bind('load ready whatever', callback);
obj.unbind('ready load whatever');
obj.unbind('ready load whatever', callback);

obj.trigger('load');
obj.trigger('ready');
Expand All @@ -263,4 +263,16 @@ $(function() {
equals(obj.count, 1, 'obj.count should have been incremented once.');
});

test('unbind without callback', function() {
var obj = { count: 0 };
var callback = function() { obj.count += 1; };
asEvented.call(obj);

obj.bind('whatever', callback);
obj.trigger('whatever');
obj.unbind('whatever', undefined);
obj.trigger('whatever');

equals(obj.count, 2, 'obj.count should have been incremented twice.');
});
});

0 comments on commit fcd9758

Please sign in to comment.