Question

I want to be able to return pre-loaded data from an angular service if it's already there and if it's not I want to get the data from a web service.

Obviously one of these operations is synchronous and the other isn't so I want to wrap the simple return in an asynchronous way so that I don't have to have lots of if...else blocks in my controllers.

I've tried using the deferred api as follows.

worksOrdersApp.factory("WorksOrderService", function ($http, $q) {
    var _jobs = [];

    var newWorksOrders = function () {
        var deferredJobs = $q.defer();
        if (_jobs.length === 0) {
            return $http.get('/api/WorksOrders/GetNew').success(function(data) {
                deferredJobs.resolve(data);
            });
        } else {
            deferredJobs.resolve(_jobs);
        }

        return deferredJobs.promise;
    };

    return {
        getNewWorksOrders: newWorksOrders,
    };
});

Then in my controller it should be a simple case of calling...

 WorksOrderService.getNewWorksOrders().then(function (data) {
     $scope.values = data;
 });

This initially seems to work, the value of data in the service is an array as I'd expect. However when I inspect the value of data in the controller after the call to deferredJobs.resolve() it's a http response object with details of the status code and a separate data.data property which contains the array. This still means I've got to have if...else code in the controller to check if data is an array or a http response.

How do I just pass the array back to my controller?

Was it helpful?

Solution

Typical, as soon as I ask the question I spot the answer.

In the if (_jobs.length === 0) block I'm returning the $http.get instead of executing it and allowing the promise to return the result.

This does what I expect.

if (_jobs.length === 0) {
    $http.get('/api/WorksOrders/GetNew').success(function(data) {
        _jobs = data; // I also forgot this line, without it _jobs will always be empty and the else block will never execute
        deferredJobs.resolve(data);
    });
} else {
    deferredJobs.resolve(_jobs);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top