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

Commit

Permalink
fix(tooltip): make sure tooltip scope is evicted from cache
Browse files Browse the repository at this point in the history
This fix makes sure the tooltip.$scope is cleared from angular.element.cache when $destroy is called, preventing a memory leak.

Closes #484
  • Loading branch information
Joe Grund authored and pkozlowski-opensource committed Jun 24, 2013
1 parent 3fcb70f commit 9246905
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
40 changes: 40 additions & 0 deletions src/tooltip/test/tooltip.spec.js
Expand Up @@ -238,6 +238,46 @@ describe('tooltip', function() {
}));
});

describe('cleanup', function () {
var elmBody, elm, elmScope, tooltipScope;

function inCache() {
var match = false;

angular.forEach(angular.element.cache, function (item) {
if (item.data && item.data.$scope === tooltipScope) {
match = true;
}
});

return match;
}

beforeEach(inject(function ( $compile, $rootScope ) {
elmBody = angular.element('<div><input tooltip="Hello!" tooltip-trigger="fooTrigger" /></div>');

$compile(elmBody)($rootScope);
$rootScope.$apply();

elm = elmBody.find('input');
elmScope = elm.scope();
tooltipScope = elmScope.$$childTail;
}));

it( 'should not contain a cached reference', function() {
expect( inCache() ).toBeTruthy();
elmScope.$destroy();
expect( inCache() ).toBeFalsy();
});

it( 'should not contain a cached reference when visible', inject( function( $timeout ) {
expect( inCache() ).toBeTruthy();
elm.trigger('fooTrigger');
elmScope.$destroy();
$timeout.flush();
expect( inCache() ).toBeFalsy();
}));
});
});

describe('tooltipWithDifferentSymbols', function() {
Expand Down
9 changes: 5 additions & 4 deletions src/tooltip/tooltip.js
Expand Up @@ -303,12 +303,13 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position' ] )
}
});
}

// if this trigger element is destroyed while the tooltip is open, we
// need to close the tooltip.
scope.$on('$destroy', function closeTooltipOnDestroy () {

// Make sure tooltip is destroyed and removed.
scope.$on('$destroy', function onDestroyTooltip() {
if ( scope.tt_isOpen ) {
hide();
} else {
tooltip.remove();
}
});
}
Expand Down

0 comments on commit 9246905

Please sign in to comment.