From d8160ae8b313ae6cf6bee09371b38b03f164cf2b Mon Sep 17 00:00:00 2001 From: Christoph Altmann Date: Wed, 30 Sep 2015 10:22:22 +0200 Subject: [PATCH 1/3] Return -1 instead of 0 if no callback is passed to unbind method --- asevented.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asevented.js b/asevented.js index 365ea34..6de53c9 100644 --- a/asevented.js +++ b/asevented.js @@ -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); } From 1feecb7ff1cac886d4b7066650cc8c48a1fbb656 Mon Sep 17 00:00:00 2001 From: Christoph Altmann Date: Wed, 30 Sep 2015 10:24:44 +0200 Subject: [PATCH 2/3] Add test for unbinding event without callback --- test/asevented_test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/asevented_test.js b/test/asevented_test.js index 6f4c356..92476a2 100644 --- a/test/asevented_test.js +++ b/test/asevented_test.js @@ -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.'); + }); }); From 0e0850a43a06e8c85921378ddf48f4b13b2e5435 Mon Sep 17 00:00:00 2001 From: Christoph Altmann Date: Wed, 30 Sep 2015 10:26:48 +0200 Subject: [PATCH 3/3] Fix tests by passing callback to unbind --- test/asevented_test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/asevented_test.js b/test/asevented_test.js index 92476a2..eeb5fc5 100644 --- a/test/asevented_test.js +++ b/test/asevented_test.js @@ -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.'); }); @@ -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'); @@ -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');