New to Coldbox - problem with event flow

Hi all,

I am new to Coldbox and still pretty green with regard to OO, so be
gentle. :slight_smile:

I am using Coldbox 3.0.0 and I have my General.cfc preHandler function
set up to create some objects. In the preHandler function itself I
make sure to do this:

<cfset var rc = event.getCollection()>

While I am in the preHandler function this works great. Now, from this
function I want to create an object, for instance:

<cfset rc.site = createObject("component","site.general").init()>

The site object instantiates just fine. Now I have a method in this
object that needs to call on the rc structure. When I first tried to
call that method I got a message saying that the variable "rc" doesn't
exist. So I naturally added in the code <cfset var rc =
event.getCollection()>.

But now I get this message: variable [EVENT] doesn't exist

I saw in the documentation about putting super.init code in the init
function of my cfc, but that didn't solve anything (and based on
further reading it seems that this may no longer even be necessary).

I also tried putting in an event argument as such:

<cfargument name="event" type="coldbox.system.beans.requestContext"
required="true">

(I also tried without the "type" attribute.)

But then get a message that the argument was required but doesn't
exist.

So I am not sure what I am doing wrong. Thanks in advance for any and
all help!

Gary

I'm not sure if I followed all of that, but to be clear, any locally varred variables that are set in preHandler will not be available in the main handler method. That's just how CFC scopes work. You will need the <cfset var rc = event.getCollection()> bit at the top of every handler.

Now, any methods in your site.general component will not have access to your rc variable or the event context unless you pass them in. The framework passes the event argument into all handlers, but if you want to reference it in your model, you need to keep passing it along.

So your general handler might have something like so:

<cffunction name="index">
    <cfargument name="event" type="coldbox.system.beans.requestContext" required="true">
    <cfset var rc = arguments.event.getCollection()>

    <cfset rc.site = createObject("component","site.general").init()>
    <cfset rc.site.someMethod(arguments.event)> <!--- This method can deal with the request context --->
    <cfset rc.site.anotherMethod(rc)> <!--- This method can deal with the request collection --->
    ...
</cffunction>

Also, for what it's worth, I don't usually let my model deal with the request context (event) or request collection directly. The handler is the go-between and it hands the model the peices it needs.

Good luck.

Thanks!

~Brad

Is the event being passed in by the arguments?

Regards,
Andrew Scott
http://www.andyscott.id.au/

From: coldbox@googlegroups.com [mailto:coldbox@googlegroups.com] On
Behalf Of Gary
Sent: Thursday, 11 November 2010 9:16 AM
To: ColdBox Platform
Subject: [coldbox:6711] New to Coldbox - problem with event flow

Hi all,

I am new to Coldbox and still pretty green with regard to OO, so be

gentle. :slight_smile:

I am using Coldbox 3.0.0 and I have my General.cfc preHandler function set
up to create some objects. In the preHandler function itself I make sure

to do

this:

<cfset var rc = event.getCollection()>

While I am in the preHandler function this works great. Now, from this
function I want to create an object, for instance:

<cfset rc.site = createObject("component","site.general").init()>

The site object instantiates just fine. Now I have a method in this object

that

needs to call on the rc structure. When I first tried to call that method

I got a

message saying that the variable "rc" doesn't exist. So I naturally added

in the

code <cfset var rc = event.getCollection()>.

But now I get this message: variable [EVENT] doesn't exist

I saw in the documentation about putting super.init code in the init

function

I figured out what I was doing wrong. I was mistakenly using (or
trying to use) these setup functions as events, which they aren't.

What I had to do was pass in the rc structure, and then have that
structure be an argument in the function being called.

Thanks Brad and Andrew - your comments were very helpful and helped me
figure out what I was doing wrong.

Gary

I should also point out that it is very well ok to send the event across
instead, in fact in some case it makes it easier and less congested when
dealing with many vars.

Regards,
Andrew Scott
http://www.andyscott.id.au/

From: coldbox@googlegroups.com [mailto:coldbox@googlegroups.com] On
Behalf Of Gary
Sent: Thursday, 11 November 2010 12:42 PM
To: ColdBox Platform
Subject: [coldbox:6717] Re: New to Coldbox - problem with event flow

I figured out what I was doing wrong. I was mistakenly using (or trying to

use)

these setup functions as events, which they aren't.

What I had to do was pass in the rc structure, and then have that

structure be