代码之家  ›  专栏  ›  技术社区  ›  Regular Jo

将应用程序的用户定义函数包含到模块中

  •  2
  • Regular Jo  · 技术社区  · 8 年前

    我为简单目的提出的申请

    • 应用
      • 包括 functions.cfm
      • 缓存对象 application.o
        • cfcomponent 每个模块都具有创建/更新/查看模型类型
          • 每个补偿包括 函数.cfm 这样它就可以使用它们,从而使每个模块都包含它们不使用的UDF。
          • 一个例子是
            • application.o.items = new cfcs.items()
              • 包括 函数.cfm
            • application.o.categories = new cfcs.categories()
              • 包括 函数.cfm

    结果是,10个模块中的每一个都复制了 函数.cfm 进入每个模块。

    我重新设计了,将函数转换成一个组件(它被命名为 application._ ,复制到 variarables._ 在每个模块内部调用 _.myUDF() 应该打哪个电话 应用程序。_ 参考 而不是为每个组件重新创建文件中的每个函数。

    这应该是最快最轻的方法,不是吗?有人能推荐这种样式的增强功能吗,或者给出原因 include 可能更可取,如果我更改了向组件添加函数,就不需要重新创建组件了。 functions.cfc ?

    3 回复  |  直到 8 年前
        1
  •  4
  •   Adrian J. Moreno    8 年前

    functions.cfm functionA()

    functions.cfc functions

    <cfset functions.functionA()>

    extend

    <cfcomponent extends="cfc.functions">

    component.cfc <CF_ROOT>\WEB-INF\cftags

        2
  •  2
  •   Alex    8 年前

    functions.cfm functions.cfc

    component {
    
        function doSomething()    { ... }
        function doAnotherThing() { ... }
        ...
    
    }
    

    // create an instance of your "functions" component.
    helperFunctions          = new functions();
    
    // pass this instance to the others components using the constructor
    // (components are passed by reference, so make sure your "functions" component is stateless)
    application.o.items      = new cfcs.items(helperFunctions);
    application.o.categories = new cfcs.categories(helperFunctions);
    

    items.cfc categories.cfc

    component {
    
        function init(functionsInstance) {
    
            // keep the reference to the "functions" component
            variables._ = arguments.functionsInstance;
        }
    
        function someFunction() {
    
            // use the passed "functions" component's functions
            variables._.doSomething();
            variables._.doAnotherThing();
        }
    
        ...
    
    }
    

    application.o

        3
  •  1
  •   Matthew Moore    8 年前

    include application scope

    functions.cfm CFCs

    application.functions variables.functions

    推荐文章