0

I have a bunch of functions that are loaded via services that need to be executed from a $q.all([funcA(),funcB(),funcC()]). And then I have json file which tells the script which services to process. The problem I have is getting the json value to be saved into an array that that can be passed to the $q.all(doFuncs).then where the values of the array will be processed as functions.

The functions/services, as mentioned, are loaded from an earlier global script, defined as:

function funcA() {
 // do something
}
function funcB() {
 // do something
}
function funcC() {
  // do something
}

The json file which is dynamically loaded from a remote server will allow me to turn on/off different vendors to be processed as needed:

json:

var vendorObj = { 
   "vendorA" : {
      "cName" : "Foo Company",
      "active" : 1,
      "funcName" : "funcA"
   },
   "vendorB" : {
      "cName" : "Bar Company",
      "active" : 0,
      "funcName" : "funcB"
   },
   "vendorC" : {
      "cName" : "FooBar Company",
      "active" : 1,
      "funcName" : "funcC"
   }
}

Looping through the json if "active == 1", then add the function name to an array.

var doFunctions = [] ;
for (var key in vendorObj) {
   if (vendorObj[key].active == 1) {
      doFunctions.push(window[vendorObj[key].funcName]) ;
   }
}

If I process them like in this for loop it works, the added/active functions are executed properly: for (var x=0;xx ; }

However, in $q.all() when manually defined, the functions are passed in an array as '$q.all([funcA(),funcC(),funcG()]).then(function(response) { ... })'

So the problem I am having is how to pass doFunctions so each value is auto-recognized as a function as I am not able to add the () after each array value when simply just stating the array name as in the following:

$q.all(doFunctions).then(function(response) {
   // process `response`
}
rolinger
  • 2,787
  • 1
  • 31
  • 53

0 Answers0