Making an Ajax call is simple and straight forward in ModelGlue. The concept is simple and is given in detail in Doug’s blog. Read the full tutorial.
In order to accomplish this we will have to break the ModelGlue architecture (till now..i couldn’t find any other suitable methods). Anyone who is familiar to ModelGlue architecture will find it easy to understand the below code.
My ModelGlue event:
<message name="testFunction" />
Please note that i am not using a view in this event.
And my controller function:-
<cffunction name="testFunction" returnType="any" output="true" >
<cfargument name="event" type="any" required="true">
<cfset justtest = 1>
<CFCONTENT TYPE="text" RESET="Yes"><CFOUTPUT>#serializeJSON(justTest)#
<cfset request.modelGlueSuppressDebugging = true />
<cfsetting showdebugoutput="false" /></CFOUTPUT><cfabort>
Here we are converting the justtest variable in JSON format and outputting via cfcontent. Here cfcontent acts as the rendered view and returns justtest in JSON format. Here aborting at the end of controller function can be considered as breaking the ModelGlue archi , but this is one way around to solve the problem.
By the way here is my Ajax call:-
var myresult = response.responseText;
I am making use of prototype library for this.
We will get 1 if we alert the responseText call back from the server.
This is only applicable to ModelGlue2. If you are using ModelGlue3 then you would also need to check out how to use fomats in MG3. Thanks Ray Camdon for sharing this link.