Posts

Showing posts from June, 2017

What is the difference between Custom tag and Coldfusion Component?

The differences are Custom Tags: -A single entry point is available for custom tags -Custom tags does not support formalized parameter passing and validation mechanism -Persistence is not possible for custom tags -Custom tags are accessible locally and only by ColdFusion ColdFusion Component: -Multiple entry points are available for CFC -CFC does support formalized parameter passing and validation mechanism -Persistence is possible for CFC -CFCs can be accessed as web services

How to I initialize a CFC at the same time as I create it?

In many examples using CFCs, the call that creates the CFC also calls an init() method. This is done to both create an instance of a CFC as well as initialize it with certain information. So for example, you may want to pass in a datasource name to a CFC so that the component can perform database queries. How is this done? This is a 2 step process. The first is to call the CFC using syntax like the example below: <cfset cachedQuery = createObject("component", "CachedQuery").init(dsn)> This says to create a variable called cachedQuery which will contain a reference to a CFC. The CFC is created using the createObject() function. At the same time that the CFC is created, a method of the CFC called init() is called. Whatever is returned from the init() method is what will actually be loaded into the CachedQuery variable. This is called method chaning. It is essentially the act of calling multiple functions in one line. The result of the first operation (cre

Init() function in ColdFusion

init() is just a standard convention most people adopt with components. The init() function initializes any variables needed by the component's functions. Then returns a reference to the current component. ie Test . cfm < cfset myObj = createObject ( "component" , "MyComponent" ). init ( dsn = "My DSN" )> MyComponent . cfc <cfcomponent> < cffunction name = "init" output = "false" returntype = "MyComponent" > < cfargument name = "dsn" type = "string" > <!--- initialize variables used within this component ---> < cfset variables . dsn = arguments . dsn > <!--- return a reference to this component ---> < cfreturn this > < /cffunction> </ cfcomponent