Wednesday, February 26, 2003

kwfu: ['cfproperty','cfc']

A long time ago I promised to put some more info in my blog about CFCs. Well, now I'm coming good on that promise.

You may be wondering exactly what use the <cfproperty> tag is. Well, there are at least 2 uses for it. I was intending to put them both into this post, but I got a bit overexuberant with the examples, so I'll split it into 2 blog posts.

1. Providing static data for component instances


When you create a component instance, you can use the getMetaData() function to retrieve all the data that is defined in the <cfproperty> tag. You also get information about the attributes in the <cfcomponent> and <cffunction> tags, but I digress. As an example:

Say you have employee.cfc that contains employee information.

<cfcomponent displayname="employee">
<cfproperty name="minage" type="numeric" value="18">
<cfproperty name="maxage" type="numeric" value="65">

<cffunction name="getEmployee" access="public">
<!--- Code to get employee info goes here --->
</cffunction>

<!--- More methods go here --->

</cfcomponent>

You can create an instance of this component as follows:

<cfscript>
emp = createObject('component','employee');
</cfscript>

You can then retrieve and view the data defined in the cfproperty tags like this:

<cfdump var="#getMetaData(emp)#">

If you look at the output from the <cfdump> tag, you will notice that it contains all the attributes of all the <cfcomponent>, ;<cfproperty> and <cffunction> tags in the component. This is true even if you define your own attributes in those tags. This can be particularly useful for defining information about how component instances and properties of those instances should be treated. You could, for example, create an editHandler attribute for the <cfcomponent> tag that defined what file should be used to edit instances of that component. You might also use this to determine default values for the properties of your component instance. The <cfproperty> tag will not create the properties for you, but you can use the metadata to initialize them yourself.

Something like this:

<cfcomponent displayname="employee">
<cfproperty name="minage" type="numeric" value="18">
<cfproperty name="maxage" type="numeric" value="65">

<cfscript>
init();
</cfscript>

<cffunction name="init" access="private">
<cfset md = getMetaData(this)>
<cfloop from="1" to="#arrayLen(md.properties)#" index="i">
<cfset this[md.properties[i].name] = md.properties[i].value>
</cfloop>
</cffunction>

<!--- More methods go here --->

</cfcomponent>

The following code will allow you to see how this works:

<cfscript>
emp = createObject('component','employee');
</cfscript>
<cfdump var="#emp#">

This is a pretty useful feature, but you need to be careful about the time that getMetaData() can take to return data in complex component hierarchies. You should also be aware that you could quite easily do this instead:

<cfcomponent displayname="employee">

<cfscript>
init();
</cfscript>

<cffunction name="init" access="private">
<cfset md = getMetaData(this)>
<cfset this.minage = 18>
<cfset this.maxage = 65>
</cffunction>

<!--- More methods go here --->

</cfcomponent>

The downside of this way of doing things is that when you use the component browser you will not see what the default values will be in the documentation code.

Hopefully all that made sense, next time <cfproperty> and webservices.

Spike

Sunday, February 23, 2003

kwfu: ['travelling']

Well, it's been a long time since my last post and a lot of things have happened in that time...

I've left my job in the Netherlands, been ski-ing in the French Alps for Christmas and New Year, spent a few weeks travelling across Canada, and got to Australia in time to speak at MXDU in Sydney on 19th and 20th of February. The conference was great and many thanks go out to the speakers and organizers who made it as good as it was. Special thanks to Geoff Bowers and the team from Daemon who had all the speakers over for a good ole aussie barbie on Friday night.

I'm planning on hanging around in Australia for a few months before moving on to Asia for a while. After that, who knows, but it might well involve getting in touch with the Australian authorities for a slightly more permanent visa.

Spike