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

Commit

Permalink
fix(paging): garbage collect parent $watchers
Browse files Browse the repository at this point in the history
- Garbage collect parent $watchers on $destroy

Closes #5276
  • Loading branch information
wesleycho committed Jan 16, 2016
1 parent c4171a2 commit 32b88f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/pagination/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ angular.module('ui.bootstrap.pagination', ['ui.bootstrap.paging'])
uibPaging.create(this, $scope, $attrs);

if ($attrs.maxSize) {
$scope.$parent.$watch($parse($attrs.maxSize), function(value) {
ctrl._watchers.push($scope.$parent.$watch($parse($attrs.maxSize), function(value) {
maxSize = parseInt(value, 10);
ctrl.render();
});
}));
}

// Create page object used in template
Expand Down
11 changes: 9 additions & 2 deletions src/paging/paging.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ angular.module('ui.bootstrap.paging', [])
create: function(ctrl, $scope, $attrs) {
ctrl.setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop;
ctrl.ngModelCtrl = { $setViewValue: angular.noop }; // nullModelCtrl
ctrl._watchers = [];

ctrl.init = function(ngModelCtrl, config) {
ctrl.ngModelCtrl = ngModelCtrl;
Expand All @@ -18,11 +19,11 @@ angular.module('ui.bootstrap.paging', [])
};

if ($attrs.itemsPerPage) {
$scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
ctrl._watchers.push($scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
ctrl.itemsPerPage = parseInt(value, 10);
$scope.totalPages = ctrl.calculateTotalPages();
ctrl.updatePage();
});
}));
} else {
ctrl.itemsPerPage = config.itemsPerPage;
}
Expand Down Expand Up @@ -80,6 +81,12 @@ angular.module('ui.bootstrap.paging', [])
ctrl.ngModelCtrl.$render();
}
};

$scope.$on('$destroy', function() {
while (ctrl._watchers.length) {
ctrl._watchers.shift()();
}
});
}
};
}]);
14 changes: 14 additions & 0 deletions src/paging/test/paging.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,18 @@ describe('paging factory', function() {
expect(ctrl.ngModelCtrl.$render).toHaveBeenCalled();
});
});

describe('gc', function() {
it('should clear watchers', function() {
var watcher1 = jasmine.createSpy('watcher1'),
watcher2 = jasmine.createSpy('watcher2');
ctrl._watchers = [watcher1, watcher2];

$scope.$destroy();

expect(ctrl._watchers.length).toBe(0);
expect(watcher1).toHaveBeenCalled();
expect(watcher2).toHaveBeenCalled();
});
});
});

0 comments on commit 32b88f1

Please sign in to comment.