Can $ httpBackend be used in production for abstract data service requests?

I have a data service in my application that is responsible for receiving information for my controllers. This information may come from local storage, a window, or an ajax request. The problem I ran into is the $qpromise answers are not like the answers $http.

    this.getContactDetails = function(data) {
        // The first time this method is called, we expect contact details to be preloaded on the page.
        // We want to read and return that object then remove it from the page so subsequent requests are to the server.
        if(typeof $window.preloadData.contact !== 'undefined') {
            var contactDetails = JSON.parse(JSON.stringify($window.preloadData.contact));
            delete $window.preloadData.contact;
            // Since the method call should always have the same return type, we manually create a deferred object and set the resolution using the $q service.
            var deferred = $q.defer();
            deferred.resolve(contactDetails);
            return deferred.promise;
        }
        var request = requests.contactDetails.get;
        return $http(request);
    };

The service $qdoes a nice job here, but it decides as the object that he defined. I would not expect it to return an answer. I know I $httpBackendcan accomplish this.

$httpBackend.whenGET(request).respond(contactDetails);

MockE2E, , . , , , . , , , $httpBackend, , $http. $httpBackend , url, body headers, $http .

- $http- .

var contactDetails = JSON.parse(JSON.stringify({
    data: $window.preloadData.contact
}));

. / ?

+3
1

$cacheFactory $httpProvider .

:

, $http . , , .

, :

  • {object} info() - , .
  • {{*}} put ({string} key, {*} value) - - .
  • {{*}} get ({string} key) - undefined .
  • {void} remove ({string} key) - - .
  • {void} removeAll() - .
  • {void} destroy() - $cacheFactory.

, localStorage, cookie .., data, AJAX.

+1

All Articles