Skip to content

Commit

Permalink
chore(ngTableParams): remove backward compatibility shims
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

1) `ngTableAfterReloadData $scope` event removed

Eventing no longer makes *direct* calls to `$scope.$emit`. Instead a strongly typed pub/sub service
(`ngTableEventsChannel`) is used.

**To migrate**

*Previously:*

```js
    $scope.$on('ngTableAfterReloadData', yourHandler)
```

*Now:*

```js
    ngTableEventsChannel.onAfterReloadData(yourHandler, $scope)
```

2) `$scope` removed from `NgTableParams`

Because of 1. above, `NgTableParams` no longer requires a reference to `$scope`.

A reference to `$scope` was largely an internal requirement so there should be no code change
required on your part.

3) `getData` signature change

The `$defer` paramater supplied to your `getData` method has been removed. Instead your
`getData` method should return an array or a promise that resolves to an array.

**To migrate**

*Previously:*

```js
    var tp = new NgTableParams({}, { getData: getData });

    function getData($defer, params){
        // snip
        $defer.resolve(yourDataArray);
    }
```

*Now:*

```js
    var tp = new NgTableParams({}, { getData: getData });

    function getData(params){
        // snip
        return yourDataArrayOrPromise;
    }
```

4) `ngTableParams` renamed to `NgTableParams`

**To migrate**

*Previously:*

```js
    var tp = new ngTableParams();
```

*Now:*

```js
    var tp = new NgTableParams();
```
  • Loading branch information
christianacca committed Jun 17, 2016
1 parent da06936 commit 9b81066
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 187 deletions.
1 change: 0 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ module.exports = function(grunt) {
'src/scripts/ngTableEventsChannel.js',
'src/scripts/ngTableFilterConfig.js',
'src/scripts/ngTableDefaultGetData.js',
'src/scripts/ngTableGetDataBcShim.js',
'src/scripts/ngTableColumn.js',
'src/scripts/ngTableParams.js',
'src/scripts/ngTableController.js',
Expand Down
36 changes: 0 additions & 36 deletions src/scripts/ngTableGetDataBcShim.js

This file was deleted.

51 changes: 6 additions & 45 deletions src/scripts/ngTableParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @description Parameters manager for ngTable
*/

angular.module('ngTable').factory('NgTableParams', ['$q', '$log', '$filter', 'ngTableDefaults', 'ngTableGetDataBcShim', 'ngTableDefaultGetData', 'ngTableEventsChannel', function($q, $log, $filter, ngTableDefaults, ngTableGetDataBcShim, ngTableDefaultGetData, ngTableEventsChannel) {
angular.module('ngTable').factory('NgTableParams', ['$q', '$log', '$filter', 'ngTableDefaults', 'ngTableDefaultGetData', 'ngTableEventsChannel', function($q, $log, $filter, ngTableDefaults, ngTableDefaultGetData, ngTableEventsChannel) {
var isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
Expand Down Expand Up @@ -144,16 +144,6 @@
newSettings.total = newSettings.dataset.length;
}

// todo: remove the backwards compatibility shim and the following two if blocks
if (newSettings.getData && newSettings.getData.length > 1){
// support the old getData($defer, params) api
newSettings.getDataFnAdaptor = ngTableGetDataBcShim;
}
if (newSettings.getGroups && newSettings.getGroups.length > 2){
// support the old getGroups($defer, params) api
newSettings.getGroupsFnAdaptor = ngTableGetDataBcShim;
}

var originalDataset = settings.dataset;
settings = angular.extend(settings, newSettings);

Expand Down Expand Up @@ -558,9 +548,9 @@
isCommittedDataset = true;

if (self.hasGroup()) {
pData = runInterceptorPipeline(runGetGroups);
pData = runInterceptorPipeline($q.when(settings.getGroups(self)));
} else {
pData = runInterceptorPipeline(runGetData);
pData = runInterceptorPipeline($q.when(settings.getData(self)));
}

log('ngTable: reload data');
Expand All @@ -576,11 +566,6 @@
ngTableEventsChannel.publishAfterReloadData(self, data, oldData);
self.reloadPages();

// todo: remove after acceptable depreciation period
if (settings.$scope) {
settings.$scope.$emit('ngTableAfterReloadData');
}

return data;
}).catch(function(reason){
errParamsMemento = prevParamsMemento;
Expand Down Expand Up @@ -623,17 +608,7 @@
}
})();

function runGetData(){
var getDataFn = settings.getDataFnAdaptor(settings.getData);
return $q.when(getDataFn.call(settings, self));
}

function runGetGroups(){
var getGroupsFn = settings.getGroupsFnAdaptor(settings.getGroups);
return $q.when(getGroupsFn.call(settings, self));
}

function runInterceptorPipeline(fetchFn){
function runInterceptorPipeline(fetchedData){
var interceptors = settings.interceptors || [];

return interceptors.reduce(function(result, interceptor){
Expand All @@ -644,14 +619,12 @@
}, function(reason){
return rejectFn(reason, self);
});
}, fetchFn());
}, fetchedData);
}

