14 July 2006

CFC's - not all that object oriented

I had this cracking idea to store repopulate data in CF flash forms using a cfc and storing it in the session scope, then using a remoting function to broadcast this data so the form fields could data bind to remote service. Didn't quite work.

My cfc basically looked like this:

<cfcomponent>

<cfset this.formData = StructNew() />

<cffunction name="getValues" returntype="remote">
<cfreturn this.formData />
</cffunction>

</cfcomponent>

I instantiated the above cfc in the onSessionStart function:

Session.objFormData = CreateObject("component","formData")

I then used the onRequestStart function to populate the Session based object (as per the previous post):

<cfloop list="#form.fieldnames#" index="item">
<cfset Session.objFormData.formData[item] = Evaluate(item) />
</cfloop>

This works fine and removes the need for an "action" page - just simply copy the form into the session until you need it (a bit wasteful, yes, but really quick code if you're in a hurry!)

You'd think the value returned by "this.formData" would be the data stored in the session instance. Not so... all that was returned was an empty structure! This was essentially the uninstantiated cfc with no data. Doing a cfdump of the session showed that the object and data were still in the session scope, but "this" did not return the values of the object in the session.

1 comment:

JB said...

Hi, try sticking the cfid and cftoken to the url of the action page. Something to do with not remembering which user you are. Seems to work. let me know. JB