1

I have a factory with 4 methods but when i call any of them i always get the dataset returned from the first method called.

Factory code:

app.factory('EnumLookupsSvc', function ($http) {
var factory = {};

var promise;

factory.getInvoiceTypes = function() {
    if (!promise) {
        // $http returns a promise, which has a then function, which also returns a promise
        promise = $http.get('/api/invoice/GetInvoiceTypes').then(function(response) {
            // The then function here is an opportunity to modify the response
            console.log(response);
            // The return value gets picked up by the then in the controller.
            return response.data;
        });
    }
    // Return the promise to the controller
    return promise;
};

    factory.getLtlClasses = function() {
        if (!promise) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get('/api/invoice/GetLtlClasses').then(function(response) {
                // The then function here is an opportunity to modify the response
                console.log(response);
                // The return value gets picked up by the then in the controller.
                return response.data;
            });
        }
        // Return the promise to the controller
        return promise;
    };

    factory.getDirections = function() {
        if (!promise) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get('/api/invoice/GetDirections').then(function(response) {
                // The then function here is an opportunity to modify the response
                console.log(response);
                // The return value gets picked up by the then in the controller.
                return response.data;
            });
        }
        // Return the promise to the controller
        return promise;
    };

    factory.getLineItemTypes = function() {
        if (!promise) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get('/api/invoice/GetLineItemTypes').then(function(response) {
                // The then function here is an opportunity to modify the response
                console.log(response);
                // The return value gets picked up by the then in the controller.
                return response.data;
            });
        }
        // Return the promise to the controller
        return promise;
    };


return factory;
});

Controller Code:

"use strict";

app.controller('InvoiceDetailEditorCtrl', function ($scope, $routeParams, InvoiceSvc, EnumLookupsSvc) {

EnumLookupsSvc.getLtlClasses().then(function (data) {
    $scope.ltlClasses = data;
});

EnumLookupsSvc.getDirections().then(function (data) {
    $scope.directions = data;
});

EnumLookupsSvc.getLineItemTypes().then(function (data) {
    $scope.lineItemTypes = data;
});

EnumLookupsSvc.getInvoiceTypes().then(function (data) {
    $scope.invoiceTypes = data;
});

$scope.invoice = InvoiceSvc.get({ id: $routeParams.invoiceId });



});

Chromes network monitor shows only one request is made to /api/invoice/GetLtlClasses

FIXED! *The fix was to move the var promise into the methods:*

factory.getInvoiceTypes = function () {
    var promise;
    if (!promise) {
        // $http returns a promise, which has a then function, which also returns a promise
        promise = $http.get('/api/invoice/GetInvoiceTypes').then(function(response) {
            // The then function here is an opportunity to modify the response
            console.log(response);
            // The return value gets picked up by the then in the controller.
            return response.data;
        });
    }
    // Return the promise to the controller
    return promise;
};

1 Answer 1

3

This is because you made promise global variable, so the first call set it to some value and for all other calls !promise is false, and they return promise.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.