Skip to content

Commit

Permalink
feat(NgTableParams): add isDataReloadRequired and hasFilterChanges me…
Browse files Browse the repository at this point in the history
…thods

This commit also adds extra test coverage and some test refactoring
  • Loading branch information
ccrowhurstram committed Aug 2, 2015
1 parent f9777d3 commit 95b0f2b
Show file tree
Hide file tree
Showing 3 changed files with 536 additions and 205 deletions.
40 changes: 38 additions & 2 deletions src/scripts/03-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ app.factory('NgTableParams', ['$q', '$log', 'ngTableDefaults', 'ngTableGetDataBc
}

var self = this,
committedParams,
isCommittedDataset = false,
log = function() {
if (settings.debugMode && $log.debug) {
$log.debug.apply(this, arguments);
Expand Down Expand Up @@ -176,8 +178,12 @@ app.factory('NgTableParams', ['$q', '$log', 'ngTableDefaults', 'ngTableGetDataBc
//auto-set the total from passed in data
newSettings.total = newSettings.data.length;
}
// note: using non-strict equality as want null and undefined to be treated the same
if (newSettings.data && (newSettings.data != settings.data)) {
// note: using != as want null and undefined to be treated the same
if (newSettings.hasOwnProperty('data') && (newSettings.data != settings.data)) {
if (isCommittedDataset){
this.page(1); // reset page as a new dataset has been supplied
}
isCommittedDataset = false;
ngTableEventsChannel.publishDatasetChanged(this, newSettings.data, settings.data);
}

Expand Down Expand Up @@ -410,6 +416,17 @@ app.factory('NgTableParams', ['$q', '$log', 'ngTableDefaults', 'ngTableGetDataBc
return pages;
};

/**
* @ngdoc method
* @name NgTableParams#isDataReloadRequired
* @description Return true when a change to this `NgTableParams` instance should require the reload method
* to be run so as to ensure the data presented to the user reflects the `NgTableParams`
*/
this.isDataReloadRequired = function(){
// note: using != as want to treat null and undefined the same
return !isCommittedDataset || !angular.equals(params, committedParams);
};

/**
* @ngdoc method
* @name NgTableParams#hasFilter
Expand All @@ -431,6 +448,16 @@ app.factory('NgTableParams', ['$q', '$log', 'ngTableDefaults', 'ngTableGetDataBc
return result;
};

/**
* @ngdoc method
* @name NgTableParams#hasFilterChanges
* @description Return true when a change to `NgTableParams.filters`require the reload method
* to be run so as to ensure the data presented to the user reflects these filters
*/
this.hasFilterChanges = function(){
return !angular.equals((params && params.filter), (committedParams && committedParams.filter));
};

/**
* @ngdoc method
* @name NgTableParams#url
Expand Down Expand Up @@ -479,6 +506,10 @@ app.factory('NgTableParams', ['$q', '$log', 'ngTableDefaults', 'ngTableGetDataBc
pData = null;

settings.$loading = true;

committedParams = angular.copy(params);
isCommittedDataset = true;

if (settings.groupBy) {
pData = runGetGroups();
} else {
Expand All @@ -503,6 +534,11 @@ app.factory('NgTableParams', ['$q', '$log', 'ngTableDefaults', 'ngTableGetDataBc
}

return data;
}).catch(function(reason){
committedParams = null;
isCommittedDataset = false;
// "rethrow"
return $q.reject(reason);
});
};

Expand Down

0 comments on commit 95b0f2b

Please sign in to comment.