如何将 cfdiv 绑定到维护状态的 CFC?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cfdiv 绑定是对 CFC 的单个请求——它们之间不缓存或维护状态。对 VARIABLES 等内部共享范围的访问在请求返回时结束。您必须手动构建/维护此状态。
步骤 1:将 CFC 放在与启用 SessionManagement 的 Application.cfc 相同的目录中:
步骤 2:更改 CFC,以便在每个单独请求之间保留的预期变量驻留在 SESSION 范围内:
步骤 3:编写函数,以便您读/写这个有状态范围:
步骤 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:
Step 2: Change your CFC so that the intended variable that will persist between each individual request resides in the SESSION scope:
Step 3: Write your functions so that you read/write this stateful scope:
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.