[Top] [All Lists]

Re: CFC instantiation

Subject: Re: CFC instantiation
From: GArlington
Date: Wed, 9 Apr 2008 01:48:06 -0700 PDT
Newsgroups: macromedia.coldfusion.advanced_techniques
On Apr 8, 5:32 pm, Steve <[email protected]> wrote:
> I'm just starting to try and wrap my head around the idea of
> instantiating CFCs.
> Here's a real simple example, that I think if I could get
> clarification on to make sure I'm doing it "right", might help a lot
> of other people in this situation.
> Keeping it real basic, let's say I have a simple user registration
> process.
> registration1.cfm asks for an account type and just submits it to
> registration2.cfm where it becomes a hidden form field.
> registration1.cfm also instantiates a component called _userreg.cfc.
> This component handles all of the functions related to a user signing
> up (as opposed to a user logging in and after they've logged in).
> So on registration1.cfm, I'm instantiating it using:
> <CFSET SESSION.userreg=CreateObject("component", "_userreg") />
> The first problem I hit was trying to get my init function to work.
> In _userreg.cfc, I have the following function to set the DSN:
>   <cffunction name="init" access="public">
>           <cfparam name="request.dsn" default="#application.dsn.dsn#">
>   </cffunction>
> And I'm trying to run the init with this (placed just after the CFSET
> above)
> <CFSET SESSION.userreg.Init() />
> On register2.cfm I have a form field for e-mail address - to see if
> it's already in the system.  I have a function in _userreg.cfc called
> "checkEmail" and it takes one argument - the email address submitted:
> <cfif StructKeyExists(form,"register2")>
>         <cfinvoke component="#SESSION.userreg#" method="checkEmail"
> returnvariable="result" email="#trim(" />
> </cfif>
> If, in my checkEmail function I have the cfquery dsn set to
> #request.dsn#, I get an error that it's undefined.
> If I set it to #application.dsn.dsn# it works fine.
> Am I not instantiating it properly, or is it a problem with the init()
> function?
> Thanks,

As you store your cfc instance in session (persistent) scope, just use
<cffunction name="init" ...>
<cfparam name="dsn" default="#application.dsn.dsn#" />
<cfset variables.dsn = arguments.dsn />
Now, every time you invoke [or I prefer using <cfset retVal =
obj.function(params) /> notation] your cfc (and any function in it)
the cfc can refer to it's own persistent variables scope and dsn will
be defined in it...

<Prev in Thread] Current Thread [Next in Thread>