如何使用cfscript从不同的组件动态调用方法? [英] How to dynamically call a method from a different component by using cfscript?

查看:112
本文介绍了如何使用cfscript从不同的组件动态调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找从cfscript中的不同组件动态调用方法的最佳方法。请注意,这是关于不同组件中的方法。到目前为止,我已经尝试了3种不同的方法,但是它们都不是我正在寻找的:



所有的情况都是在一个组件方法中的cfscript中写的。假设我试图动态调用 MyComponent 组件中的 setName(required string name)方法。所有案例都定义了以下变量:

  var myComp = new MyComponent 
var myMethod =setName;
var args = {name=foo};




  • use / code> for the job

      evaluate(myComp。#myMethod#(argumentCollection = args) 

    专业人员:使用非常少的代码

    cons :代码不是很干净,使用evaluate()似乎在在线社区有一个邪恶的声誉。


  • < cfinvoke>

      invoke(MyComponent,myMethod,args) 

    专业人员:我可以使用 cfinvoke

    cons :每次调用都会创建一个 MyComponent 的新实例。 p>


  • 在MyComponent中创建 dynamicMethod 方法

      myComp.dynamicMethod(myMethod,args); 

    MyComponent的动态方法:

      public any function dynamicMethod(required string methodName,required struct argumentColl){
    var cfcMethod = variables [arguments.methodName];
    return cfcMethod(argumentCollection = arguments.argumentColl);
    }

    优点:我终于可以直接调用myComp。目前为止最舒适的解决方案。

    cons :我现在可以通过dynamicMethod调用MyComponent的私有方法。

    (我也试过'变量'解决方案在MyComponent之外,但是那个函数失去了它的工作上下文。例如,如果MyComponent扩展一个组件,'super'范围将不再指向扩展组件)。




这些解决方案都不是完美的,所以没有其他方法可以从不同的控制器调用动态函数?

如果没有,哪一个是最好的解决方案?



欢迎任何建议,谢谢。

解决方案

良好的分析。



你可以做的一件事是更紧密地模仿< cfinvoke> ; 与您的包装函数。 < cfinvoke> 将采用组件路径组件实例(即: / code>属性。因此,'每次调用都会创建一个MyComponent的新实例'的'con'不太有效。



ColdFusion 10,btw添加了<​​a href =http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSd8001ae4abdbd911-5c900a4e1350d8b2511-8000.html> invoke() 函数实现这一点。我注意到你在CF9,所以这对你没有帮助。但它可能与其他人谁可能着陆这个问题相关。


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 different component. So far I've tried 3 different methods, but none of them seem be exactly what I'm looking for:

All cases are written in cfscript inside a component method. Let's say I'm trying to dynamically call the setName(required string name) method in the MyComponent component. All cases have following variables defined:

var myComp = new MyComponent();
var myMethod = "setName";  
var args = {"name"="foo"};

  • use evaluate() for the job

    evaluate("myComp.#myMethod#(argumentCollection=args)");
    

    pros: is done with very little code
    cons: 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.

  • use a cfml wrapper for <cfinvoke>

    invoke("MyComponent", myMethod, args);
    

    pros: I can use all functionality of cfinvoke
    cons: It creates a new instance of MyComponent with every invoke.

  • create a dynamicMethod method in MyComponent

    myComp.dynamicMethod(myMethod, args);
    

    dynamicMethod of MyComponent:

    public any function dynamicMethod(required string methodName, required struct argumentColl){  
      var cfcMethod = variables[arguments.methodName];  
      return cfcMethod(argumentCollection=arguments.argumentColl);
    }
    

    pros: I can finally call myComp directly. Most comfortable solution so far.
    cons: I can now call private methods of MyComponent via dynamicMethod.
    (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).

None of these solutions seem to be perfect, so is there no other way to call a dynamic function from a different controller?
And if there isn't, which one of these is the best solution?

Any advice is welcome, thanks.

解决方案

Good analysis.

One thing you could do here is to more-closely emulate <cfinvoke> with your wrapper function. <cfinvoke> will take either a component path or a component instance (ie: an object) in that COMPONENT attribute. So your 'con' of 'It creates a new instance of MyComponent with every invoke.' isn't really valid.

ColdFusion 10, btw, adds a invoke() function to achieve just this. I note you're on CF9, so this is no help to you. But it's perhaps relevant for other people who might land on this question.

这篇关于如何使用cfscript从不同的组件动态调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