How can I bind cfdiv to a CFC that maintains state?
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: Hel开发者_StackOverflowlo World.
Workarounds are welcome.
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.
精彩评论