Changes between Initial Version and Version 1 of QuickStart/4:HandlingaForm

Show
Ignore:
Timestamp:
04/15/09 19:43:30 (17 years ago)
Author:
trac (IP: 127.0.0.1)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • QuickStart/4:HandlingaForm

    v1 v1  
     1= Quickstart 4: Handling a Form = 
     2 
     3Ok, we've built a form. It submits to an event handler named "translationFormAction." Now, we need to create that event. 
     4 
     5Let's do it now. Add the following new event handler XML to /translator/config/!ModelGlue.xml: 
     6 
     7{{{ 
     8<event-handler name="translationFormAction"> 
     9 
     10</event-handler> 
     11}}} 
     12 
     13Ok, fire up a browser, go to http://localhost/translator/index.cfm?event=transationForm, fill in a phrase, and click Ok. 
     14 
     15Not much happened, eh? 
     16 
     17Obviously, we need to tell the application to translate the phrase that's been entered. Our first step is to add a message to the translationFormAction. A message is like a town crier: it's the event handler's way of saying "I need the phrase to be translated!" The event handler itself doesn't care who does the translation. 
     18 
     19Let's add the message: 
     20 
     21{{{ 
     22<event-handler name="translationFormAction"> 
     23 
     24<broadcasts> 
     25 
     26<message name="NeedTranslation" /> 
     27 
     28</broadcasts> 
     29 
     30</event-handler> 
     31}}} 
     32 
     33Try the form again if you like. Still nothing. That's because the event handler can ask for translations until it's blue in the face (or view), but nothing's going to happen unless something ''listens'' to the message. 
     34 
     35At the top of !ModelGlue.xml, there's a <controllers> block we haven't talked about yet. It declares a list of <controllers>, which in turn contain a list of <message-listeners>. 
     36 
     37It sounds complicated, but it isn't. 
     38 
     39You'll see that each <controller> tag has a TYPE attribute. That attribute points to a CFC known as a "Controller CFC". When Model-Glue starts, it creates an instance of each Controller CFC listed. 
     40 
     41The <message-listener> tags simply tell the controller "Whenever an event handler broadcasts a message named MESSAGE, execute your function named FUNCTION." 
     42 
     43So, to get something to translate, we need to tell our Controller to listen for the !NeedTranslation message. Add a new <message-listener> tag to the default <controller> block, making it look like this: 
     44 
     45{{{ 
     46<controller name="MyController" type="modelglueapplicationtemplate.controller.Controller"> 
     47 
     48<message-listener message="OnRequestStart" function="OnRequestStart" /> 
     49 
     50<message-listener message="OnRequestEnd" function="OnRequestEnd" /> 
     51 
     52<message-listener message="NeedTranslation" function="TranslatePhrase" /> 
     53 
     54</controller> 
     55}}} 
     56 
     57Ok, now try the form. Again. 
     58 
     59Still nothing? There's one last piece to the puzzle: write a !TranslatePhrase function in the Controller that asks our Model to translate a phrase! That's easy enough - just open /translator/controller/Controller.cfc and add the following <cffunction>: 
     60 
     61{{{ 
     62<cffunction name="TranslatePhrase" access="public" returntype="void" output="false"> 
     63 
     64<cfargument name="event" type="any"> 
     65 
     66<cfset var translator = createObject("component", "translator.model.PigLatinTranslator").init("aeiou") /> 
     67 
     68<cfset var phrase = arguments.event.getValue("phrase") /> 
     69 
     70<cfset var result = translator.translate(phrase) /> 
     71 
     72<cfset arguments.event.trace("TranslatePhrase Results", result) /> 
     73 
     74<cfset arguments.event.setValue("translatedPhrase", result) /> 
     75 
     76</cffunction> 
     77}}} 
     78 
     79So what does all that do? 
     80 
     81First, arguments.event is like the twin sibling of the viewstate variable in a view. It has a getValue() method to get variables from the FORM and URL scopes. However, it also has a setValue() method that can set values into the viewstate, readable in views or later message listener functions. 
     82 
     83Second, it creates an instance of our application's Model CFC: 
     84 
     85{{{ 
     86<cfset var translator = createObject("component", "translator.model.PigLatinTranslator").init("aeiou") /> 
     87}}} 
     88 
     89Third, it gets the value of FORM.phrase 
     90 
     91{{{ 
     92<cfset var phrase = arguments.event.getValue("phrase") /> 
     93}}} 
     94 
     95Fourth, it asks the model to translate the phrase: 
     96 
     97{{{ 
     98<cfset var result = translator.translate(phrase) /> 
     99}}} 
     100 
     101Fifth, it adds the results of the translation to the Model-Glue debugging output, so that we can see that it's been translated properly: 
     102 
     103{{{ 
     104<cfset arguments.event.trace("TranslatePhrase Results", result) /> 
     105}}} 
     106 
     107Last, it sets the results of the translation into the viewstate so that our view templates can display the data: 
     108 
     109{{{ 
     110<cfset arguments.event.setValue("translatedPhrase", result) /> 
     111}}} 
     112 
     113Ok, finally, run the form! Seriously, something will happen this time! You won't get much, but you'll see a line in the Model-Glue debugging trace labelled "!TranslatePhrase Results" that shows the translated phrase. 
     114 
     115As a last step, let's add a view that shows our translated phrase. That'd probably make users happy, because we'll turn the debugging trace off when we deploy this application. 
     116 
     117Following the same steps as we did when we built the form, first add a dspPhrase.cfm file to /translator/views. You'll notice that we again use an eXit Event, this time to build a link back to the translation form. To the link, we append the "phrase" value. If you look back at frmPhrase.cfm, the VALUE attribute of our <cfinput> tag was set to viewstate.getValue("phrase"). getValue(), by default, returns an empty string if a value is not defined. By appending the phrase to the URL, we can set the default value of the input box. 
     118 
     119{{{ 
     120<cfset translationForm = viewstate.getValue("myself") & viewstate.getValue("xe.translationForm") & "&phrase=" & viewstate.getValue("phrase") /> 
     121 
     122<cfset translatedPhrase = viewstate.getValue("translatedPhrase") /> 
     123 
     124<cfoutput> 
     125 
     126<p>Translated Phrase: #translatedPhrase#</p> 
     127 
     128<p><a href="#translationForm#">Translate Again.</a></p> 
     129 
     130</cfoutput> 
     131}}} 
     132 
     133Second, tell the event handler to <include> the view: 
     134 
     135{{{ 
     136<event-handler name="translationFormAction"> 
     137 
     138<broadcasts> 
     139 
     140<message name="NeedTranslation" /> 
     141 
     142</broadcasts> 
     143 
     144<views> 
     145 
     146<include name="body" template="dspPhrase.cfm"> 
     147 
     148<value name="xe.translationForm" value="translationForm" /> 
     149 
     150</include> 
     151 
     152</views> 
     153 
     154</event-handler> 
     155}}} 
     156 
     157Wow. You now know about half of Model-Glue. There's really only another quarter or so that's fundamental knowledge, and the remaining bit after that is just icing on the cake that you'll use every now and then.