Skip to content

dataService ($routeProvider extension)

Jayson Harshbarger edited this page Nov 18, 2016 · 3 revisions

The projectX.dataService service provides support for loading Data Package files automatically by specifying a datapackage or datapackageUrl property in a route definition (defined using ngRoute $routeProvider). Neither datapackage nor datapackageUrl are required.

datapackageUrl

For example, the following configuration will load the data package at components/examples/datapackage.json

function configRoutes($routeProvider) {

  $routeProvider
  .when('/examples', {
    templateUrl: 'components/examples/index.html',
    controller: 'ExamplesCtrl',
    datapackageUrl: 'components/examples/datapackage.json'
  });

}

angular
  .module('examples', ['projectX.dataService'])
  .config(['$routeProvider', configRoutes]);

The dataPackage is processed using projectX.dataService.loadPackage and passed as a dependency injected into the controller (using $routeProvider route resolve).

datapackage

Similar to template and templateUrl, the datapackage property is a string or a function that returns a datapackage object which is processed using projectX.dataService and passed as a dependency injected into the controller. If the datapackage property is a function, it will be called with the route parameters extracted from the current $location.path() by applying the current route.

For example:

function configRoutes($routeProvider) {

  $routeProvider
  .when('/examples/:id', {
    templateUrl: 'components/examples/index.html',
    controller: 'ExamplesCtrl',
    datapackage: $routeParams => {
      return {
        name: $routeParams.id,
        title: $routeParams.id,
        resources: [`../../data/examples/${$routeParams.id}.json`]
      };
    }
  });

}

angular
  .module('examples', ['projectX.dataService'])
  .config(['$routeProvider', configRoutes]);