Changes between Initial Version and Version 1 of QuickStart/2:ModellingourApplication

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/2:ModellingourApplication

    v1 v1  
     1= Quickstart 2: Modeling our Application = 
     2 
     3The "Model" for our application will consist of a single CFC that translates a phrase into, well, something else. In this case, we'll start with pig latin. To create a CFC-based Model for our application, do the following: 
     4 
     51. Create a file in /translator/model called !PigLatinTranslator.cfc 
     6 
     72. Open the file, and paste in the following code. You'll see that it's a simple CFC with a constructor function named "init" that receives what letters are considered vowels. It only has one other function, "translate," which changes a phrase into Pig Latin. 
     8 
     9{{{ 
     10<cfcomponent name="PigLatinTranslator" output="false"> 
     11 
     12<cffunction name="init" returntype="any" access="public" output="false"> 
     13 
     14<cfargument name="vowels" type="string" required="true" /> 
     15 
     16<cfset variables.vowels = arguments.vowels /> 
     17 
     18<cfreturn this /> 
     19 
     20</cffunction> 
     21 
     22<cffunction name="translate" returntype="string" access="public" output="false"> 
     23 
     24<cfargument name="phrase" /> 
     25 
     26<cfset var firstVowel = reFindNoCase("[#variables.vowels#]", arguments.phrase) - 1/> 
     27 
     28<cfset var result = trim(arguments.phrase) /> 
     29 
     30<!--- We started with a consonant ---> 
     31 
     32<cfif len(result) and firstVowel gt 0> 
     33 
     34<cfset result = right(arguments.phrase, len(arguments.phrase) - firstVowel) & left(arguments.phrase, firstVowel) & "ay" /> 
     35 
     36<!--- We started with a vowel ---> 
     37 
     38<cfelseif len(result)> 
     39 
     40<cfset result = result & "ay" /> 
     41 
     42</cfif> 
     43 
     44<cfreturn result /> 
     45 
     46</cffunction> 
     47 
     48</cfcomponent> 
     49}}} 
     50 
     51And that's it. Our entire Model. It doesn't know anything about a user interface, or form variables, or session, or any of that stuff, so it's very, very reusable.