How to inject a bean in a view with Crux

33 Views Asked by At

From the Apache Royale documentation about Crux, it seems a bean can be injected into any view added to the display list.

Yet, when I try to have a bean injected in a view other than the Application itself, nothing happens and I have manually set my bean all the way down from an event handler located in my Application class (where injection and event handling works as expected).

Here is an example of a view I'm trying to use :

package views
{
    import events.MyEvent;
    import models.MyModel;

    import org.apache.royale.events.Event;
    import org.apache.royale.jewel.ScrollableSectionContent;

    public class MyView extends ScrollableSectionContent
    {
        [Bindable]
        [Inject(source="myModel")]
        public var myModel:MyModel;

        public function MyView():void
        {
            super();
            addEventListener("addedToStage", handleAddedToStageEvent);
        }

        [PostConstruct]
        public function setUp():void
        {
            // This is never called
        }

        [EventHandler(event="MyEvent.EVENT_TYPE", properties="data")]
        public function handleEvent(data:*):void
        {
            // Event is not handled here when fired
        }

        private function addedToStage(e:Event):void
        {
            // Get called whenever view is added to the display list
            // but myModel remains null
        }
    }
}

And here is my Crux configuration in my Application :

<beads>
    <crux:JSStageEvents packageExclusionFilter="_default_"/>
    <crux:Crux>
        <crux:beanProviders>
            <config:Beans/>
        </crux:beanProviders>
        <crux:config>
            <crux:CruxConfig eventPackages="events.*"
                             viewPackages="views.*"/>
        </crux:config>
    </crux:Crux>
</beads>

Is there anything I should be aware of for injection to work in a view ?

1

There are 1 best solutions below

1
Greg Dove On

Is your MyView class somewhere inside the top level package 'views' ? If yes, then there may be another reason that it's not working that is not clear to me. But if it is not in that package (somewhere at or below top level 'views' package), then that may be the reason. The CruxConfig shown only specifies view classes inside 'views' as valid view classes to inject into. You should be able to add comma delimited list in here if you have other packages to include for filtering view classes.