Question: CFC Undefined with variably defined method name

Question

CFC Undefined with variably defined method name

Answers 1
Added at 2017-01-03 22:01
Tags
Question

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.

Answers
nr: #1 dodano: 2017-01-03 23:01

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});
}
Source Show
◀ Wstecz