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

getting started with cfc's - how do i pass a variable in?

Participant ,
Apr 19, 2008 Apr 19, 2008
hi - i'm trying to write my first cfc. i want it to get some data from the database but cant seem to get it to accept a variable that exists in the page i'm calling the cfc from (although oddly it seems to be able to make use of form variables that exists in that page - i can give more info on that weirdness if you want it)

i have this: (code attached)

but get this error:

"Element LOCATIONID is undefined in FORCFC."

what's that all about - i just set it on the line before i invoked the cfc - didn't i?

thanks in advance
TOPICS
Getting started
1.0K
Translate
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 ,
Apr 19, 2008 Apr 19, 2008
In your function you need cfargument tags.

Passing your argument to a function can be done a variety of ways. My preference is argumentcollection = somestructure.
Translate
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
Participant ,
Apr 19, 2008 Apr 19, 2008
thanks Dan, that's what i thought - but when i change the funcion to this

<cfcomponent displayname="coordinates cfc" hint="This is the CFC to get the x and y coordinates and locationname of a location from the locationid pass this as forcfc.locationid">
<!--- this function gets the x and y coordinates of a location from the locationid --->
<cffunction name="get_location_details" hint="gets the x and y coordinates and locationname of a location from the locationid pass this as forcfc.locationid" returntype="query">
<cfargument name="forcfc.locationid" />
<cfquery datasource="arj" name="rs_location" >
select locationname, xcoord, ycoord
from tbl_locations
where locationkey = #forcfc.locationid#
</cfquery>
<cfreturn rs_location>
</cffunction>
</cfcomponent>

(by adding this line - <cfargument name="forcfc.locationid" /> )

i get this error:

Invalid argument name.
The name forcfc.locationid used for an argument has illegal characters in it.

so does that mean i can't scope my variables when i use cfc's - that sounds a bit odd.

would passing the vars as a structure as you suggest get around this problem somehow?

forgive me but could you elaborate on how to do that (i dont know to make a scructure) and ive studies the cf docs on this but that doesnt seem to help either

at this rate i think i'll give cfc's a miss and just make an include that does what i want - but that sounds counter intuative as everyone says how important it is to use cfc's

thanks for anything else you can give me

cheers

Nick
Translate
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 ,
Apr 19, 2008 Apr 19, 2008
Give your arguments simple names. No periods.

CFCs are designed to be available to more than one calling template. Therefore they are islands of code unto themselves. They have nothing to do with calling templates other than receiveing arguments and returning something.
Translate
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
Participant ,
Apr 19, 2008 Apr 19, 2008
quote:

Originally posted by: Dan Bracuk
Give your arguments simple names. No periods.



so taking that forward then it means i cant scope my variables (that i want to be available to cfc's) - that's a bit of an odd design feature of cfc's then isn't it - i thought scoping variables was important?

i have to say that i will stick to doing what i do at the moment, which is (whenever i have a piece of code i use several times throughout my app) i stick it in a separate cfm doc (with nothing else in the template except that piece of code) and then just 'invoke' that code using <cfinclude...

what do you say to that?

v interested to hear your views - as i said everyone tells me cfc's are v important but a) their a bit weird to get my head around (compared to my cfinclude model above) b) now it seems i have to stop scoping my variables in order to use them (which surely is a step backwards isn't it)

thanks very much in advance - i hope someone can tell my cfc's are still the way to go 🙂
Translate
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
Contributor ,
Apr 23, 2008 Apr 23, 2008
LATEST
After you check out the LiveDocs links that Adam posted, check out my site. I've got a series on OOP with CF with code examples. It starts here:

Intro to Object.cfc

Part 3, Injecting Data into Objects should cover your specific question.

HTH,

Adrian
Translate
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
Participant ,
Apr 19, 2008 Apr 19, 2008
Can you also use cfparam as well in cfcs instead of arguments Dan?
Translate
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 ,
Apr 19, 2008 Apr 19, 2008
quote:

Originally posted by: Hydrowizard
Can you also use cfparam as well in cfcs instead of arguments Dan?

To do what?
Translate
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 ,
Apr 19, 2008 Apr 19, 2008
Inside your cfc, you can use the arguments scope, the this scope, the property scope, or the variables scope.

cfc's are not the only way to reuse code. You can also write custom tags, or included files. Also since some cfc's are nothing more than a bunch of udfs, you can put these udfs into an included file.
Translate
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 ,
Apr 19, 2008 Apr 19, 2008
Translate
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