Not quite sure that is what I mean... I have several objects extending
a base object.I need a reference to a datasource bean in all of them.
Instead of using <cfproperty name="dsn"
type="coldbox:datasource:myDsn" scope="Variables"> in _every_ object I
figured I could add
this line in the base object (the one that all objects are extending).
This should make the dsn bean accessible in all my objects.
You are doing it ok but you are not using “GETMODEL()” method. That is how coldbox puts everything together. You can put 1000 cfproperties with their specific annotations but you will not get any dependency injection if you do not go through the factory to request the object. You cannot do createObject() and expect ColdBox to autowire it for you. That would mean that the DI framework is doing byte weaving at the JVM level, and we are not doing that.
Please refer to the modelintegration guide, and try to retrieve the objects, via GetModel(). That method in turn calls the beanFactory plugin.
Thanks Luis for the answer. I didn't realize the GetModel() function
was required in order to get the CB functionality.
I replaced the CreateObject("component",myObject).init() for GetModel
("#myObject#").init() (#myObject# resolves to a model name set via
addModelMapping()).
I get a ColdBox error
"Error Messages: Variable GETMODEL is undefined.".
I figure this makes sense since this object does not extend
coldbox.system.eventHandler.
Let me be clear and summarize what I want to do:
<!--- initialize the main component --->
<cfcomponent name="main" extends="coldbox.system.eventhandler"
output="false" autowire="true">
<cffunction name="onApplicationStart" access="public"
returntype="void" output="false">
<!--- I want to use the CB Object Caching Model hence the call to
getColdboxOCM--->
<!--- old code, see my first post and Luis' comment
<cfset myVar=CreateObject("component",myObject).init()>
--->
<cfset getColdboxOCM().set("oCms",getModel('cms').initcomponent())>
...more code here...
</cffunction>
</cfcomponent>
<!--- have the main component initializer child components --->
<cfcomponent displayname="cms" output="false">
<cffunction name="initcomponent" access="public">
<cfset VARIABLES.contentTypes= {}>
<cfset types="graphic,text,video">
<cfloop list="#types#" index="contentType">
<!--- #contentType# refers to an object and is set in config/
modelmappings.cfm --->
<cfset getColdboxOCM().set("#contentType#",GetModel
("#contentType#").init())>
<!--- In component #contentType# I use the parent component's
(base.cfc) init method where I want to use the cfproperty tag - see my
start post --->
</cfloop>
<cfreturn this>
</cffunction>
</cfcomponent>
getColdboxOCM() gives me the CB error "Variable GETCOLDBOXOCM is
undefined." because I don't extend component cms from
coldbox.system.eventHandler. So I still cannot use GetModel() here.
I read the Model Integration Guide but can not find an example where
component B is instantiated using GetModel() in component A that _does
not extend the coldbox.system.eventhandler class_.
Does that mean I have to extend this model "cms" with the
coldbox.system.eventhandler class??
don’t do inheritance just to bring in external functionality that does not belong there.
If you want to be able to “create” and use OTHER model objects within model objects, you need to inject the beanfactory plugin itself. In order to do this. Now, what I would do is setup a “baseModel” object with certain features and methods to provide me all the functionality I need to talk to coldbox model objects. Example:
<cffunction name="initComponents" access="private" returntype="void"
output="false">
<cfset VARIABLES.contentTypes= {}>
<cfset types="graphic,text,video">
<cfloop list="#types#" index="contentType">
<!--- the reference to the object is set in config/modelmappings.cfm
--->
<cfset getColdboxOCM().set("#contentType#",GetModel
("#contentType#").init(dsn=dsn))>
</cfloop>
</cffunction>
The dsn bean is known in the initComponents function (I can dump it)
but when passed as an argument to init() I get a CB error
"Error Messages: The DSN parameter to the init function is required
but was not passed in." on line 187 of D:\www\wwwroot\coldbox\system
\plugins\beanFactory.cfc
I would suggest that just relax, have a coffee and read model guide, its really powerful concepts but require some attention before firing off any code.
Hopefully once you read the guide then you will understand the magic of coldbox model stuff.
NO SCOPE THEN
If you don't specify any scope then ColdBox default scope is "instance"
Hi Sana, Thanks for you answer and patience! I read this in the MIG:
CFPROPERTY
* name : The name of the property to be injected
* type : The type of property to inject (see chart)
* scope : Into which scope to inject the object/setting to.
Defaults to variables scoppe
NO SCOPE THEN
If you don't specify any scope then ColdBox default scope is "instance"
Hi Sana, Thanks for you answer and patience! I read this in the MIG:
CFPROPERTY
* name : The name of the property to be injected
* type : The type of property to inject (see chart)
* scope : Into which scope to inject the object/setting to.
Defaults to variables scoppe