Skip to content

View Injection Hidden Feature #180

@prankard

Description

@prankard

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions