ng-table icon indicating copy to clipboard operation
ng-table copied to clipboard

Make ngTableParams as class or factory method

Open maxaon opened this issue 12 years ago • 1 comments

Currently ngTableParams shres two design patterns: prototype and builder. As well, ngTableParams has method duplication getData and getGroups in ngTableParams and in ngTableParams.settings wich is confusing Possible solutions are:

  1. Use as classes: 1.1 Short version
var params = new NgTableParams({page: 1, count: 10},{total:120});
params.getData=function() {....}

1.2 Long version with proper inheritahce. Can be used to make custom classes or in global override of NgTableParams 2. Hide inheritance and use as builder (factory):

ngTableParams.create({page: 1, count: 10}, {total:120},{getData:function(){...})

Factory method will hide complexity of iheritance from end user. And alsou inheritance can be skipped. 3. Use getData and getGroups as callbacks and do not insert into ngTableParams. So getData and getGroups will be in settings, not in object. Default settings will look like this:

function defaultGetData(params) {
  if (angular.isArray(this.data) && angular.isObject(params)) {
    return this.data.slice((params.page() - 1) * params.count(), params.page() * params.count());
  } else {
    return [];
  }
}
var settings = {
  $scope: null, // by the way, this is bad idea to expose scope
  $loading: false,
  data: null,
  total: 0,
  defaultSort: 'desc',
  filterDelay: 750,
  counts: [10, 25, 50, 100],
  getGroups: defaultGetGroups,
  getData: defaultGetData
};

And getData will be like this:

this.getData = function (params) {
  return $q.when(this.settings.getData.call(this, params));
};

Default settings also can be transferred into provider/constant to change default setting globally in app.

maxaon avatar Jul 19 '14 13:07 maxaon

Agree this would be beneficial. I have a factory which handles bootstrapping all my report interfaces (tables and graphs) and passing an instance of ngtable out of this factory into my controller is pretty inelegant at the moment.

minirobotdan avatar Feb 03 '15 10:02 minirobotdan