Calling CFCs from JavaScript


As you learned in Chapter 19, "Creating Advanced ColdFusion Components", any CFC method that uses access="Remote" can be invoked via the Macromedia Flash player, as Web Services, via HTML forms, or via simple URL invocation. If you access a remote method via the URL, the value that the method returns will be automatically serialized as a WDDX packet. That means that you can call CFCs from JavaScript in much the same way that the robot page was called in Listing 17.9.

NOTE

This applies only to methods that return values via <cfreturn>, rather than generating HTML or some other type of ad-hoc output.


For instance, Listing 17.11 creates a simple CFC that can be used in place of the ad-hoc Films Robot.cfm page shown earlier (Listing 17.10). This component has just one method, GetFilmsBy Rating(), which accepts a rating ID and returns a recordset that contains the ID number and title of each film with that rating.

Listing 17.11. FilmCFC.cfcA ColdFusion Component That Can be Accessed via JavaScript
 <!--- Name:        FilmCFC.cfc Author:      Nate Weiss and Ben Forta Description: Example CFC for supplying              recordset data to JavaScript              via WDDX Created:     02/01/05 ---> <cfcomponent>   <!--- GetFilmsByRating() method --->   <cffunction name="GetFilmsByRating"               returntype="query"               access="remote"               hint="Returns films that match the specified RatingID.">     <!--- Required argument: Rating ID --->     <cfargument name="RatingID"                 type="numeric"                 required="Yes">     <!--- Local variable --->     <cfset var FilmsQuery="">     <!--- Get film information from database --->     <cfquery datasource="ows"              name="FilmsQuery"              cachedwithin="#CreateTimeSpan(0,0,10,0)#">       SELECT FilmID, MovieTitle       FROM Films       WHERE Ratingid=#ARGUMENTS.RatingID#     </cfquery>     <!--- Return the query --->     <cfreturn FilmsQuery>   </cffunction> </cfcomponent> 

You can invoke the GetFilmsByRating() method with your browser, using a URL similar to the following (again, you may need to adjust the URL a bit depending on how your ColdFusion server is configured):

http://127.0.0.1:8500/ows_adv/17/FilmCFC.cfc?Method=GetFilmsByRating&RatingID=1

The CFC should respond with approximately the same WDDX packet that this URL produces (which invokes the simple robot from Listing 17.10):

http://127.0.0.1:8500/ows_adv/17/FilmsRobot.cfm?RatingID=1

Therefore, it's a simple matter to adjust Listing 17.9 so that it interacts with the CFC instead of the robot page. Simply change this line:

 <cfset FilmComponentURL="http://127.0.0.1:8500/ows_adv/17/FilmsRobot.cfm?"> 

to this:

[View full width]

<cfset FilmComponentURL="http://127.0.0.1:8500/ows_adv/17/FilmCFC .cfc?Method=GetFilmsByRating">

Once that change is made, the Listing 17.9 page will work just as it did before, retrieving the film information from the server in real time. The only difference is that a CFC is now the supplier of data. This is cool because you can use the same CFC to supply data to JavaScript pages, Flash applications, consumers of Web Services, and your own ColdFusion pages.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net