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

Commit 32b88f1

Browse files
committed
fix(paging): garbage collect parent $watchers
- Garbage collect parent $watchers on $destroy Closes #5276
1 parent c4171a2 commit 32b88f1

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/pagination/pagination.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ angular.module('ui.bootstrap.pagination', ['ui.bootstrap.paging'])
1212
uibPaging.create(this, $scope, $attrs);
1313

1414
if ($attrs.maxSize) {
15-
$scope.$parent.$watch($parse($attrs.maxSize), function(value) {
15+
ctrl._watchers.push($scope.$parent.$watch($parse($attrs.maxSize), function(value) {
1616
maxSize = parseInt(value, 10);
1717
ctrl.render();
18-
});
18+
}));
1919
}
2020

2121
// Create page object used in template

src/paging/paging.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ angular.module('ui.bootstrap.paging', [])
88
create: function(ctrl, $scope, $attrs) {
99
ctrl.setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop;
1010
ctrl.ngModelCtrl = { $setViewValue: angular.noop }; // nullModelCtrl
11+
ctrl._watchers = [];
1112

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

2021
if ($attrs.itemsPerPage) {
21-
$scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
22+
ctrl._watchers.push($scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
2223
ctrl.itemsPerPage = parseInt(value, 10);
2324
$scope.totalPages = ctrl.calculateTotalPages();
2425
ctrl.updatePage();
25-
});
26+
}));
2627
} else {
2728
ctrl.itemsPerPage = config.itemsPerPage;
2829
}
@@ -80,6 +81,12 @@ angular.module('ui.bootstrap.paging', [])
8081
ctrl.ngModelCtrl.$render();
8182
}
8283
};
84+
85+
$scope.$on('$destroy', function() {
86+
while (ctrl._watchers.length) {
87+
ctrl._watchers.shift()();
88+
}
89+
});
8390
}
8491
};
8592
}]);

src/paging/test/paging.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,18 @@ describe('paging factory', function() {
253253
expect(ctrl.ngModelCtrl.$render).toHaveBeenCalled();
254254
});
255255
});
256+
257+
describe('gc', function() {
258+
it('should clear watchers', function() {
259+
var watcher1 = jasmine.createSpy('watcher1'),
260+
watcher2 = jasmine.createSpy('watcher2');
261+
ctrl._watchers = [watcher1, watcher2];
262+
263+
$scope.$destroy();
264+
265+
expect(ctrl._watchers.length).toBe(0);
266+
expect(watcher1).toHaveBeenCalled();
267+
expect(watcher2).toHaveBeenCalled();
268+
});
269+
});
256270
});

0 commit comments

Comments
 (0)