robotlegs / robotlegs-framework

An ActionScript 3 application framework for Flash and Flex
https://robotlegs.tenderapp.com/
MIT License
967 stars 261 forks source link

View Injection Hidden Feature #180

Open prankard opened 8 years ago

prankard commented 8 years ago

Currently there is a hidden feature due to this line in MediatorFactory.as:

mapTypeForFilterBinding(mapping.matcher, type, item);
mediator = createMediator(item, mapping);
unmapTypeForFilterBinding(mapping.matcher, type, item)

Which I found whilst trying to modify the code for another feature.

So the ViewComponent get's mapped for the mediator. But it's actaully exposed to the entire injector for a bit longer than expected. So you can inject the view component whilst in the 'preInitialize', 'initialize' and 'postInitialize' phase.

For example you can do the following which should cause errors when injecting.

class ExampleConfig
{
    [Inject]
    public mediatorMap:IMediatorMap;

    [Inject]
    public commandMap:IEventCommandMap;

    [Inject]
    public injector:IInjector;

    public function config():void
    {
        mediatorMap.map(ExampleView).toMediator(ExampleMediator);
        commandMap.map(ExampleEvent.TRIGGER_COMMAND).toCommand(ExampleCommand);
        injector.map(IExampleService).toType(ExampleService);
    }
}
class ExampleMediator
{
    public function initialize():void
    {
        dispatchEvent(new ExampleEvent(ExampleEvent.TRIGGER_COMMAND));
    }
}
class ExampleCommand
{
    [Inject]
    public service:IExampleService;

    [Inject]
    public viewComponent:ExampleView;

    public function execute():void
    {
        trace(viewComponent + " is not null");
    }
}
class ExampleService implements IExampleService
{
    [Inject]
    public viewComponent:ExampleView;

    [PostConstruct]
    public function init():void
    {
        trace(viewComponent + " is not null");
    }
}

Let me know if this is a hidden feature that isn't documented, or a bug and I'll fix :godmode: