Skip to content

Commit

Permalink
fix(ngTableController): prevent "stackoverflow" exception when data i…
Browse files Browse the repository at this point in the history
…tems are recursive data structures

Watching the entire ngTableParams structure "by value" including it's assigned page of data causes
angular.copy to throw an exception.

This fix reduces that part of the ngTableParams structure that is observed for changes.

The fix should also improve performance where bound objects are large graphs and/or the grid
page size is large
  • Loading branch information
ccrowhurstram committed Jan 27, 2015
1 parent 3113e34 commit 4a344db
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/scripts/04-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,30 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
};
})();

function getParamsWatchState(params) {
if (!params) {
return params;
}

var allKeys = Object.keys(params);
var observedKeys = allKeys.filter(function (key) {
return key !== "data";
});
var state = {};
for (var i = 0; i < observedKeys.length; i++) {
var propName = observedKeys[i];
state[propName] = params[propName];
}
return state;
}

function resetPage() {
$scope.params.$params.page = 1;
}

$scope.$watch('params.$params', function(newParams, oldParams) {
$scope.$watch(function() {
return getParamsWatchState($scope.params.$params)
}, function(newParams, oldParams) {

if (newParams === oldParams) {
return;
Expand Down Expand Up @@ -129,11 +148,14 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng

// note: this we're setting up watches to simulate angular's isolated scope bindings

$scope.$watch(tableParamsExpr, (function (params) {
var tableParamsGetter = $parse(tableParamsExpr);
$scope.$watch(function () {
return getParamsWatchState(tableParamsGetter($scope));
}, (function (params) {
if (angular.isUndefined(params)) {
return;
}
$scope.paramsModel = $parse(tableParamsExpr);
$scope.paramsModel = tableParamsGetter;
$scope.params = params;
}), true);

Expand Down

0 comments on commit 4a344db

Please sign in to comment.