all files / app/matches/ matchesSelectOptions.service.js

100% Statements 14/14
100% Branches 2/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                                                                         
/**
 * @ngdoc service
 * @name app.matches.matchesSelectOptions
 * @description
 * Provides a list of matches to populate a select list
 */
(function() {
  'use strict';
 
  angular
    .module('app.matches')
    .factory('matchesSelectOptions', matchesFunc);
 
  /** @ngInject */
  function matchesFunc($log, $q, matchesPath, crudResource) {
 
    return getSelectOptions;
 
    /**
     * @ngdoc function
     * @name getSelectOptions
     * @methodOf app.matches.matchesSelectOptions
     * @description
     * Makes a REST API request to retrieve a list of matches.
     * Creates an array of match titles 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 deferred = $q.defer();
      crudResource.getResource(matchesPath).query(
        function (response) {
          var options = [];
          angular.forEach(response, function (value) {
            options.push({title: value.title || '(untitled)', id: value.id});
          });
          deferred.resolve(options);
        },
        function (response) {
          $log.error('data error ' + response.status + " " + response.statusText);
          deferred.reject();
        }
      );
      return deferred.promise;
    }
 
  }
})();