This repository was archived by the owner on May 29, 2019. It is now read-only.
File tree 3 files changed +25
-4
lines changed
3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -12,10 +12,10 @@ angular.module('ui.bootstrap.pagination', ['ui.bootstrap.paging'])
12
12
uibPaging . create ( this , $scope , $attrs ) ;
13
13
14
14
if ( $attrs . maxSize ) {
15
- $scope . $parent . $watch ( $parse ( $attrs . maxSize ) , function ( value ) {
15
+ ctrl . _watchers . push ( $scope . $parent . $watch ( $parse ( $attrs . maxSize ) , function ( value ) {
16
16
maxSize = parseInt ( value , 10 ) ;
17
17
ctrl . render ( ) ;
18
- } ) ;
18
+ } ) ) ;
19
19
}
20
20
21
21
// Create page object used in template
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ angular.module('ui.bootstrap.paging', [])
8
8
create : function ( ctrl , $scope , $attrs ) {
9
9
ctrl . setNumPages = $attrs . numPages ? $parse ( $attrs . numPages ) . assign : angular . noop ;
10
10
ctrl . ngModelCtrl = { $setViewValue : angular . noop } ; // nullModelCtrl
11
+ ctrl . _watchers = [ ] ;
11
12
12
13
ctrl . init = function ( ngModelCtrl , config ) {
13
14
ctrl . ngModelCtrl = ngModelCtrl ;
@@ -18,11 +19,11 @@ angular.module('ui.bootstrap.paging', [])
18
19
} ;
19
20
20
21
if ( $attrs . itemsPerPage ) {
21
- $scope . $parent . $watch ( $parse ( $attrs . itemsPerPage ) , function ( value ) {
22
+ ctrl . _watchers . push ( $scope . $parent . $watch ( $parse ( $attrs . itemsPerPage ) , function ( value ) {
22
23
ctrl . itemsPerPage = parseInt ( value , 10 ) ;
23
24
$scope . totalPages = ctrl . calculateTotalPages ( ) ;
24
25
ctrl . updatePage ( ) ;
25
- } ) ;
26
+ } ) ) ;
26
27
} else {
27
28
ctrl . itemsPerPage = config . itemsPerPage ;
28
29
}
@@ -80,6 +81,12 @@ angular.module('ui.bootstrap.paging', [])
80
81
ctrl . ngModelCtrl . $render ( ) ;
81
82
}
82
83
} ;
84
+
85
+ $scope . $on ( '$destroy' , function ( ) {
86
+ while ( ctrl . _watchers . length ) {
87
+ ctrl . _watchers . shift ( ) ( ) ;
88
+ }
89
+ } ) ;
83
90
}
84
91
} ;
85
92
} ] ) ;
Original file line number Diff line number Diff line change @@ -253,4 +253,18 @@ describe('paging factory', function() {
253
253
expect ( ctrl . ngModelCtrl . $render ) . toHaveBeenCalled ( ) ;
254
254
} ) ;
255
255
} ) ;
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
+ } ) ;
256
270
} ) ;
You can’t perform that action at this time.
0 commit comments