Injections

Daniel's Avatar

Daniel

28 Jan, 2010 08:08 PM

I've read through the knowledge base but am still a little fuzzy on how an implementation of the following would actually be coded.

Though it may not be best practice, how can an arbitrary view component be injected into a command? Lets say I want to display a popup in a command or change the state property on a view component.

` public class MyCommand extends Command

{       
    [Inject] public var app:MyApp;

    override public function execute() : void
    {           
        app.addChild( new MyPopupComponent() );

        // or

        app.currentState( 'ShowPopupState' );

    }
}

`

So the command above is mapped to an event, but how is the arbitrary view component mapped in this scenario?

  1. Support Staff 1 Posted by Joel Hooks on 28 Jan, 2010 08:12 PM

    Joel Hooks's Avatar

    I'd do this work in a Mediator. Either have the Command dispatch an event that triggers the action in the mediator, or intercept the event in the mediator to do the work on the view.

  2. 2 Posted by Daniel on 28 Jan, 2010 08:17 PM

    Daniel's Avatar

    Sure, this could be done in my application mediator and I know how to map the view component there. But how can a mediator listen for framework events?

  3. 3 Posted by Daniel on 28 Jan, 2010 08:23 PM

    Daniel's Avatar

    I'll answer my own question here...

    in the mediator's onRegister method I can map a listener on the framework's eventDispatcher object so

    eventMap.mapListener( eventDispatcher, SystemEvent.MY_EVENT, onMyEvent );

  4. Support Staff 4 Posted by Shaun Smith on 28 Jan, 2010 09:02 PM

    Shaun Smith's Avatar

    Although it's not a good idea to inject view components into commands, to answer your original question you'd need to map the view component for injection like this (perhaps in your context startup method):

    injector.mapValue(MyApp, app);
    

    Beware though: if a mediator is mapped to MyApp, then the injection will be overwritten when the mediator is constructed and the injection will no longer be available.

  5. Support Staff 5 Posted by Joel Hooks on 28 Jan, 2010 09:04 PM

    Joel Hooks's Avatar

    And remember, just because you CAN doesn't mean you should (as is the case with Shaun's example ;) )

  6. Joel Hooks closed this discussion on 28 Jan, 2010 09:04 PM.

  7. Daniel re-opened this discussion on 28 Jan, 2010 09:48 PM

  8. 6 Posted by Daniel on 28 Jan, 2010 09:48 PM

    Daniel's Avatar

    @shaun Thats what I initially tried in my context and it didn't work. I wasn't aware that mappings are overwritten---an important note to remember.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac