| | 1 | [[TOC(heading=Training Section Contents, Training*)]] |
| | 2 | |
| | 3 | = Section 5: Mother's Little Helper = |
| | 4 | |
| | 5 | Part of a quality design is in not repeating the prescription of algorithms. Sure we can encapsulate our algorithm in a User Defined Function (UDF), but in a Model-Glue world, where do UDFs go? |
| | 6 | |
| | 7 | Model-Glue 3 introduces the concept of helpers. Helpers are libraries of user defined functions. By placing these files in the proper location, Model-Glue 3 will automatically load them and make them accessible via the new helpers scope, available to both controller and view files. Let's implement a helper in our application. |
| | 8 | |
| | 9 | 1. Open /PlantOMatic/config/ColdSpring.xml, find the definition for modelglue.modelGlueConfiguration and ensure you have a helper mapping pointing to /PlantOMatic/helpers: |
| | 10 | |
| | 11 | {{{ |
| | 12 | |
| | 13 | <property name="helperMappings"><value>/PlantOMatic/helpers</value></property> |
| | 14 | |
| | 15 | }}} |
| | 16 | |
| | 17 | 2. Create a new file called date.cfm inside /PlantOMatic/helpers |
| | 18 | 3. Place the following function inside /PlantOMatic/helpers/date.cfm |
| | 19 | |
| | 20 | {{{ |
| | 21 | |
| | 22 | <cfscript> |
| | 23 | /* |
| | 24 | * Calculates the number of business days between 2 dates. |
| | 25 | * @param date1 First date. (Required) |
| | 26 | * @param date2 Second date. (Required) |
| | 27 | * @return Returns a number. |
| | 28 | * @author Harry Goldman (harry@icn.net) |
| | 29 | * @version 1, April 10, 2008 |
| | 30 | */ |
| | 31 | |
| | 32 | function businessDaysBetween( date1, date2 ) { |
| | 33 | var numberOfDays = 0; |
| | 34 | |
| | 35 | while ( date1 LT date2 ) { |
| | 36 | date1 = dateAdd( "d", 1, date1 ); |
| | 37 | if ( dayOfWeek( date1 ) GTE 2 AND dayOfWeek( date1 ) LTE 6 ) |
| | 38 | numberOfDays = incrementValue( numberOfDays ); |
| | 39 | } |
| | 40 | return numberOfDays; |
| | 41 | } |
| | 42 | </cfscript> |
| | 43 | |
| | 44 | }}} |
| | 45 | |
| | 46 | 4. Open the file /PlantOMatic/views/List.Shipment.cfm and change the following line: |
| | 47 | |
| | 48 | {{{ |
| | 49 | |
| | 50 | <td>#dateFormat( CreateDate, "short" )#</td> |
| | 51 | |
| | 52 | }}} |
| | 53 | |
| | 54 | To call our new UDF from the helpers scope: |
| | 55 | |
| | 56 | {{{ |
| | 57 | |
| | 58 | <td>#dateFormat( CreateDate, "short" )# #helpers.date.businessDaysBetween( CreateDate, now() )# (Business days ago)</td> |
| | 59 | |
| | 60 | }}} |
| | 61 | |
| | 62 | |
| | 63 | 5. Now, click the Home navigation element again to run the Saved Shipments screen and see the new helper in action: |
| | 64 | |
| | 65 | [[Image(training05-1.png, nolink)]][[BR]] |
| | 66 | '' Date displayed with helper function '' |
| | 67 | |
| | 68 | Helpers are a helpful (''editors note: remove lame pun'') way to organize your User Defined Functions and share them across views and controllers. For more information on Model-Glue Helpers, visit the following documentation: |
| | 69 | |
| | 70 | |
| | 71 | [http://docs.model-glue.com/wiki/HowTos/HowToUseHelpers How To Use Helpers] |
| | 72 | |
| | 73 | ---- |
| | 74 | |
| | 75 | Back: [wiki:Training/Section04 Training Section 4: Request Formats Are Your Friend] |
| | 76 | |
| | 77 | Next: [wiki:Training/Section06 Training Section 6: Flow and Dependencies] |