Skip to content

Clean up grid on destruction #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/classes/eventProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,28 @@
}
// resize on window resize
var windowThrottle;
$(window).resize(function(){
var windowResize = function(){
clearTimeout(windowThrottle);
windowThrottle = setTimeout(function() {
//in function for IE8 compatibility
domUtilityService.RebuildGrid($scope,grid);
}, 100);
});
};
$(window).on('resize.nggrid', windowResize);
// resize on parent resize as well.
var parentThrottle;
$(grid.$root.parent()).on('resize', function() {
var parentResize = function() {
clearTimeout(parentThrottle);
parentThrottle = setTimeout(function() {
//in function for IE8 compatibility
domUtilityService.RebuildGrid($scope,grid);
}, 100);
};
$(grid.$root.parent()).on('resize.nggrid', parentResize);

$scope.$on('$destroy', function(){
$(window).off('resize.nggrid', windowResize);
$(grid.$root.parent()).off('resize.nggrid', parentResize);
});
};
// In this example we want to assign grid events.
Expand Down
31 changes: 24 additions & 7 deletions src/directives/ng-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@
options.gridDim = new ngDimension({ outerHeight: $($element).height(), outerWidth: $($element).width() });

var grid = new ngGrid($scope, options, sortService, domUtilityService, $filter, $templateCache, $utils, $timeout, $parse, $http, $q);

// Set up cleanup now in case something fails
$scope.$on('$destroy', function cleanOptions() {
delete options.gridDim;
delete options.selectRow;
delete options.selectItem;
delete options.selectAll;
delete options.selectVisible;
delete options.groupBy;
delete options.sortBy;
delete options.gridId;
delete options.ngGrid;
delete options.$gridScope;
delete options.$gridServices;
// Plugins should already have been killed as they are children of $scope
});

return grid.init().then(function() {
// if columndefs are a string of a property ont he scope watch for changes and rebuild columns.
if (typeof options.columnDefs === "string") {
$scope.$parent.$watch(options.columnDefs, function (a) {
$scope.$on('$destroy', $scope.$parent.$watch(options.columnDefs, function (a) {
if (!a) {
grid.refreshDomSizes();
grid.buildColumns();
Expand All @@ -25,15 +42,15 @@
grid.buildColumns();
grid.eventProvider.assignEvents();
domUtilityService.RebuildGrid($scope, grid);
}, true);
}, true));
}
else {
grid.buildColumns();
}

// Watch totalServerItems if it's a string
if (typeof options.totalServerItems === "string") {
$scope.$parent.$watch(options.totalServerItems, function (newTotal, oldTotal) {
$scope.$on('$destroy', $scope.$parent.$watch(options.totalServerItems, function (newTotal, oldTotal) {
// If the newTotal is not defined (like during init, set the value to 0)
if (!angular.isDefined(newTotal)) {
$scope.totalServerItems = 0;
Expand All @@ -42,7 +59,7 @@
else {
$scope.totalServerItems = newTotal;
}
});
}));
}
// If it's NOT a string, then just set totalServerItems to 0 since they should only be setting this if using a string
else {
Expand Down Expand Up @@ -71,10 +88,10 @@
}
$scope.$emit("ngGridEventData", grid.gridId);
};
$scope.$parent.$watch(options.data, dataWatcher);
$scope.$parent.$watch(options.data + '.length', function() {
$scope.$on('$destroy', $scope.$parent.$watch(options.data, dataWatcher));
$scope.$on('$destroy', $scope.$parent.$watch(options.data + '.length', function() {
dataWatcher($scope.$eval(options.data));
});
}));
}

grid.footerController = new ngFooter($scope, grid);
Expand Down