Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

fix(typeahead): dangling event listeners #4636

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
@@ -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('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-append-to-body="true"></div>');

expect(window.addEventListener).toHaveBeenCalledWith('resize', jasmine.any(Function), false);
expect(document.body.addEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function), false);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty line between specs


it('should remove event listeners when attached to body', function() {
spyOn(window, 'removeEventListener');
spyOn(document.body, 'removeEventListener');

var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-append-to-body="true"></div>');
element.remove();

expect(window.removeEventListener).toHaveBeenCalledWith('resize', jasmine.any(Function));
expect(document.body.removeEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function));
});
});
});

/* Deprecation tests below */
10 changes: 10 additions & 0 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
@@ -359,6 +359,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
if (appendToBody || appendToElementId) {
$popup.remove();
}

if (appendToBody) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add empty line above this line

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

angular.element($window).unbind('resize', fireRecalculating);
$document.find('body').unbind('scroll', fireRecalculating);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add empty line below this line

// Prevent jQuery cache memory leak
popUpEl.remove();
});