Skip to content

Commit

Permalink
fix(ngTableController): table not reloaded when new NgTableParams bou…
Browse files Browse the repository at this point in the history
…nd to scope
  • Loading branch information
ccrowhurstram committed Aug 2, 2015
1 parent ab9ffdf commit d8cbd77
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/scripts/04-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
$scope.params.$params.page = 1;
}

$scope.$watch('params.$params', function(newParams, oldParams) {
function onParamsChange (newParams, oldParams) {

// We don't want to watch for changes to $params whilst the NgTableParams.reload function is executing
// (ie $loading === true).
Expand Down Expand Up @@ -72,7 +72,7 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
maybeResetPage();
$scope.params.reload();
};
if ($scope.params.settings().filterDelay && !$scope.params['$$paramsFirstTimeLoad']){
if ($scope.params.settings().filterDelay && !$scope.params['$$paramsFirstTimeLoad']) {
delayFilter(applyFilter, $scope.params.settings().filterDelay);
} else {
applyFilter();
Expand All @@ -81,7 +81,12 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
$scope.params.reload();
}

}, true);
}

// watch for changes to $params values (eg when a filter or page changes)
$scope.$watch('params.$params', onParamsChange, true);
// watch for changes to $params reference (eg when a new NgTableParams is bound to the scope)
$scope.$watch('params.$params', onParamsChange, false);

this.compileDirectiveTemplates = function () {
if (!$element.hasClass('ng-table')) {
Expand Down
77 changes: 77 additions & 0 deletions test/tableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,4 +654,81 @@ describe('ng-table', function() {
});
});
});

describe('internals', function(){

var elm,
NgTableParams;

beforeEach(inject(function($compile, _NgTableParams_) {
NgTableParams = _NgTableParams_;
elm = angular.element(
'<table ng-table="tableParams">' +
'<tr ng-repeat="user in $data">' +
'<td title="\'Age\'">{{user.age}}</td>' +
'</tr>' +
'</table>');

$compile(elm)(scope);
scope.$digest();
}));

function getData ($defer, params) {
if (!params.hasOwnProperty('getDataCallCount')){
params.getDataCallCount = 0;
}
params.getDataCallCount++;
$defer.resolve([]);
}

it('should reload when binding a new tableParams to scope', function(){
var tableParams = new NgTableParams({}, { getData: getData });
scope.tableParams = tableParams;
scope.$digest();

expect(tableParams.getDataCallCount).toBe(1);
});

it('should reload when binding a new tableParams to scope multiple times', function(){
var tableParams1 = new NgTableParams({}, { getData: getData });
scope.tableParams = tableParams1;
scope.$digest();

expect(tableParams1.getDataCallCount).toBe(1);

var tableParams2 = new NgTableParams({}, { getData: getData });
scope.tableParams = tableParams2;
scope.$digest();

expect(tableParams2.getDataCallCount).toBe(1);
});

it('should not reload when filter value is assigned the same value', function(){
// given
var tableParams = new NgTableParams({ filter: {age: 10} }, { filterDelay: 0, getData: getData });
scope.tableParams = tableParams;
scope.$digest();
tableParams.getDataCallCount = 0; //reset

// when
tableParams.filter({ age: 10});
scope.$digest();

expect(tableParams.getDataCallCount).toBe(0);
});

it('should reload when filter value changes', function(){
// given
var tableParams = new NgTableParams({ filter: {age: 10} }, { filterDelay: 0, getData: getData });
scope.tableParams = tableParams;
scope.$digest();
tableParams.getDataCallCount = 0; //reset

// when
tableParams.filter({ age: 12});
scope.$digest();

expect(tableParams.getDataCallCount).toBe(1);
});
});
});

0 comments on commit d8cbd77

Please sign in to comment.