views:

290

answers:

3

Hi All, I am using Ajax with ModelGlue in a ColdFusion Application. I want to make an Ajax call to return a value. I do not want to render any view. I just want a database interaction and bring back a value.

My Ajax call:

new Ajax.Request(root+'test.testFunction',{
  method: 'post',
  parameters: {param1:paramval},
  onSuccess: function(response){
    alert(response.responseText);
    var myresult = response.responseText;
  }
});

my modelglue event :

<event-handler name="test.testFunction">
     <broadcasts>
          <message name="testFunction" />
     </broadcasts>
</event-handler>

and my controller function:

<cffunction name="testFunction" returnType="any" output="true" >
     <cfargument name="event" type="any" required="true">
        <cfset justtest = 1>
     <cfreturn justtest>
</cffunction>

I am using prototype as my ajax library.

When i alert the responseText i am getting null value. Is this bcoz i have not included the view part in the event handler? If i included the view part then i wud have to create a new page which i dont want to do. Is it possible to get just a server value by ajax call without rendering any view? I want to have myresult value as 1 according to the above scenario.

Pls help. Thnx for any help.

+3  A: 

When you say you "just want to bring back a value" -- that's your "view". What you want to do is use a special view for your remote (ajax) event that just spits out the value. For example, if you want it to return JSON, you might do this:

Event Configuration:

<event-handler name="test.testFunction">
     <broadcasts>
          <message name="testFunction" />
     </broadcasts>
     <views>
          <include name="body" template="renderJson.cfm" />
     </views>
</event-handler>

Controller function:

<cffunction name="testFunction" returnType="any" output="true" >
     <cfargument name="event" type="any" required="true">
     <cfset event.setValue('justtest', 1) />
</cffunction>

renderJson.cfm:

<cfoutput>#serializeJson(event.getValue('justtest'))#</cfoutput>

If you're using Model-Glue 3, you can use the new Event Formats feature to piggy-back this ajax view on an existing event that does the same thing for a different view-format.

Adam Tuttle
A: 

In this situation, you should really be calling the remote proxy of your service, bypassing the MVC framework. :)

Oh, and don't forget you can use <cfajaxproxy> if you're using CF8.

Henry
That didn't appear to be the consensus reached in your question
Antony
It depends. In this case, calling the actual method makes sense.
Henry
+1  A: 

Try using this at the end of your controller function:

<CFCONTENT TYPE="text" RESET="Yes"><CFOUTPUT>#serializeJSON(justTest)#
<cfset request.modelGlueSuppressDebugging = true />
<cfsetting showdebugoutput="false" /></CFOUTPUT><cfabort>

So like this:

<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>

</cffunction>

This will keep your current view and return 'justTest' as a json.

If you are using firefox you should be able to see the reponse from the server.

Masterbuddha
@Masterbuddha ...thnx ..it worked...u r one hell of a coder..If i understand this correctly the function is not returning anything but instead the server returns the data in the form of cfcontent. The cfcontect actually acts as a view in this case. Am i correct? anyways thnx a lot man.....
ajithmanmu
Something about this solution really rubs me the wrong way. If you want to interrupt the Model-Glue event lifecycle, why use a Model-Glue event to begin with? In that case, I would do as Henry suggested and just use a remote proxy.
Adam Tuttle