All files / web/bundles/pimui/js/fetcher attribute-fetcher.js

94.12% Statements 16/17
88.89% Branches 8/9
100% Functions 6/6
94.12% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73    1162x 1162x                   200x 102x   102x   102x 102x   102x               98x                     155x   155x 93x         155x                     1353x             727x   727x        
'use strict';
 
define(['jquery', 'underscore', 'pim/base-fetcher', 'routing'], function($, _, BaseFetcher, Routing) {
  return BaseFetcher.extend({
    identifierPromise: null,
    fetchByTypesPromises: [],
 
    /**
     * Return the identifier attribute
     *
     * @return {Promise}
     */
    getIdentifierAttribute: function() {
      if (null === this.identifierPromise) {
        this.identifierPromise = $.Deferred();
 
        return this.fetchByTypes([this.options.identifier_type]).then(
          function(attributes) {
            Eif (attributes.length > 0) {
              this.identifierPromise.resolve(attributes[0]).promise();
 
              return this.identifierPromise;
            }
 
            return this.identifierPromise.reject().promise();
          }.bind(this)
        );
      }
 
      return this.identifierPromise;
    },
 
    /**
     * Fetch attributes by types
     *
     * @param {Array} attributeTypes
     *
     * @return {Promise}
     */
    fetchByTypes: function(attributeTypes, useCache = true) {
      var cacheKey = attributeTypes.sort().join('');
 
      if (!_.has(this.fetchByTypesPromises, cacheKey) || !useCache) {
        this.fetchByTypesPromises[cacheKey] = this.getJSON(this.options.urls.list, {types: attributeTypes.join(',')})
          .then(_.identity)
          .promise();
      }
 
      return this.fetchByTypesPromises[cacheKey];
    },
 
    /**
     * This method overrides the base method, to send a POST query instead of a GET query, because the request
     * URI can be too long.
     * TODO Should be deleted to set it back to GET.
     *
     * {@inheritdoc}
     */
    getJSON: function(url, parameters) {
      return $.post(Routing.generate(url), parameters, null, 'json');
    },
 
    /**
     * {@inheritdoc}
     */
    clear: function() {
      BaseFetcher.prototype.clear.apply(this, arguments);
 
      this.identifierPromise = null;
    },
  });
});