CFC Undefined with variably defined method name

207 views Asked by At

After reviewing the following topics:

ColdFusion 9 Dynamic Method Call

Dynamic Method Call

I still have a question regarding an error. I made an illustration to try and make it clear:

undefined cfc image

I am wanting to use a variably named function. (But this is Not the problem I am having, I can run a variably named function)

I think it's coming back as undefined because service2's method is being called from the context of service1, and cannot call a method in service1 because service1 is "locked", awaiting a return value from service2 first.

I would like to keep the methods where they are, unless there is no workaround. Am I understanding the problem correctly?

Using CF16 and Framework1.

1

There are 1 answers

3
Matt Wilde On BEST ANSWER

As I could use a quick solution, I will post something I have come up with, which WORKS, but I am still looking for a "best practice" sort of suggestion.

Service1 now calls a new method in service2, say "chooseFunctionToRun".

function chooseFunctionToRun(funcName,param1,param2){
   var functionToRun = this[funcName];
   return functionToRun(param1,param2);
}

now, whatever functionToRun evaluates to (MethodA for example) can freely call a method from Service1 without Service1 being undefined.

It seems like it's just a "separation of concerns" for the code, and the assets are moved from Service1 to Service2 so Service2 can decide the variable function to be run. Still not sure why it works. any comments welcome.

UPDATE Alex proposed Using CFInvoke, which is cleaner.

function chooseFunctionToRun(funcName,param1,param2){
   invoke("",funcName,{arg1=param1,arg2=param2});
}