Changes between Version 1 and Version 2 of HowTos/HowToUseRemoting
- Timestamp:
- 11/16/09 23:54:26 (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
HowTos/HowToUseRemoting
v1 v2 1 = How To UseRemoting =1 = Remoting = 2 2 3 3 == History == … … 10 10 == The Basics == 11 11 12 The process is simple. Model-Glue 3 has a component called "!RemotingService.cfc" that sits in the root of your project folder and uses cfmodule to run the index.cfm file of your application. The !RemotingService extends !ModelGlue.gesture.remoting.!AbstractRemotingService which provides one function: executeEvent(). When you call !RemotingService.executeEvent(...), the remoting service uses a handle on the ModelGlue instance in the application scope to run the event and then extracts values you've asked for from the Event object into a struct which is then returned to your remoting client. And because of how this is all implemented, it can be used either via a Flex !RemoteObject, directly, or as a web service. This makes it possible for Flex, AJAX, and HTML applications to all make use of common functionality.12 The process is simple. Model-Glue 3 has a component called "!RemotingService.cfc" that sits in the root of your project folder and uses cfmodule to run the index.cfm file of your application. The !RemotingService extends !ModelGlue.gesture.remoting.!AbstractRemotingService which provides one function: executeEvent(). When you call !RemotingService.executeEvent(...), the remoting service uses a handle on the !ModelGlue instance in the application scope to run the event and then extracts values you've asked for from the Event object into a struct which is then returned to your remoting client. And because of how this is all implemented, it can be used either via a Flex !RemoteObject, directly, or as a web service. This makes it possible for Flex, AJAX, and HTML applications to all make use of common functionality. 13 13 14 14 == Some basic rules regarding the !RemotingService.cfc == … … 16 16 Regardless of any other factor, the CFC in question must follow two rules: 17 17 18 1. It must live in the root folder of a Model-Glue application. This is because it relies on the presence of an instan e of !ModelGlue.cfc in the application scope to locate and run any events. It can have any name but it has to be in the root of the application.18 1. It must live in the root folder of a Model-Glue application. This is because it relies on the presence of an instance of !ModelGlue.cfc in the application scope to locate and run any events. It can have any name but it has to be in the root of the application. 19 19 1. It must extend modelglue.gesture.remoting.!AbstractRemotingService. This base class provides essential functionality (such as locating the Model-Glue instance in the application scope regardless of the name of the variable containing it). 20 20 … … 29 29 The code for this CFC 30 30 {{{ 31 #!xml 32 <cfcomponent output="false"> 33 <cfset variables.instance = StructNew()/> 34 35 <cffunction name="init" access="public" output="false" returntype="Users"> 36 <cfreturn this/> 37 </cffunction> 31 <cfcomponent output="false"> 32 <cfset variables.instance = StructNew() /> 33 34 <cffunction name="init" access="public" output="false" returntype="Users"> 35 <cfreturn this /> 36 </cffunction> 38 37 39 38 <cffunction name="getUsers" access="remote" output="false" returntype="any"> 40 <cfset var local = StructNew() />41 <cfset local.myQuery = QueryNew("name","varchar") >39 <cfset var local = StructNew() /> 40 <cfset local.myQuery = QueryNew("name","varchar") /> 42 41 <cfloop from="1" to="12" index="local.j"> 43 <cfset QueryAddRow(local.myQuery, 1) >44 <cfset QuerySetCell(local.myQuery, "name", "Person-#local.j#", local.j) >42 <cfset QueryAddRow(local.myQuery, 1) /> 43 <cfset QuerySetCell(local.myQuery, "name", "Person-#local.j#", local.j) /> 45 44 </cfloop> 46 <cfreturn local.myQuery />45 <cfreturn local.myQuery /> 47 46 </cffunction> 48 47 … … 56 55 The code for !GetController.cfc is.. 57 56 {{{ 58 #!xml59 57 <cfcomponent output="false" beans="Users" extends="ModelGlue.gesture.controller.Controller"> 60 58 … … 72 70 }}} 73 71 74 Notice here, we are using the new Model-Glue 3 feature called "bean injection". The controller has a scope called "beans" which contains an instance of our Users.cfc class. For this to work properly, you need to define the Users.cfc bean in the Coldspring.xml file like so.. 75 76 {{{ 77 #!xml 72 Notice here, we are using the new Model-Glue 3 feature called "bean injection". The controller has a scope called "beans" which contains an instance of our Users.cfc class. For this to work properly, you need to define the Users.cfc bean in the !ColdSpring.xml file like so.. 73 74 {{{ 78 75 <bean id="Users" class="mapping_to_your_app.model.Users"/> 79 76 }}} … … 84 81 85 82 {{{ 86 #!xml 87 <controller id="!GetController" type="mapping_to_your_app.controller.!GetController"> 83 <controller id="GetController" type="mapping_to_your_app.controller.GetController"> 88 84 <message-listener function="users" message="get.users" /> 89 85 </controller> … … 93 89 94 90 {{{ 95 #!xml96 91 <event-handler name="get.users"> 97 92 <broadcasts> … … 108 103 109 104 {{{ 110 #!xml111 105 <cfdump label="My Users" var="#event.getValue("users")#"> 112 106 }}} … … 124 118 == A couple of Important Things to Note about using Flex Remoting: == 125 119 126 1. If you have a /!ModelGlue mapping (via either Application.cfc or the ColdFusion Administrator), Flash Remoting will not work until you have edited {cfusion_home}/wwwroot/WEB-INF/flex/remoting-config.xml (on J2EE or multiserver, it's inside {jrun_home}/servers/cfusion.ear/cfusion.war/WEB-INF/flex/remoting-config.xml). You have to find the setting <use-mappings>false</use-mappings> and change it to true. Then restart your CF server. Until you've done this, calls to MG remoting from Flex applications will fail due a file not found. This only applies to Flex applications making remoting calls through the !FlashRemoting gateway.120 1. If you have a /!ModelGlue mapping (via either Application.cfc or the !ColdFusion Administrator), Flash Remoting will not work until you have edited {cfusion_home}/wwwroot/WEB-INF/flex/remoting-config.xml (on J2EE or multiserver, it's inside {jrun_home}/servers/cfusion.ear/cfusion.war/WEB-INF/flex/remoting-config.xml). You have to find the setting <use-mappings>false</use-mappings> and change it to true. Then restart your CF server. Until you've done this, calls to MG remoting from Flex applications will fail due a file not found. This only applies to Flex applications making remoting calls through the !FlashRemoting gateway. 127 121 128 122 1. For the application to work properly, it will need to understand the remoting destination and endpoints. There are a number of ways to handle this, this is the simplest. In the Flex project properties in Flex Builder, go to Flex Compiler and add the following to the Additional Compiler Arguments" … … 135 129 136 130 {{{ 137 #!xml138 131 <?xml version="1.0" encoding="utf-8"?> 139 132 <mx:Application … … 172 165 </mx:Application> 173 166 }}} 167 174 168 We define our Remote Object service with the "mx:RemoteObject" tag. In this tag, we called the standard "!ColdFusion" service defined in your remoting-config.xml in your flex folder on the !ColdFusion server. The source defines the specific cfc we will are calling which in with our MG3 app will always be the !RemotingService.cfc. When the result comes back, we have a function called "resultHandler()" which will process the data. 175 169 … … 185 179 186 180 == AJAX Example with jQuery == 181 187 182 jQuery is a popular Javascript library for building Rich Internet Applications. We're not going explain all the features of jQuery here, but this simple example will explain how it can be used to call the MG3 remoting service. As before, we'll go ahead and see the entire code and then go through it's operation. 188 183 189 184 The following code is a single html file. 190 185 {{{ 191 #!xml192 186 <script type="text/javascript" src="js/jquery.js"></script> 193 187
![(please configure the [header_logo] section in trac.ini)](/ModelGlue.com/trac.cgi/chrome/site/your_project_logo.png)