Changes between Initial Version and Version 1 of HowTos/HowToUseEventCopyToScope

Show
Ignore:
Timestamp:
11/16/09 23:19:44 (16 years ago)
Author:
cfgrok (IP: 64.30.223.5)
Comment:

Renamed page for alpha sorting on home page

Legend:

Unmodified
Added
Removed
Modified
  • HowTos/HowToUseEventCopyToScope

    v1 v1  
     1= Event.copyToScope() = 
     2 
     3This feature makes it easier to pull variables out of the event scope and place them into another scope. 
     4 
     5Say you want to pull 3 values from the event and use them in your view. Normally, you would run 3 separate commands: 
     6 
     7{{{ 
     8    <cfset foot = event.getValue("foot") /> 
     9    <cfset hand = event.getValue("hand") /> 
     10    <cfset xe.nested = event.getValue("xe.nested") /> 
     11}}} 
     12 
     13the end result being that each value: foot,hand,xe.nested is now in the variables scope ready for use in your view. 
     14 
     15Here in the labs of Model-Glue, we want to reduce developer excise when possible. The new copyToScope() method can be used to short cut the same process. 
     16 
     17{{{ 
     18    <cfset event.copyToScope(variables, "foot,hand,xe.nested") /> 
     19}}} 
     20 
     21The end result is the exact same as above, except only 1 line of code is needed to pull several values from the event at once. 
     22 
     23What about default values? 
     24 
     25As event.getValue() takes an optional argument to set default values, our new friend event.copyToScope() will take an optional array of default values. The position of the list elements pertain to the position of the value in the array. This means the 1st value in the list gets the 1st position in the array, the 2nd gets the 2nd and so on. Let's look at an example where I want to provide defaults to 2 items in the list: 
     26 
     27{{{ 
     28    <cfset defaultArray = ["DefaultForFoot", "DefaultForHand"] /> 
     29    <cfset event.copyToScope(variables, "foot,hand,xe.nested", defaultArray) /> 
     30}}} 
     31 
     32Once the function executes, the value of variables.foot is !DefaultForFoot and the value for variables.hand is !DefaultForHand. If no value existed in the event for xe.nested, it will be set to an empty string as per normal when a value is requested from the event that has not been set yet. 
     33 
     34Does it only work with the variables scope? 
     35 
     36Nope. It will work with any scope you see fit. Session, Server, Application, even a plain ole struct, though I expect the variables scope will be most often used.