Skip to content

Instantly share code, notes, and snippets.

@mhenke
Created October 2, 2010 13:38
Show Gist options
  • Save mhenke/607648 to your computer and use it in GitHub Desktop.
Save mhenke/607648 to your computer and use it in GitHub Desktop.
<!--- place in views/say folder --->
<cfoutput>
#javaScriptIncludeTag("jquery-1.4.2.min")#
</cfoutput>
<script language="JavaScript">
// Listen to the "click" event of the "alert-button" link and make an AJAX request
$(document).ready(function(){$("#alert-button").click(function() {
$.ajax({
type: "POST",
url: $(this).attr("href"), // Outputs "/say/hello"
dataType: "json",
success: function(response) {
$("h1").html(response.message);
$("p").html(response.time);
}
});
return false; // keeps the normal request from firing
});
});
</script>
<cfoutput>
<!--- View code --->
<h1></h1>
<p></p>
#linkTo(text="Alert me!", controller="say", action="hello", id="alert-button")#
</cfoutput>
<!--- place in views/say folder --->
<cfoutput>
#javaScriptIncludeTag("jquery-1.4.2.min")#
</cfoutput>
<cfoutput>
<!--- View code --->
<h1></h1>
<p></p>
#remoteLinkTo(text="Alert me!", controller="say", action="hello")#
</cfoutput>
<!--- place in views/say folder --->
<cfoutput>
<!--- View code --->
<h1></h1>
<p></p>
#linkTo(text="Alert me!", action="hello", id="alert-button", params="format=json")#
</cfoutput>
<!--- place in views/say folder --->
<cfoutput>
<!--- Remote View code --->
#pageInsertHTML(selector="h1", content=greeting.message)#
#pageInsertHTML(selector="p", content=greeting.time)#
</cfoutput>
<!--- place in controllers folder --->
<cfcomponent extends="Controller">
<cffunction name="init">
<cfset filters("turnOffDebugging")>
</cffunction>
<!--- approach 1 --->
<cffunction name="hello">
<!--- Prepare the message for the user --->
<cfset greeting = {}>
<cfset greeting["message"] = "Hi there">
<cfset greeting["time"] = Now()>
<!--- If your request is an AJAX request, respond with the CFML SerializeJSON function --->
<cfif isAjax()>
<cfset renderText(SerializeJSON(greeting))>
</cfif>
<!--- Otherwise, let Wheels do its normal HTML response (the view file at `views/say/hello.cfm`) --->
</cffunction>
<!--- approach 2
<cffunction name="hello">
<!--- Prepare the message for the user --->
<cfset greeting = {}>
<cfset greeting["message"] = "Hi there">
<cfset greeting["time"] = Now()>
<!--- If your request is an AJAX request, respond with the `views/say/hello.js.cfm remote view` --->
<cfif isAjax()>
<cfset renderRemotePage()>
</cfif>
<!--- Otherwise, let Wheels do its normal HTML response (the `views/say/hello.cfm` view file) --->
</cffunction>
--->
<!--- approach 3
<cffunction name="hello">
<!--- Prepare the message for the user --->
<cfset greeting = {}>
<cfset greeting["message"] = "Hi there">
<cfset greeting["time"] = Now()>
<!--- Provides will determinate the format you want and convert the data appropriately --->
<cfset renderWith(greeting)>
</cffunction>
--->
<cffunction name="turnOffDebugging">
<!--- turn off debugging output --->
<cfsetting showDebugOutput="No">
</cffunction>
</cfcomponent>
@mhenke
Copy link
Author

mhenke commented Oct 2, 2010

This is the sample code from "Tutorial: Wheels, AJAX, and You" http://cfwheels.org/docs/1-0/chapter/wheels-ajax-and-you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment