From bdd4e982b7fee9811b40b545c21a74711686875c Mon Sep 17 00:00:00 2001 From: Andy Joslin Date: Wed, 17 Jul 2013 11:36:09 -0400 Subject: [PATCH] fix(jqLite): support space-seperated events in off Closes #3256 --- src/jqLite.js | 14 +++++++------ test/jqLiteSpec.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/jqLite.js b/src/jqLite.js index 072557ba0e16..8a8a28e8d2d4 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -207,12 +207,14 @@ function JQLiteOff(element, type, fn) { delete events[type]; }); } else { - if (isUndefined(fn)) { - removeEventListenerFn(element, type, events[type]); - delete events[type]; - } else { - arrayRemove(events[type], fn); - } + forEach(type.split(' '), function(type) { + if (isUndefined(fn)) { + removeEventListenerFn(element, type, events[type]); + delete events[type]; + } else { + arrayRemove(events[type], fn); + } + }); } } diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 92ccc2a80db9..e74c09a54731 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -947,6 +947,31 @@ describe('jqLite', function() { }); + it('should deregister all listeners for types separated by spaces', function() { + var aElem = jqLite(a), + clickSpy = jasmine.createSpy('click'), + mouseoverSpy = jasmine.createSpy('mouseover'); + + aElem.on('click', clickSpy); + aElem.on('mouseover', mouseoverSpy); + + browserTrigger(a, 'click'); + expect(clickSpy).toHaveBeenCalledOnce(); + browserTrigger(a, 'mouseover'); + expect(mouseoverSpy).toHaveBeenCalledOnce(); + + clickSpy.reset(); + mouseoverSpy.reset(); + + aElem.off('click mouseover'); + + browserTrigger(a, 'click'); + expect(clickSpy).not.toHaveBeenCalled(); + browserTrigger(a, 'mouseover'); + expect(mouseoverSpy).not.toHaveBeenCalled(); + }); + + it('should deregister specific listener', function() { var aElem = jqLite(a), clickSpy1 = jasmine.createSpy('click1'), @@ -974,6 +999,32 @@ describe('jqLite', function() { browserTrigger(a, 'click'); expect(clickSpy2).not.toHaveBeenCalled(); }); + + + it('should deregister specific listener for multiple types separated by spaces', function() { + var aElem = jqLite(a), + masterSpy = jasmine.createSpy('master'), + extraSpy = jasmine.createSpy('extra'); + + aElem.on('click', masterSpy); + aElem.on('click', extraSpy); + aElem.on('mouseover', masterSpy); + + browserTrigger(a, 'click'); + browserTrigger(a, 'mouseover'); + expect(masterSpy.callCount).toBe(2); + expect(extraSpy).toHaveBeenCalledOnce(); + + masterSpy.reset(); + extraSpy.reset(); + + aElem.off('click mouseover', masterSpy); + + browserTrigger(a, 'click'); + browserTrigger(a, 'mouseover'); + expect(masterSpy).not.toHaveBeenCalled(); + expect(extraSpy).toHaveBeenCalledOnce(); + }); });