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

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

问题描述

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

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:

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

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"};

  • 对工作使用 evaluate()

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

    优点:只需很少的代码即可完成
    缺点:代码不是很干净",并且evaluate() 的使用似乎在在线社区中享有邪恶"的名声.我不希望我的代码是邪恶的.

    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.

    <cfinvoke> 使用 cfml 包装器

    use a cfml wrapper for <cfinvoke>

    invoke("MyComponent", myMethod, args);
    

    优点:我可以使用cfinvoke的所有功能
    缺点:每次调用都会创建一个新的 MyComponent 实例.

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

    在MyComponent中创建一个dynamicMethod方法

    create a dynamicMethod method in MyComponent

    myComp.dynamicMethod(myMethod, args);
    

    MyComponent 的动态方法:

    dynamicMethod of MyComponent:

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

    优点:我终于可以直接调用 myComp 了.迄今为止最舒适的解决方案.
    缺点:我现在可以通过 dynamicMethod 调用 MyComponent 的私有方法.
    (我还在 MyComponent 之外尝试了作为变量的函数"解决方案,但随后该函数失去了其工作上下文.例如,如果 MyComponent 将扩展组件,则超级"范围将不再引用扩展组件).

    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?

    欢迎任何建议,谢谢.

    推荐答案

    很好的分析.

    您可以在这里做的一件事是使用您的包装函数更接近地模拟 <cfinvoke>.<cfinvoke> 将采用组件路径 COMPONENT 属性中的组件实例(即:对象).所以你的骗局"是每次调用都会创建一个 MyComponent 的新实例".不是真的有效.

    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 添加了 invoke() 函数来实现这一点.我注意到你在 CF9 上,所以这对你没有帮助.但这可能与可能涉及此问题的其他人有关.

    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天全站免登陆