ColdBox: Using 'Persist'
Posted on June 10, 2008 at 5:48 PM in ColdBox, ColdFusion
Back in the ColdBox 2.5.1 release, a new feature was added: persist. Think of it as a sort of "flash memory", allowing your application to "remember" event variables when moving the user to the next event. I was recently asked about this feature, so I figured I would make a quick post about it in an effort to help more people.
In your event handlers (controllers), it is often necessary to send the request along to another event. ColdBox provides two methods for doing this in your handlers:
- setNextEvent() - for use with standard URLs
- setNextRoute() - for use with SES URLs
In the docs, you'll see that there is an optional argument for these methods named 'persist'. Providing a comma-delimited list of variable names to this argument will assure you that the variables are carried over to the next event. It's very simple...
- // for standard URLs
- setNextEvent(
- event: "my.event",
- persist: "variable1,variable2,variable3"
- );
- //
- // for SES URLs
- setNextRoute(
- route: "my/event",
- persist: "variable1,variable2,variable3"
- );
In this example, variable1, variable2, and variable3 will still exist in my.event when the request gets there. Schweeeeet!! But, there is one MAJOR point to make, so pay really close attention here...
In order to persist variables between events, they *must* exist in the event object, or request collection.
In other words, these are *not* var'd variables within the event method.
- <cffunction name="doSomething">
- <!--- set the method arguments --->
- <cfargument name="Event" type="coldbox.system.beans.requestContext" />
- <cfscript>
- var foo = "bar";
- setNextRoute(
- route: "foo/bar2",
- persist: "foo"
- );
- </cfscript>
- </cffunction>
In the example above, we're telling setNextRoute() to persist an event variable named 'foo'. As you can see, 'foo' is a var'd variable within the event method. This example will not result in 'foo' existing when the request gets to the next event.
The 'persist' argument is valid for event variables only, as shown in the next example.
- <cffunction name="doSomething">
- <!--- set the method arguments --->
- <cfargument name="Event" type="coldbox.system.beans.requestContext" />
- <cfscript>
- var foo = "bar";
- event.setValue("foo", foo);
- setNextRoute(
- route: "foo/bar2",
- persist: "foo"
- );
- </cfscript>
- </cffunction>
As you can see in this example, 'foo' exists in the event itself, so it will be passed along to the next event.
Just wanted to pass that along, since it seems to be a common misconception.
Latest Articles
- No recent entries.
Categories
- ColdBox (21) [RSS]
- ColdFusion (92) [RSS]
- Fusebox (3) [RSS]
- General (22) [RSS]
- jQuery (15) [RSS]
- Kalendar (1) [RSS]
- Linux (1) [RSS]
- Mura CMS (1) [RSS]
- Railo (1) [RSS]
- Rants (5) [RSS]
- Transfer (8) [RSS]
- Uni-Form Tag Library (36) [RSS]
Quick Links
Blogs I Read
Calendar
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
« Aug | ||||||
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Subscribe
Enter a valid email address.
On 6/10/08 at 7:33 PM, Sana said:
just one setting to change then framework will manage every thing for you.
<Setting name="FlashURLPersistScope" value="client" />
more info on coldbox wiki
http://ortus.svnrepository.com/coldbox/trac.cgi/wi...
On 6/17/08 at 9:23 AM, Dave Shuck said:
On 6/17/08 at 9:30 AM, Dave Shuck said:
'...it comes in handy' OR '...is quite useful'
Your choice! :)
On 10/21/09 at 5:47 PM, Jonathan said:
<cffunction name="doSomething">
<!--- set the method arguments --->
<cfargument name="Event" type="coldbox.system.beans.requestContext" />
<cfscript>
var persist = structnew();
var foo = "bar";
persist.foome=event.getvalue('foome');
persist.fooyou=event.getvalue('fooyou');
setNextEvent( event="foo/bar2",varStruct="persist");
</cfscript>
</cffunction>
On 10/21/09 at 6:15 PM, Matt Quackenbush said:
setNextEvent(event="foo.bar2",varStruct=persist);
On 10/21/09 at 7:17 PM, Jonathan said:
On 10/21/09 at 7:32 PM, Matt Quackenbush said:
On 10/21/09 at 9:09 PM, Jonathan said:
<cffunction name="doThis">
<cfargument name="Event" type="coldbox.system.beans.requestContext" />
<cfscript>
var foo = "bar";
event.setValue("foo", foo);
setNextEvent(
event: "league.didit",
persist: "foo"
);
</cfscript>
</cffunction>
<cffunction name="didIt">
<cfargument name="Event" type="coldbox.system.beans.requestContext" />
<cfscript>
event.setView("myView");
</cfscript>
</cffunction>
myView just does a <cfdump var="#session#"> I should see a var named foo that contains bar correct?
On 10/21/09 at 9:16 PM, Matt Quackenbush said:
On 10/21/09 at 9:48 PM, Jonathan said:
On 10/21/09 at 9:51 PM, Matt Quackenbush said:
On 11/4/12 at 9:36 PM, Adam Cameron said:
Thanks for this blog post. I was having difficulty getting this working, and your post here put me in the right direction (the problem was me being daft in the end, but going over what you'd said made me spot where I was being daft).
Thanks!
I'm making slow progress with Coldbox so far, but am also pleased with the number of "aha!" moments I am having as I progress, too.
Cool.
--
Adam