Skip to content

Commit da942e9

Browse files
mportugadlgski
authored andcommitted
fix(memory_leaks): Ensuring events get unbound when grid is destroyed. (#5913)
Adding a set of destroy listeners that unbinds jquery events when the grid is destroyed. #4203
1 parent ecfaa9b commit da942e9

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

src/features/edit/js/gridEdit.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,11 @@
507507
});
508508

509509

510-
$scope.$on( '$destroy', rowWatchDereg );
510+
$scope.$on('$destroy', function destroyEvents() {
511+
rowWatchDereg();
512+
// unbind all jquery events in order to avoid memory leaks
513+
$elm.off();
514+
});
511515

512516
function registerBeginEditEvents() {
513517
$elm.on('dblclick', beginEdit);
@@ -1042,6 +1046,11 @@
10421046

10431047
return true;
10441048
});
1049+
1050+
$scope.$on('$destroy', function unbindEvents() {
1051+
// unbind all jquery events in order to avoid memory leaks
1052+
$elm.off();
1053+
});
10451054
}
10461055
};
10471056
}
@@ -1185,6 +1194,11 @@
11851194
}
11861195
return true;
11871196
});
1197+
1198+
$scope.$on('$destroy', function unbindEvents() {
1199+
// unbind jquery events to prevent memory leaks
1200+
$elm.off();
1201+
});
11881202
}
11891203
};
11901204
}
@@ -1277,7 +1291,7 @@
12771291
}
12781292
};
12791293

1280-
$elm[0].addEventListener('change', handleFileSelect, false); // TODO: why the false on the end? Google
1294+
$elm[0].addEventListener('change', handleFileSelect, false);
12811295

12821296
$scope.$on(uiGridEditConstants.events.BEGIN_CELL_EDIT, function () {
12831297
$elm[0].focus();
@@ -1287,11 +1301,15 @@
12871301
$scope.$emit(uiGridEditConstants.events.END_CELL_EDIT);
12881302
});
12891303
});
1304+
1305+
$scope.$on('$destroy', function unbindEvents() {
1306+
// unbind jquery events to prevent memory leaks
1307+
$elm.off();
1308+
$elm[0].removeEventListener('change', handleFileSelect, false);
1309+
});
12901310
}
12911311
};
12921312
}
12931313
};
12941314
}]);
1295-
1296-
12971315
})();

src/features/move-columns/js/column-movable.js

+2
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@
565565
movingElm.css({'width': reducedWidth + 'px'});
566566
}
567567
};
568+
569+
$scope.$on('$destroy', offAllEvents);
568570
}
569571
}
570572
};

src/features/selection/js/selection.js

+4
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,10 @@
736736
window.setTimeout(function () { evt.target.onselectstart = null; }, 0);
737737
}
738738
}
739+
740+
$scope.$on('$destroy', function unbindEvents() {
741+
$elm.off();
742+
});
739743
}
740744
};
741745
}]);

src/js/core/directives/ui-grid-menu.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,11 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18
196196
angular.element($window).on('resize', applyHideMenu);
197197
}
198198

199-
$scope.$on('$destroy', function () {
200-
angular.element(document).off('click touchstart', applyHideMenu);
201-
});
202-
203-
204-
$scope.$on('$destroy', function() {
199+
$scope.$on('$destroy', function unbindEvents() {
205200
angular.element($window).off('resize', applyHideMenu);
201+
angular.element(document).off('click touchstart', applyHideMenu);
202+
$elm.off('keyup', checkKeyUp);
203+
$elm.off('keydown', checkKeyDown);
206204
});
207205

208206
if (uiGridCtrl) {

src/js/core/directives/ui-grid-viewport.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@
133133
}
134134
}
135135

136-
136+
$scope.$on('$destroy', function unbindEvents() {
137+
$elm.off();
138+
});
137139
},
138140
controller: ['$scope', function ($scope) {
139141
this.rowStyle = function (index) {

src/js/core/services/ui-grid-util.js

+5
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,11 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
12781278
for ( var i = mouseWheeltoBind.length; i; ) {
12791279
$elm.on(mouseWheeltoBind[--i], cbs[fn]);
12801280
}
1281+
$elm.on('$destroy', function unbindEvents() {
1282+
for ( var i = mouseWheeltoBind.length; i; ) {
1283+
$elm.off(mouseWheeltoBind[--i], cbs[fn]);
1284+
}
1285+
});
12811286
};
12821287
s.off.mousewheel = function (elm, fn) {
12831288
var $elm = angular.element(elm);

0 commit comments

Comments
 (0)