如何将 cfdiv 绑定到维护状态的 CFC?

发布于 2024-12-08 17:48:25 字数 969 浏览 0 评论 0原文

CFM

<html>

<head>

<title>Test Page</title>

</head>

<body>

    <cfform>
        <cfinput type="text" name="input">
    </cfform>

    <cfscript>
        calc = CreateObject("component", "TestCFC");
    </cfscript>

    <cfdiv bind="cfc:TestCFC.func({input})"></cfdiv>
    <cfdiv bind="cfc:TestCFC.func2()"></cfdiv>

    
</body>
</html>

CFC

<cfcomponent>
    <cfscript>
        this.output = '';
        
        remote function func(input){
            output = input;
            return output;  
        }
        remote function func2(){
            return output & ' Hello World.';    
        }
    </cfscript>
</cfcomponent>

输入:

第一句话:

预期输出

第一句话:

第一句话:你好,世界。

欢迎提供解决方法。

CFM

<html>

<head>

<title>Test Page</title>

</head>

<body>

    <cfform>
        <cfinput type="text" name="input">
    </cfform>

    <cfscript>
        calc = CreateObject("component", "TestCFC");
    </cfscript>

    <cfdiv bind="cfc:TestCFC.func({input})"></cfdiv>
    <cfdiv bind="cfc:TestCFC.func2()"></cfdiv>

    
</body>
</html>

CFC

<cfcomponent>
    <cfscript>
        this.output = '';
        
        remote function func(input){
            output = input;
            return output;  
        }
        remote function func2(){
            return output & ' Hello World.';    
        }
    </cfscript>
</cfcomponent>

Input:

First Words:

Expected output

First Words:

First Words: Hello World.

Workarounds are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2024-12-15 17:48:25

cfdiv 绑定是对 CFC 的单个请求——它们之间不缓存或维护状态。对 VARIABLES 等内部共享范围的访问在请求返回时结束。您必须手动构建/维护此状态。

步骤 1:将 CFC 放在与启用 SessionManagement 的 Application.cfc 相同的目录中:

<cfset this.sessionManagement = true />
<cfset this.sessionTimeout = createTimeSpan(0,20,0,0) />

步骤 2:更改 CFC,以便在每个单独请求之间保留的预期变量驻留在 SESSION 范围内:

<cfcomponent>

    <cfset session.output = '' />

步骤 3:编写函数,以便您读/写这个有状态范围:

    remote function func(input){
        session.output = arguments.input;
        return session.output;
    }

步骤 4:

为您的客户端提供一种跨 DIV 链接“onchange”事件的机制。它可以通过多种方式完成,但必须由您完成(它不是自动的)——最快的起点是参考您之前关于 将动态事件绑定到多个字段

值得注意的是,使用 CFC 包装像 SESSION 这样的作用域绝对应该小心谨慎,尽可能使用保护措施(即 StructKeyExists(SESSION,'output') ),因为不同类型的请求(网络与服务)以不同的方式调用共享范围(或根本不调用),并且您还需要担心超时。

cfdiv bindings are single requests to the CFC--state is not cached or maintained between them. Access to internal shared scopes like VARIABLES ends at the return of the request. You must build/maintain this state manually.

Step 1: Put your CFC in the same directory as an Application.cfc that enables SessionManagement:

<cfset this.sessionManagement = true />
<cfset this.sessionTimeout = createTimeSpan(0,20,0,0) />

Step 2: Change your CFC so that the intended variable that will persist between each individual request resides in the SESSION scope:

<cfcomponent>

    <cfset session.output = '' />

Step 3: Write your functions so that you read/write this stateful scope:

    remote function func(input){
        session.output = arguments.input;
        return session.output;
    }

Step 4:

Come up with a mechanism for your client to chain 'onchange' events across DIVs. It can be done any number of ways, but must be done by you (it's not automatic) -- The quickest starting point would be to refer to your previous question about binding dynamic events to multiple fields.

It's worth it to note that using a CFC to wrap a scope like SESSION should absolutely be done with care, use protections where possible (ie StructKeyExists(SESSION,'output') ), as different types of requests (web vs. service) invoke shared scopes differently (or not at all), and you also have timeouts to worry about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文