ColdFusion 9.0 Resources |
Providing resultsColdFusion components can provide information in the following ways:
You can use either technique, or a combination of both, in your applications. The best technique to use depends on your application’s needs and your coding methodologies. For example, many CFC methods that perform business logic return the results as a variable, and many CFC methods that display output directly are designed as modular units for generating output, and do not do business logic. Displaying outputIf you do not specifically suppress output, any text, HTML code, or output that CFML tags generate inside your method gets returned as generated output to the client that calls the component method. If the client is a web browser, it displays these results. For example, the following getLocalTime1 component method shows the local time directly on the page that invokes the method: <cfcomponent> <cffunction name="getLocalTime1"> <cfoutput>#TimeFormat(now())#</cfoutput> </cffunction> </cfcomponent> Component methods that are called by using Flash Remoting or as web services cannot use this method to provide results. Returning a results variableIn the component method definition, you use the cfreturn tag to return the results to the client as variable data. For example, the following getLocalTime2 component method returns the local time as a variable to the ColdFusion page or other client that invokes the method: <cfcomponent> <cffunction name="getLocalTime"> <cfreturn TimeFormat(now())> </cffunction> </cfcomponent> The ColdFusion page or other client, such as a Flash application, that receives the result then uses the variable data as appropriate. Note: If a CFC is invoked using a URL or by submitting
a form, ColdFusion returns the variable as a WDDX packet. A CFC
that is invoked by Flash Remoting, or any other instance of a CFC,
must not return the This scope.
You can return values of all data types, including strings, integers, arrays, structures, and instances of CFCs. The cfreturn tag returns a single variable, as does the return CFScript statement. Therefore, if you want to return more than one result value at a time, use a structure. If you do not want to display output in a method, use output="false" in the cffunction tag. For more information on using the cfreturn tag, see the CFML Reference. Documenting CFCsColdFusion provides several ways to include documentation about your CFCs in your component definitions. The documentation is available when you use introspection to display information about the CFC or call the GetMetadata or GetComponentMetaData function to get the component’s metadata. You can use the following tools for documenting CFCs:
For information on displaying the information, see Using introspection to get information about components. The displayname and hint attributesThe cfcomponent, cffunction, cfargument, and cfproperty tags have displayname and hint attributes. The displayname attribute lets you provide a more descriptive name for a component, attribute, method, or property. When you use introspection, this attribute appears in parentheses next to the component or method name, or on the parameter information line. You use the hint attribute for longer descriptions of the component, method, or argument. In the introspection display, this attribute appears on a separate line or on several lines of the component or method description, and at the end of the argument description. Metadata attributesYou can include arbitrary metadata information as attributes of the cfcomponent, cffunction, cfargument, and cfproperty tags. To create a metadata attribute, specify the metadata attribute name and its value. For example, in the following cfcomponent tag, the Author attribute is a metadata attribute. This attribute is not used as a function parameter; instead, it indicates who wrote this CFC. <cfcomponent name="makeForm" Author="Bean Lapin"> Metadata attributes are not used by ColdFusion for processing; they also do not appear in standard ColdFusion introspection displays; however, you can access and display them by using the GetMetaData or GetComponentMetaData function to get the metadata. Each attribute name is a key in the metadata structure of the CFC element. Metadata attributes are used for more than documentation. Your application can use the GetMetadata function to get the metadata attributes for a component instance, or the GetComponentMetaData function to get the metadata for an interface or component that you have not yet instantiated. You can then act based on the values. For example, a mathCFC component could have the following cfcomponent tag: <cfcomponent displayname="Math Functions" MetaType="Float"> In this case, a ColdFusion page with the following code sets the MetaTypeInfo variable to Float: <cfobject component="mathCFC" name="MathFuncs"> <cfset MetaTypeInfo=GetMetadata(MathFuncs).MetaType> Note: All metadata values are replaced by strings
in the metadata structure returned from the GetMetadata function.
Because of this, do not use expressions in custom metadata attributes.
The cfproperty tagThe cfproperty tag is used to create complex data types with WSDL descriptors and for component property documentation, as follows:
Note: The cfproperty tag does not
create a variable or assign it a value. It is used for information
purposes only. You use a cfset tag, or CFScript assignment statement,
to create the property and set its value.
Saving and naming ColdFusion componentsThe following table lists the locations in which you can save component files and how they can be accessed from each location:
Note: ColdFusion mappings and custom tag roots can
exist within the web root. If so, they are accessible to remote
requests, including URL, form, Flash Remoting, and web services
invocation.
When you store components in the same directory, they are members of a component package. You can group related CFCs into packages. Your application can refer to any component in a directory specifically by using a qualified component name that starts with a subdirectory of one of the accessible directories and uses a period to delimit each directory in the path to the directory that contains the component. For example, the following example is a qualified name of a component named price: catalog.product.price In this example, the price.cfc file must be in the catalog\product subdirectory of a directory that ColdFusion searches for components, as listed in the preceding table. When you refer to a component using the qualified name, ColdFusion looks for the component in the order described in Specifying the CFC location. Establishing a descriptive naming convention is a good practice, especially if you plan to install the components as part of a packaged application. |