• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I pass a variable to a constructor?

Guest
Aug 28, 2007 Aug 28, 2007

Copy link to clipboard

Copied

If I have a CFC like the one below:
<cfcomponent>
<!--- call init() automatically when the CFC is instantiated --->
<cfset init()>
<cffunction name="init" access="public" output="no" returntype="shoppingCart">
<cfargument name="cartID" type="UUID" required="yes" >
<cfset variables.cartID = arguments.cartID>
<cfreturn this>
</cffunction>
</cfcomponent>

...how do I pass a requried variable to the constructor? (I am using Flex as my front end with Flash Remoting to call my CFCs so I won't be instantiating this from ColdFusion).
Any help appreciated....
TOPICS
Advanced techniques

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Aug 29, 2007 Aug 29, 2007
Say for example I have the following CFC called test.cfc:

<cfcomponent>
<cfset init()>
<cffunction name="init" access="public" output="no" returntype="test">
<cfargument name="cartID" type="numeric" required="yes" >
<cfset variables.cartID = arguments.cartID>
<cfreturn this>
</cffunction>
</cfcomponent>

...and I have the following CFM file that calls it.....test.cfm.....

<cfobject component="test" name="comp">
<cfset comp.init(6)>
<cfdump var="#comp.init(6)#">

This will cause an error ...

Votes

Translate

Translate
LEGEND ,
Aug 28, 2007 Aug 28, 2007

Copy link to clipboard

Copied

spatz04 wrote:
> If I have a CFC like the one below:
> <cfcomponent>
> <!--- call init() automatically when the CFC is instantiated --->
> <cfset init()>
> <cffunction name="init" access="public" output="no"
> returntype="shoppingCart">
> <cfargument name="cartID" type="UUID" required="yes" >
> <cfset variables.cartID = arguments.cartID>
> <cfreturn this>
> </cffunction>
> </cfcomponent>
>
> ...how do I pass a requried variable to the constructor? (I am using Flex as
> my front end with Flash Remoting to call my CFCs so I won't be instantiating
> this from ColdFusion).
> Any help appreciated....
>

You would have an <mx:method...> in your <mx:RemoteObject...> for the
init function in the CFC. Then you will need to call this from the
appropriate place in your flex application. Maybe in a function called
with the creationComplete event.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 29, 2007 Aug 29, 2007

Copy link to clipboard

Copied

Say for example I have the following CFC called test.cfc:

<cfcomponent>
<cfset init()>
<cffunction name="init" access="public" output="no" returntype="test">
<cfargument name="cartID" type="numeric" required="yes" >
<cfset variables.cartID = arguments.cartID>
<cfreturn this>
</cffunction>
</cfcomponent>

...and I have the following CFM file that calls it.....test.cfm.....

<cfobject component="test" name="comp">
<cfset comp.init(6)>
<cfdump var="#comp.init(6)#">

This will cause an error saying the parameter cartID to function init() was required but not passed in, because the constructor call ( <cfset init()> ) does not pass a parameter to the init function. The only way I can get this to work is to comment out the <cfset init()> line. I guess I don't need this in my scenario because I'm calling the init method directly anyway.

FYI: I've been following Ben Fortas phone selector example ( a good example of building a simple Flex/CF app):
http://www.adobe.com/devnet/flex/articles/coldfusionflex_part3_02.html

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2007 Aug 29, 2007

Copy link to clipboard

Copied

LATEST
Yes you would not use the <cfset init()> line in the pseudo constructor.
Then, as you where doing in your cfml example, you will call init()
directly in your code.

Then to translate this to your mxml, you would put a <mx:method
name="init" ...> tag in your <mx:remoteObject... tag block. Then as
some relevant part of your code you would call the init() method in your
mxml. Very likely on some creationComplete() event.

One thing to remember, without using more sophisticated techniques, a
CFC can not maintain state when using remote calls. This is true for
flex|flash remoting or web services. In other words you can't make one
call to a method to set a value, then make another call to return the
value when using remote methods.




Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation