Ask Ben: Accessing Cached CFCs With AJAX via Remote Proxies

<cfcomponent
	extends="RemoteProxy"
	output="false"
	hint="I provide remote proxy functionality for the MessageService.">
 
	<!---
		NOTE: No need for an "Init" method as the following
		methods are going to be called on a one-off basis per
		AJAX request.
	--->
 
	<!---
		Because we are breaking encapsulation throughout this
		component, it's best to store a global reference to it
		here in case it needs to change (will only need to be
		changed here).
	--->
	<cfset THIS.MessageService = APPLICATION.MessageService />
 
 
	<!--- NOTE: Give all methods REMOTE access. --->
 
 
	<cffunction
		name="AddMessage"
		access="remote"
		returntype="struct"
		returnformat="json"
		output="false"
		hint="I add a message to the cached message collection.">
 
		<!--- Define arguments. --->
		<cfargument
			name="Message"
			type="string"
			required="true"
			hint="I am the message being added."
			/>
 
		<!--- Define the local scope. --->
		<cfset var LOCAL = {} />
 
		<!--- Create a new response. --->
		<cfset LOCAL.Response = THIS.NewResponse() />
 
		<!--- Check to see if the message is valid. --->
		<cfif Len( Trim( ARGUMENTS.Message ) )>
 
			<!--- Add the message. --->
			<cfset THIS.MessageService.AddMessage(
				Trim( ARGUMENTS.Message )
				) />
 
		<cfelse>
 
			<!--- Add the error. --->
			<cfset THIS.AddResponseError(
				LOCAL.Response,
				"Message",
				"Message must not be empty."
				) />
 
		</cfif>
 
		<!--- Return the response. --->
		<cfreturn LOCAL.Response />
	</cffunction>
 
 
	<cffunction
		name="GetMessages"
		access="remote"
		returntype="struct"
		returnformat="json"
		output="false"
		hint="I return the cached message collection.">
 
		<!--- Define the local scope. --->
		<cfset var LOCAL = {} />
 
		<!--- Create a new response. --->
		<cfset LOCAL.Response = THIS.NewResponse() />
 
		<!--- Set the messages as the response data. --->
		<cfset LOCAL.Response.Data = THIS.MessageService.GetMessages() />
 
		<!--- Return the response. --->
		<cfreturn LOCAL.Response />
	</cffunction>
 
</cfcomponent>

For Cut-and-Paste