title: How to dynamically call a method from a different component by using cfscript?
body: <p>I'm looking for the best way to dynamically call a method from a different component in cfscript. Notice that it's concerning a method in a <strong>different</strong> component. So far I've tried 3 different methods, but none of them seem be exactly what I'm looking for:</p>
  
  <p>All cases are written in cfscript inside a component method. Let's say I'm trying to dynamically call the <code>setName(required string name)</code> method in the <em>MyComponent</em> component. All cases have following variables defined:</p>
  
  <pre><code>var myComp = new MyComponent();
  var myMethod = "setName";  
  var args = {"name"="foo"};
  </code></pre>
  
  <ul>
  <li><p>use <code>evaluate()</code> for the job  </p>
  
  <pre><code>evaluate("myComp.#myMethod#(argumentCollection=args)");
  </code></pre>
  
  <p><strong>pros</strong>: is done with very little code<br>
  <strong>cons</strong>: code is not very 'clean' and use of evaluate() seems to have an 'evil' reputation in the online community. I wouldn't want my code to be evil.</p></li>
  <li><p>use a cfml wrapper for <code>&lt;cfinvoke&gt;</code>  </p>
  
  <pre><code>invoke("MyComponent", myMethod, args);
  </code></pre>
  
  <p><strong>pros</strong>: I can use all functionality of <code>cfinvoke</code><br>
  <strong>cons</strong>: It creates a new instance of <code>MyComponent</code> with every invoke.</p></li>
  <li><p>create a <code>dynamicMethod</code> method in MyComponent  </p>
  
  <pre><code>myComp.dynamicMethod(myMethod, args);
  </code></pre>
  
  <p>dynamicMethod of MyComponent:  </p>
  
  <pre><code>public any function dynamicMethod(required string methodName, required struct argumentColl){  
    var cfcMethod = variables[arguments.methodName];  
    return cfcMethod(argumentCollection=arguments.argumentColl);
  }
  </code></pre>
  
  <p><strong>pros</strong>: I can finally call myComp directly. Most comfortable solution so far.<br>
  <strong>cons</strong>: I can now call private methods of MyComponent via dynamicMethod.<br>
  (I've also tried the 'function as variable' solution outside of MyComponent, but then the function looses its working context. e.g. if MyComponent would extend a component, the 'super' scope would no longer refer to the extended component).</p></li>
  </ul>
  
  <p>None of these solutions seem to be perfect, so is there no other way to call a dynamic function from a different controller?<br>
  And if there isn't, which one of these is the best solution?</p>
  
  <p>Any advice is welcome, thanks.</p>