Skip to content

Commit

Permalink
fix(memory_leaks): Ensuring events get unbound when grid is destroyed. (
Browse files Browse the repository at this point in the history
#5913)

Adding a set of destroy listeners that unbinds jquery events when the grid is destroyed.

#4203
  • Loading branch information
mportuga authored and dlgski committed Dec 28, 2016
1 parent ecfaa9b commit da942e9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
26 changes: 22 additions & 4 deletions src/features/edit/js/gridEdit.js
Expand Up @@ -507,7 +507,11 @@
});


$scope.$on( '$destroy', rowWatchDereg );
$scope.$on('$destroy', function destroyEvents() {
rowWatchDereg();
// unbind all jquery events in order to avoid memory leaks
$elm.off();
});

function registerBeginEditEvents() {
$elm.on('dblclick', beginEdit);
Expand Down Expand Up @@ -1042,6 +1046,11 @@

return true;
});

$scope.$on('$destroy', function unbindEvents() {
// unbind all jquery events in order to avoid memory leaks
$elm.off();
});
}
};
}
Expand Down Expand Up @@ -1185,6 +1194,11 @@
}
return true;
});

$scope.$on('$destroy', function unbindEvents() {
// unbind jquery events to prevent memory leaks
$elm.off();
});
}
};
}
Expand Down Expand Up @@ -1277,7 +1291,7 @@
}
};

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

$scope.$on(uiGridEditConstants.events.BEGIN_CELL_EDIT, function () {
$elm[0].focus();
Expand All @@ -1287,11 +1301,15 @@
$scope.$emit(uiGridEditConstants.events.END_CELL_EDIT);
});
});

$scope.$on('$destroy', function unbindEvents() {
// unbind jquery events to prevent memory leaks
$elm.off();
$elm[0].removeEventListener('change', handleFileSelect, false);
});
}
};
}
};
}]);


})();
2 changes: 2 additions & 0 deletions src/features/move-columns/js/column-movable.js
Expand Up @@ -565,6 +565,8 @@
movingElm.css({'width': reducedWidth + 'px'});
}
};

$scope.$on('$destroy', offAllEvents);
}
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/features/selection/js/selection.js
Expand Up @@ -736,6 +736,10 @@
window.setTimeout(function () { evt.target.onselectstart = null; }, 0);
}
}

$scope.$on('$destroy', function unbindEvents() {
$elm.off();
});
}
};
}]);
Expand Down
10 changes: 4 additions & 6 deletions src/js/core/directives/ui-grid-menu.js
Expand Up @@ -196,13 +196,11 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18
angular.element($window).on('resize', applyHideMenu);
}

$scope.$on('$destroy', function () {
angular.element(document).off('click touchstart', applyHideMenu);
});


$scope.$on('$destroy', function() {
$scope.$on('$destroy', function unbindEvents() {
angular.element($window).off('resize', applyHideMenu);
angular.element(document).off('click touchstart', applyHideMenu);
$elm.off('keyup', checkKeyUp);
$elm.off('keydown', checkKeyDown);
});

if (uiGridCtrl) {
Expand Down
4 changes: 3 additions & 1 deletion src/js/core/directives/ui-grid-viewport.js
Expand Up @@ -133,7 +133,9 @@
}
}


$scope.$on('$destroy', function unbindEvents() {
$elm.off();
});
},
controller: ['$scope', function ($scope) {
this.rowStyle = function (index) {
Expand Down
5 changes: 5 additions & 0 deletions src/js/core/services/ui-grid-util.js
Expand Up @@ -1278,6 +1278,11 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
for ( var i = mouseWheeltoBind.length; i; ) {
$elm.on(mouseWheeltoBind[--i], cbs[fn]);
}
$elm.on('$destroy', function unbindEvents() {
for ( var i = mouseWheeltoBind.length; i; ) {
$elm.off(mouseWheeltoBind[--i], cbs[fn]);
}
});
};
s.off.mousewheel = function (elm, fn) {
var $elm = angular.element(elm);
Expand Down

0 comments on commit da942e9

Please sign in to comment.