New Model Glue 3 Feature: copyToScope()

We've just finished adding another feature to Model-Glue 3 called copyToScope(). This feature makes it easier to pull variables out of the event scope and place them into another scope. A big thanks to Paul Marcotte for suggesting this feature.

How does it work?

Say you want to pull 3 values from the event and use them in your view. Normally, you would run 3 separate commands:

<cfset foot = event.getValue("foot") />
<cfset hand = event.getValue("hand") />
<cfset xe.nested = event.getValue("xe.nested") />
the end result being that each value: foot,hand,xe.nested is now in the variables scope ready for use in your view.

Here 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.

<cfset event.copyToScope(variables, "foot,hand,xe.nested") />

The 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.

What about default values?

As 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:

<cfset defaultArray = ["DefaultForFoot", "DefaultForHand"] />
<cfset event.copyToScope(variables, "foot,hand,xe.nested", defaultArray) />

Once 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.

Does it only work with the variables scope?

Nope. 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.

Where can I get the code?

You can update from SVN at and try the feature out. Several bugs have also been fixed including a bug with Railo compatibility submitted by Denny Valliant. Thanks to Denny for his contribution.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
nice one, thanks!
# Posted By Chris H | 9/9/08 9:20 AM
Nice update, really cuts down on code when you have a lot vars to deal with.
# Posted By Robb | 9/9/08 11:51 AM
hi Joe
1) what baout copyomg event values in local var scope?
2) what about finail release date of gesture?
# Posted By salvatore fusto | 9/15/08 2:33 AM
© 2021 Joe Rinehart
BlogCFC was created by Raymond Camden. This blog is running version