function getDefaultSettingFns(){

return {
getDataFnAdaptor: angular.identity,
getGroupsFnAdaptor: angular.identity,
getData: getData,
getGroups: getGroups
};
Expand Down Expand Up @@ -699,8 +672,7 @@
var settings = params.settings();
var originalDataOptions = settings.dataOptions;
settings.dataOptions = { applyPaging: false };
var adaptedFn = settings.getDataFnAdaptor(settings.getData);
var gotData = $q.when(adaptedFn.call(settings, params));
var gotData = $q.when(settings.getData(params));
return gotData.then(function(data) {
var groups = {};
angular.forEach(data, function(item) {
Expand Down Expand Up @@ -768,8 +740,6 @@
* @description configuration settings for `NgTableParams`
*/
var settings = {
// todo: remove $scope after acceptable depreciation period as no longer required
$scope: null, // set by ngTable controller
$loading: false,
dataset: null, //allows data to be set when table is initialized
total: 0,
Expand Down Expand Up @@ -800,15 +770,6 @@
};
return NgTableParams;
}]);

/**
* @ngdoc service
* @name ngTableParams
* @description Backwards compatible shim for lowercase 'n' in NgTableParams
*/
angular.module('ngTable').factory('ngTableParams', ['NgTableParams', function(NgTableParams) {
return NgTableParams;
}]);
})();


105 changes: 0 additions & 105 deletions test/tableParamsSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1263,111 +1263,6 @@ describe('NgTableParams', () => {
}));
});

describe('backwards compatibility shim', () => {

it('shim should supply getData original arguments', inject((ngTableGetDataBcShim: NgTable.Data.IGetDataBcShimFunc<any>) => {
// given
var callCount = 0;
var adaptedFn = ngTableGetDataBcShim(originalGetDataFn);

// when
var tableParams = new NgTableParams({}, {});
adaptedFn(tableParams);

// then
expect(callCount).toBe(1);

function originalGetDataFn($defer: ng.IDeferred<{}>, params: NgTableParams<{}>){
callCount++;
expect($defer).toBeDefined();
expect($defer.resolve).toBeDefined();
expect(params).toBeDefined();
expect(params).toEqual(jasmine.any(NgTableParams));
}

}));

it('shim should return the getData "$defer" promise', inject((ngTableGetDataBcShim: NgTable.Data.IGetDataBcShimFunc<any>) => {
// given
var adaptedFn = ngTableGetDataBcShim(originalGetDataFn);

// when
var tableParams = new NgTableParams({}, {});
var results = adaptedFn(tableParams);

// then
expect(results).toBeDefined();
expect(results.then).toBeDefined();
results.then(data => {
expect(data.length).toBe(3);
});
scope.$digest();

function originalGetDataFn($defer: ng.IDeferred<{}>/*, params*/){
$defer.resolve([1,2,3]);
}
}));

it('shim should return the data', inject((ngTableGetDataBcShim: NgTable.Data.IGetDataBcShimFunc<number>) => {
// given
var adaptedFn = ngTableGetDataBcShim(originalGetDataFn);

// when
var tableParams = new NgTableParams({}, {});
var results = adaptedFn(tableParams);

// then
expect(results).toBeDefined();
expect(results).toEqual([1,2,3]);

function originalGetDataFn(/*$defer, params*/){
return [1,2,3];
}
}));

// it('shim should be applied when getData function supplied has more than one parameter', () => {
// // given
// var tp = createNgTableParams({ getData: originalGetDataFn});

// // when
// var dataFetched = tp.reload();

// // then
// var actualData: number[];
// dataFetched.then((data: number[]) => {
// actualData = data;
// });
// scope.$digest();
// expect(actualData).toEqual([1,2,3]);

// function originalGetDataFn($defer: ng.IDeferred<number[]>, params: NgTableParams<number>){
// params.total(3);
// $defer.resolve([1,2,3]);
// }
// });

it('shim should NOT be applied when getData has new signature', () => {
// given
var tp = createNgTableParams({ getData: newGetDataFn});

// when
var dataFetched = tp.reload();

// then
var actualData: number[];
dataFetched.then((data: number[]) => {
actualData = data;
});
scope.$digest();
expect(actualData).toEqual([1,2,3]);

function newGetDataFn(params: NgTableParams<number>){
params.total(3);
return [1,2,3];
}
});
});

describe('interceptors', () => {

it('can register interceptor', () => {
Expand Down

0 comments on commit 9b81066

Please sign in to comment.