After working in Coldbox for a while I know your first instinct is to make a cfajaxproxy call to a cfc event just like all the other Coldbox events. However, using cfajaxproxy is just a little different. Here is an example to show you one simple way of doing it, not the only way but one of them.
First let me note that I am using Coldspring version of IOC. It should work the same though with any form of IOC. Mostly we need the IOC to provide the bean to the services your Ajax call requires from within the Coldbox proxy.
After having provided a service in Coldspring like so:
<bean id="usersService" class="model.usersService">
<constructor-arg name="privatemessagesGateway">
<ref bean="privatemessagesGateway"/>
</constructor-arg>
<constructor-arg name="ColdBoxController">
<ref bean="ColdBoxController"/>
</constructor-arg>
</bean>
we can use this "usersService" bean in the coldboxproxy.cfc like this:
<cffunction name="ajxSetPMRead" access="remote" output="false" returntype="void">
<cfargument name="messageid" type="String" required="true"/>
<cfset var pm = getBean('usersService').getPrivateMessageByID(int(arguments.messageid))>
<cfset pm.setRead(1)>
<cfset getBean('usersService').savePrivateMessage(pm)>
</cffunction>
The code above is what the Ajax, cfcajaxproxy, will call as a method for Coldbox to perform. It fetches a private message object bean, sets or updates some properties, then uses the gateway to save the object bean. I don't have a return or output but you can do that also here if desired. And here is the actual view and Ajax calling code:
<cfajaxproxy cfc="coldboxproxy" jsclassname="cbProxy">
<a href="##" onClick="showMessageDetails('#privateMessagesQuery.privatemessageid#','#privateMessagesQuery.read#','#privateMessagesQuery.subject#','#privateMessagesQuery.fromUserName#','#privateMessagesQuery.message#')">#privateMessagesQuery.subject#</a>
<$cript>
function showMessageDetails(messageid,read,subject,from,message){
var e = new cbProxy();
if(read == 0){
e.setCallbackHandler(setPrivateMessageRead);
e.setErrorHandler(ajaxErrorHandler);
e.ajxSetPMRead(messageid);
}
document.getElementById('pmReply').style.display = "none";
document.getElementById('pmDetails').style.display = "inline";
document.getElementById('pmDetails_Subject').innerHTML=subject;
document.getElementById('pmDetails_From').innerHTML=from;
document.getElementById('pmDetails_Message').innerHTML=message;
}
var setPrivateMessageRead = function(res){
alert('set to read');
}
var ajaxErrorHandler = function(statusCode, statusMsg){
alert('Status: ' + statusCode + ', ' + statusMsg);
}
</script>
The cfc attribute is dot notation to the location of your coldboxproxy.cfc, mine here is at the root. Basically I'm showing the private message details in a div but needing to set the read flag in the database to 1 to indicate it has been read. And using Ajax I can set this in the database for each message read without a page refresh. The alerts in the javascript are for demonstration and testing purposes.
The code I provided is a cut down version of actual code and does not include the view event, form code, queries, or other behind the scenes code. I hope this helps to give you a step ahead on how to use Ajax with the Coldbox framework.
The tutorial is simple in its presentation but gets the job done and includes a full working application putting everything to good use.
I need to think over wether or not passing bean reference properties to all my CS beans is the most effective way of using global configs any ways..
<a href="http://seoprofesionals.com/">SEO Company</a> | <a href="http://www.squidoo.com/seoprofesionals">SEO Services</a> | <a href="http://aclickaheadseo.blogspot.com/">SEO Consultants</a>