Search code examples
apache-flexdependency-injectioninjectparsley

FastInject not detecting objectId in Parsley


I have just started using Parsley recently and I ran into this issue. The thing is I have a custom component in my project, which is "configured" by Parsley and has a piece of code as follows:

<fx:Script>
<![CDATA[
            ...
            [Inject(id="dateFormatter")]
            [Bindable] public var dateFormatter:DateFormatter;
            ...
]]>
</fx:Script>

<fx:Declarations>
<parsley:Configure />
</fx:Declarations>

My problem is that I don't want Parsley to configure the component entirely. I want to simply use FastInject in MXML, instead of using Configure, like:

<parsley:FastInject objectId="dateFormatter" property="dateFormatter" type="{DateFormatter}" />

From what I found when I searched online, the objectId in FastInject is the same as [Inject(id="dateFormatter")]. Here's the source for that. Please correct me if I am wrong :).

But when I use it, I hit the following error: Error: More than one object of type mx.formatters::DateFormatter was registered

Does this mean that the ID of the property being injected is not being picked up? It works fine when I configure the whole component and use the Inject meta-tag, but I don't want to configure the whole component.

Can someone suggest a solution?


Solution

  • FastInject by id works if objects declared in the context have an id.

    Context configuration

    <fx:Declarations>
        <foo:FooBar1 />
        <foo:FooBar2 id="fooBar2" />
    </fx:Declarations>
    

    FastInject in your component

    <fx:Declarations>
        <parsley:FastInject injectionComplete="handlerInjectComplete(event)">
            <parsley:Inject property="foobar1" type="{FooBar1}" />
            <parsley:Inject property="foobar2" objectId="fooBar2"/>
        </parsley:FastInject>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            [Bindable]
            public var foobar1:FooBar1;
            [Bindable]
            public var foobar2:FooBar2;
    
            protected function handlerInjectComplete(event:Event):void
            {
                if(foobar1) trace("foobar1 available");
                if(foobar2) trace("foobar2 available");
        }
        ]]>
    </fx:Script>
    

    This works for me.