Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(sidenav): clean registry when element is destroyed
Browse files Browse the repository at this point in the history
Element registry is currently kept even after element
is removed from DOM. Appart from the memory leak, it
also disable future components with same handle from
beeing pulled from the registry, for example when users
navigate away/back to a page, DOM is reloaded but new
component does not work anymore (old registry
component is called instead of the new one)

@ajoslin reported in referenced issue that registry
component will be refactored. Until then, this patch
should fix the bug on the component side.

Closes #473,#474
  • Loading branch information
pandaiolo authored and ajoslin committed Nov 6, 2014
1 parent 262570d commit e7a3bd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/components/sidenav/sidenav.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function mdSidenavController($scope, $element, $attrs, $timeout, $mdSidenav, $md

var self = this;

$mdComponentRegistry.register(this, $attrs.componentId);
this.destroy = $mdComponentRegistry.register(this, $attrs.componentId);

this.isOpen = function() {
return !!$scope.isOpen;
Expand Down Expand Up @@ -192,6 +192,8 @@ function mdSidenavDirective($timeout, $animate, $parse, $mdMedia, $mdConstant, $

$mdTheming.inherit(backdrop, element);

element.on('$destroy', sidenavCtrl.destroy);

scope.$watch('isOpen', setOpen);
scope.$watch(function() {
return isLockedOpenParsed(scope.$parent, {
Expand Down
8 changes: 8 additions & 0 deletions src/components/sidenav/sidenav.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ describe('mdSidenav', function() {
expect(el.hasClass('md-closed')).toBe(false);
});

it('should deregister component when element is destroyed', inject(function($mdComponentRegistry) {
var el = setup('component-id="left"');
el.trigger('$destroy');

var instance = $mdComponentRegistry.get('left');
expect(instance).toBe(null);
}));

});

describe('$mdSidenav Service', function() {
Expand Down

0 comments on commit e7a3bd8

Please sign in to comment.