I am new to Coldbox and still pretty green with regard to OO, so be
gentle.
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:
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:
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:
<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.
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.