Skip to content

Commit 4054b25

Browse files
committed
Merge pull request #4 from gregwym/master
Refactor to use init and destructor to prevent leak.
2 parents d9d80c4 + d7ab7b2 commit 4054b25

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

angular-slimscroll.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
angular.module('ui.slimscroll', []).directive('slimscroll', function() {
1+
angular.module('ui.slimscroll', []).directive('slimscroll', function () {
2+
'use strict';
3+
24
return {
35
restrict: 'A',
4-
link: function($scope, $elem, $attr) {
6+
link: function ($scope, $elem, $attr) {
7+
var off = [];
58
var option = {};
6-
var refresh = function() {
9+
10+
var refresh = function () {
711
if ($attr.slimscroll) {
812
option = $scope.$eval($attr.slimscroll);
913
} else if ($attr.slimscrollOption) {
@@ -13,19 +17,31 @@ angular.module('ui.slimscroll', []).directive('slimscroll', function() {
1317
$elem.slimScroll(option);
1418
};
1519

16-
refresh();
20+
var init = function () {
21+
refresh();
1722

18-
if ($attr.slimscroll && !option.noWatch) {
19-
$scope.$watchCollection($attr.slimscroll, refresh);
20-
}
23+
if ($attr.slimscroll && !option.noWatch) {
24+
off.push($scope.$watchCollection($attr.slimscroll, refresh));
25+
}
26+
27+
if ($attr.slimscrollWatch) {
28+
off.push($scope.$watchCollection($attr.slimscrollWatch, refresh));
29+
}
2130

22-
if ($attr.slimscrollWatch) {
23-
$scope.$watchCollection($attr.slimscrollWatch, refresh);
24-
}
31+
if ($attr.slimscrollListenTo) {
32+
off.push($scope.$on($attr.slimscrollListenTo, refresh));
33+
}
34+
};
35+
36+
var destructor = function () {
37+
off.forEach(function (unbind) {
38+
unbind();
39+
});
40+
off = null;
41+
};
2542

26-
if ($attr.slimscrollListenTo) {
27-
$scope.on($attr.slimscrollListenTo, refresh);
28-
}
43+
off.push($scope.$on('$destroy', destructor));
44+
init();
2945
}
3046
};
3147
});

0 commit comments

Comments
 (0)