src/js/utils/api.js
/**
   * This method return a $.Deferred will resolve when the request is complete (or cache)
   */
This method return a $.Deferred will resolve when the request is complete (or cache)
This method return a $ . Deferred will resolve when the request is complete ( or cache )
_sendRequest(url, data, method, disableCache, extraHeaders) {
    const cacheKey = url + '[' + (data ? JSON.stringify(data)
      + JSON.stringify(extraHeaders) : '') + ']';
    const extraOptions = JSON.parse(JSON.stringify(this.extraOptions));
    extraOptions.headers = Object.assign({}, extraOptions.headers, extraHeaders);
    const ajax = () => {
      const options = $.extend({}, {
        data: JSON.stringify(data),
      }, extraOptions, {
        url: API_END_POINT + url,
        method,
      });
      this.log('Request sent: ', cacheKey, options);
      return $.ajax(options).then((response) => {
        cache.set(cacheKey, response);
        this.log(`Request cached:${cacheKey}`);
        return response;
      });
    };
    if (this._disableCache || disableCache) {
      this.log(`Request sent without cache:${cacheKey}`);
      return ajax();
    }
    const response = cache.get(cacheKey);
    if (response) {
      this.log('Returning cached request: ' + cacheKey);
      return $.Deferred().resolve(response);
    }
    this.log('Asked for cache but not found, sending request: ' + cacheKey);
    return ajax();
  }
_sendRequest ( url , data , method , disableCache , extraHeaders ) { const cacheKey = url + '[' + ( data ? JSON . stringify ( data ) + JSON . stringify ( extraHeaders ) : '' ) + ']' ; const extraOptions = JSON . parse ( JSON . stringify ( this . extraOptions ) ) ; extraOptions . headers = Object . assign ( { } , extraOptions . headers , extraHeaders ) ; const ajax = ( ) => { const options = $ . extend ( { } , { data : JSON . stringify ( data ) , } , extraOptions , { url : API_END_POINT + url , method , } ) ; this . log ( 'Request sent: ' , cacheKey , options ) ; return $ . ajax ( options ) . then ( ( response ) => { cache . set ( cacheKey , response ) ; this . log ( ` ${ cacheKey } ` ) ; return response ; } ) ; } ; if ( this . _disableCache || disableCache ) { this . log ( ` ${ cacheKey } ` ) ; return ajax ( ) ; } const response = cache . get ( cacheKey ) ; if ( response ) { this . log ( 'Returning cached request: ' + cacheKey ) ; return $ . Deferred ( ) . resolve ( response ) ; } this . log ( 'Asked for cache but not found, sending request: ' + cacheKey ) ; return ajax ( ) ; }
This method return a $.Deferred will resolve when the request is complete (or cache)
This method return a $ . Deferred will resolve when the request is complete ( or cache )
// Use cache   // Try to get data from cache // No cache available