I was amazed to find out that keyword new was not a
reserved word. I was fairly certain that it was not a keyword that is being used but I thought It may be reserved for latter use. With that being said and my new found love for
cfcUnit (more on this later kids!) I now have a cool little utility function. Again, I got the idea from cfcUnit as they use the newObject() form. So I alway have a base class for each project that all other components or other base classes will extend. In this base class I like to have little helper functions and this new one is a great one. Here is the code to my base component.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfcomponent name="BaseComponent">
<cfset NULL = "">
<cfset variables["_objectID"] = CreateUUID()>
<cffunction name="new" access="public" returntype="WEB-INF.cftags.component">
<cfargument name="type" type="string" required="true"/>
<cfargument name="object" type="string" default="component">
<cfset var ret = "">
<cftry>
<cfset ret = createObject(arguments.object, arguments.type)>
<cfcatch type="coldfusion.runtime.CfJspPage$NoSuchTemplateException">
<cfthrow type="Exception.NoSuchComponent" message="Could not find the component '#arguments.type#'"/>
</cfcatch>
<cfcatch type="any">
<cfthrow type="Exception.Instantiation" message="Could not instantiate object of type #arguments.type#" detail="#arguments.type#"/>
</cfcatch>
</cftry>
<cfreturn ret>
</cffunction>
</cfcomponent>
1<cfcomponent name="BaseComponent">
2
3 <cfset NULL = "">
4 <cfset variables["_objectID"] = CreateUUID()>
5
6 <cffunction name="new" access="public" returntype="WEB-INF.cftags.component">
7 <cfargument name="type" type="string" required="true"/>
8 <cfargument name="object" type="string" default="component">
9 <cfset var ret = "">
10
11 <cftry>
12 <cfset ret = createObject(arguments.object, arguments.type)>
13
14 <cfcatch type="coldfusion.runtime.CfJspPage$NoSuchTemplateException">
15 <cfthrow type="Exception.NoSuchComponent" message="Could not find the component '#arguments.type#'"/>
16 </cfcatch>
17 <cfcatch type="any">
18 <cfthrow type="Exception.Instantiation" message="Could not instantiate object of type #arguments.type#" detail="#arguments.type#"/>
19 </cfcatch>
20 </cftry>
21 <cfreturn ret>
22 </cffunction>
23
24</cfcomponent>
This is a really good time saver because now anytime I need to instantiate any objects in any cfc that extends my base its really easy to do.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfcomponent name="BaseTest" extends="BaseComponent">
<cfset variables.shoppingCart = "">
<cfset variables.dsn = "">
<cffunction name="init" access="public" returntype="BaseTest">
<cfargument name="dsn" type="string" required="true">
<cfset variables.dsn = arguments.dsn>
<cfreturn this>
</cffunction>
<cffunction name="getCart" access="public" returntype="any">
<cfset variables.shoppingCart = new("ShoppingCart").init(variables.dsn)>
<cfreturn variables.shoppingCart>
</cffunction>
</cfcomponent>
1<cfcomponent name="BaseTest" extends="BaseComponent">
2
3 <cfset variables.shoppingCart = "">
4 <cfset variables.dsn = "">
5
6 <cffunction name="init" access="public" returntype="BaseTest">
7 <cfargument name="dsn" type="string" required="true">
8 <cfset variables.dsn = arguments.dsn>
9 <cfreturn this>
10 </cffunction>
11
12 <cffunction name="getCart" access="public" returntype="any">
13 <cfset variables.shoppingCart = new("ShoppingCart").init(variables.dsn)>
14 <cfreturn variables.shoppingCart>
15 </cffunction>
16
17</cfcomponent>
This entry was posted on December 14, 2006 at 5:57 PM and has received 1052 views. Comments 2 |
Print this entry.
#1 by Phil Cooper on 2/1/07 - 12:13 PM
#2 by Dan on 2/1/07 - 12:29 PM