From 68619c71bffb2229eeaaed2b87e630c779759463 Mon Sep 17 00:00:00 2001 From: Oliver Salzburg Date: Thu, 15 Oct 2015 19:22:29 +0200 Subject: [PATCH] fix(typeahead): dangling event listeners --- src/typeahead/test/typeahead.spec.js | 23 +++++++++++++++++++++++ src/typeahead/typeahead.js | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index a410abba39..79a4af35ef 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -1012,6 +1012,29 @@ describe('typeahead tests', function() { expect(element).toBeOpenWithActive(3, 0); }); }); + + describe('event listeners', function() { + it('should register event listeners when attached to body', function() { + spyOn(window, 'addEventListener'); + spyOn(document.body, 'addEventListener'); + + var element = prepareInputEl('
'); + + expect(window.addEventListener).toHaveBeenCalledWith('resize', jasmine.any(Function), false); + expect(document.body.addEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function), false); + }); + + it('should remove event listeners when attached to body', function() { + spyOn(window, 'removeEventListener'); + spyOn(document.body, 'removeEventListener'); + + var element = prepareInputEl('
'); + element.remove(); + + expect(window.removeEventListener).toHaveBeenCalledWith('resize', jasmine.any(Function)); + expect(document.body.removeEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function)); + }); + }); }); /* Deprecation tests below */ diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 12be26dc59..aa888429df 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -359,6 +359,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position']) if (appendToBody || appendToElementId) { $popup.remove(); } + + if (appendToBody) { + angular.element($window).unbind('resize', fireRecalculating); + $document.find('body').unbind('scroll', fireRecalculating); + } // Prevent jQuery cache memory leak popUpEl.remove(); }); @@ -937,6 +942,11 @@ angular.module('ui.bootstrap.typeahead') if (appendToBody || appendToElementId) { $popup.remove(); } + + if (appendToBody) { + angular.element($window).unbind('resize', fireRecalculating); + $document.find('body').unbind('scroll', fireRecalculating); + } // Prevent jQuery cache memory leak popUpEl.remove(); });