0

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?

Shiva
  • 43
  • 7

2 Answers2

1

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.

dvdgsng
  • 1,691
  • 16
  • 27
  • Thanks `dgesang`.. That worked for me :)!! My mistake was that I was using both `objectId` and `type` in the declaration!! – Shiva Apr 04 '13 at 13:50
0

Parsley FastInject gets confused when you inherit B from class A and want to inject both by id, while specifying type.

You need to use only one of objectId / type attributes of FastInject

jediz
  • 4,459
  • 5
  • 36
  • 41
  • Hey jediz.. Thanks for replying, but could you tell me on what circumstances do I use `objectID` and `type`? – Shiva Nov 21 '13 at 06:29
  • It is a good practice to inject by type when possible. That way you don't have to make up any objectId names and it makes code cleaner. You should only use injection by id if you have multiple classes (which can also be a case of subclasses) of the same type defined in your Parsley Context. – jediz Nov 22 '13 at 14:43