1

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.

Community
  • 1
  • 1
Matt Wilde
  • 271
  • 2
  • 18
  • Have you considered using createObject? In the service1.cfc you would have something like and vice versa for the service2.cfc. – snackboy Jan 04 '17 at 14:53
  • we use DI/1 dependency injection, and as of new an instance of service2 can be created in the init of service1. Would createObject still be the better option if I understand this D.I. correctly? – Matt Wilde Jan 04 '17 at 22:02
  • this is why service2 is in variables scope – Matt Wilde Jan 04 '17 at 22:10
  • @snackboy i failed to tag you in the response – Matt Wilde Jan 06 '17 at 21:50
  • 2
    No worries. I think the solution you came up with will work fine. As far as best practice, I will never win any awards for best practice or orthodoxy. But I am curious/interested as to why you would need to call a function dynamically. That seems like it's adding a layer to the code and possibly obfuscating future issues down the line. – snackboy Jan 09 '17 at 13:34
  • @snackboy I found it to make sense in this application: I start out with 10 spreadsheets, all different. I have built a function that will parse and manage these spreadsheets once they are in a basic format. To get them all to that format and ready for this "Handle-All" Function, I have to do some custom functions/processing on each of them. I make separate functions for each type of spreadsheet. My page for uploading documents gives a name to each type of sheet, that name is passed through and used as the name for the function which is doing the custom processing. – Matt Wilde Jan 09 '17 at 19:04

1 Answers1

0

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});
}
Matt Wilde
  • 271
  • 2
  • 18
  • 2
    How about `cfinvoke`? CF2016 supports tags in cfscript syntax. Regardless, this looks like bad design to me. OOP offers cleverer ways to work with "dynamic invocation", e.g. interfaces. – Alex Jan 04 '17 at 02:17
  • You're right, CFinvoke is better than what I had. I updated the answer to show how I would implement it. However, my real problem wasn't with getting the dynamic method to run. The problem was getting a "service1 undefined" when trying to run a method in service1 from method2 while service1 was waiting for a return from service2. It seemed like a thread lock. Does that make sense? I tried creating an instance of service1 by doing 'myObj = new service1();' from service2, but got an 'ORM' error, which relates to another part of our web app. – Matt Wilde Jan 04 '17 at 22:08
  • @Alex forgot to tag you in the response – Matt Wilde Jan 06 '17 at 21:50