Dispatch signal from itemrenderer?

christomanos's Avatar

christomanos

Oct 08, 2010 @ 03:02 PM

Hi all,

I have a list with an itemrenderer who has edit/delete buttons for each list item.

When I click this button I want to dispatch a as3-signal mapped with signalCommandMap to a command and the execute a service and so on.

I've created a custom signal class called RemoveItemSignal and in the itemrender I created a public var removed:RemoveItemSignal and then I dispatch it but nothing happens...

How should i code this?

  1. Support Staff 1 Posted by Shaun Smith on Oct 08, 2010 @ 03:17 PM

    Shaun Smith's Avatar

    Hi there,

    How are you providing the RemoveItemSignal reference to your item renderer instances?

  2. 2 Posted by christomanos on Oct 08, 2010 @ 03:21 PM

    christomanos's Avatar

    Hello,

    In my item renderer I have

    [Inject] public var removeItem:RemoveItemSignal (should I make a new instance?)

    and in my context

    injector.mapSingleton(RemoveItemSignal);
    signalCommandMap.mapSignalClass(RemoveItemSignal,RemoveItemCommand);

    Actually I get
    Cannot access a property or method of a null object reference.

    But should I write
    public var removeItem:RemoveItemSignal = new RemoveItemSignal?

  3. Support Staff 3 Posted by Shaun Smith on Oct 08, 2010 @ 04:37 PM

    Shaun Smith's Avatar

    Aha! Robotlegs, and all other DI frameworks, can only inject into things that they know about. RL doesn't know about the item renderers because they are created by your flex List (or DataGroup) component on-the-fly using an mx.core.IFactory instance:

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/...

    There are many different approaches to injecting dependencies (like your RemoveItemSignal) into view components.

    One way would be to use the mediatorMap to create a mapping between the view component class and a mediator class:

    mediatorMap.mapView( ViewClass, MediatorClass );
    

    And then inject into the view component during the mediator's onRegister hook:

    [Inject] public var injector:IInjector;
    [Inject] public var view:ViewClass;
    
    override public function onRegister():void
    {
        injector.injectInto(view);
    }
    

    This will also work for item renderers, but I find it overkill - especially if the mediator exists purely to perform injection into the view component.

    Another way, more appropriate for item renderers, is to provide a custom IFactory instance to your list/datagroup. The IFactory would use the injector to instantiate each item renderer:

    public class InjectableFactory implements IFactory
    {
        public var injector:IInjector;
        public var generator:Class;
        
        public function InjectableFactory(injector:IInjector, generator:Class)
        {
            this.injector = injector;
            this.generator = generator;
        }
        
        public function newInstance():*
        {
            return injector.instantiate(generator);
        }
    }
    

    You would provide it to your list like so:

    <s:List itemRenderer="{new InjectableFactory(injector, SomeDataRenderer)}"
        dataProvider="{data}"/>

    The complication here is that you need to have a reference to the injector in the component that is hosting the list. Often this isn't a problem as that host component is probably being mediated, and the mediator can pass the injector to it.

    All that said, there is an opinion that injecting into item renderers is bad practice, that item renderers shouldn't know that much about the application, and that it is up to the list (which itself would probably live in a mediated container) to catch events from its item renderers and pass them through to the app (via its mediator). But that's just an opinion, and I find that process awfully long-winded.

    I generally prefer the custom IFactory route, but you should only use it if you have a good understanding of the Flex Component life-cycle, deferred instantiation, virtualization etc. See (PDF):

    http://www.developmentarc.com/site/sites/default/files/understandin...

    Source: http://www.developmentarc.com/site/articles

  4. 4 Posted by christomanos on Oct 12, 2010 @ 11:57 AM

    christomanos's Avatar

    Wow you surely know a lot!

    I'll try the last two methods and see which works best form me. Thanks!

  5. Stray closed this discussion on Feb 13, 2011 @ 02:59 PM.

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