all files / app/teams/ teamsSelectOptions.service.js

100% Statements 14/14
50% Branches 1/2
100% Functions 6/6
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                       
/**
 * @ngdoc service
 * @name app.teams.teamsSelectOptions
 * @description
 * Provide a list of teams for populating a select list
 *
 */
 
(function () {
  'use strict';
 
  angular
    .module('app.teams')
    .factory('teamsSelectOptions', factory);
 
  /** @ngInject */
  function factory($log, $q, crudResource, teamsPath) {
 
    return getSelectOptions;
 
    /**
     * @ngdoc function
     * @name getSelectOptions
     * @methodOf app.teams.teamsSelectOptions
     * @description
     * Makes a REST API request to retrieve a list of teams.
     * Creates an array of team names from the response.
     *
     * @returns {Object} promise
     * * Resolved with an array when the REST API request succeeds
     * * Rejected when the REST API request fails
     */
    function getSelectOptions() {
      var deferredObject = $q.defer();
      crudResource.getResource(teamsPath).query(
        function (response) {
          var options = [];
          angular.forEach(response, function (value) {
            options.push({
              name: value.name || '(unnamed)',
              id: value.id,
              first_player: value.first_player,
              second_player: value.second_player
            });
          });
          deferredObject.resolve(options);
        },
        function (response) {
          $log.error('data error ' + response.status + " " + response.statusText);
          deferredObject.reject();
        }
      );
      return deferredObject.promise;
    }
 
  }
})();