http://coldfusion.com logo
#box-products
Title
# box-products
d

dick

04/25/2022, 12:07 PM
Can I inject an object into another object and pass in the
init()
arguments? I've tried this without success...
property name="test" inject="<http://path.to|path.to>.component( a='a', b='b')";
s

sknowlton

04/25/2022, 12:57 PM
I don't believe you can use that syntax. You'd have to use
getInstance( name = 'component', initArguments = { a = 'foo', b = 'bar' } );
If you regularly used the component with those arguments you can do that in your
Wirebox.cfc
with a different syntax
where you first bind the component
But the fact that you have a variable constructor suggests it maybe isn't a singleton, in which case you probably don't want to be using
inject
anyway
a

aliaspooryorik

04/25/2022, 1:07 PM
I'd take a step back and ask: • is the object you are trying to inject into a singleton? • is the object you are trying to inject a transient?
d

dick

04/25/2022, 2:08 PM
This is other people's code which I'm trying to update. Neither are annotated as
singleton
so I guess they're transient. I'd forgotten about
getInstance()
and didn't know it took an
initArguments
argument. Let me try that. It looks neater that this which I get to work in the
init()
of the receiving object
Copy code
variables.test = createObject( "component", "path.to.component" ).init( a='a', b='b' );
s

sknowlton

04/25/2022, 2:10 PM
You do need to be careful doing that in the constructor because you can run into the chicken and egg problem where Wirebox may not have built
component
yet.
onDIComplete()
is usually a safe place to put something like that - but if it's not a transient and not a singleton, having a single one stashed in your
variables
scope seems ... not very transient
guess it depends on what the component you're working on does
a

aliaspooryorik

04/25/2022, 2:15 PM
yeah, if the injected component is a singleton, you should have it configured and ready to inject with those preset parameters. If you inject it in different places with different init arguments then it's a transient. For transient usage then
getInstance
in
onDIComplete
is the easiest way to do it.
d

dick

04/25/2022, 4:16 PM
Thank you both - I now have a neater solution with
property _name_="wirebox" _inject_="wirebox";
in the pseudo constructor and
Copy code
function onDIComplete() {
	variables.test = wirebox.getInstance( "path.to.component", { a: 'a', b: 'b' } );
}
2 Views