Created
October 2, 2010 13:38
-
-
Save mhenke/607648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- place in views/say folder ---> | |
<cfoutput> | |
<!--- Remote View code ---> | |
#pageInsertHTML(selector="h1", content=greeting.message)# | |
#pageInsertHTML(selector="p", content=greeting.time)# | |
</cfoutput> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the sample code from "Tutorial: Wheels, AJAX, and You" http://cfwheels.org/docs/1-0/chapter/wheels-ajax-and-you