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;
};