I am currently working on a "pre made" project, so I am trying not to change too much of it.
I have a promise function that calls an API and get an array of objects, when creating a map, but it takes too much and it is called everytime a map needs to be created:
self.getStoreMapById = function(mapsId) {
var deferred = $q.defer();
$http.get("/api/stores/"+mapsId+'/map')
.then(function (response) {
deferred.resolve(response);
});
return deferred.promise;
}
(This is how it gets called:
Stores.getStoreMapById(CONFIG.map.id).then(function(mapResp){
So, to avoid waiting a lot of time, I am calling that function once, when the app begins and save the data in 3 different global variables
if(maplevel1 == undefined ){
Stores.getStoreMapById(24).then(function(mapResp){
maplevel1 = mapResp.data;
});
} //same for maplevel2 and 3
now, what I want to do is, instead of using this Stores.getStoreMapById to call it everytime I need to create a map, I want to have a function that checks the id and assing the data to another var:
if(mapsId == "24"){
data = maplevel1;
}
if(mapsId == "23"){
data = maplevel2;
}
if(mapsId == "21"){
data = maplevel3;
}
Is there a way to write that in a promise function so I can call:
assingMap(CONFIG.map.id).then(function(mapResp){
and keep the rest of the code unchanged?
Thanks!