Dispatch signal from itemrenderer?
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?
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
Support Staff 1 Posted by Shaun Smith on Oct 08, 2010 @ 03:17 PM
Hi there,
How are you providing the RemoveItemSignal reference to your item renderer instances?
2 Posted by christomanos on Oct 08, 2010 @ 03:21 PM
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?
Support Staff 3 Posted by Shaun Smith on Oct 08, 2010 @ 04:37 PM
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:
And then inject into the view component during the mediator's onRegister hook:
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:
You would provide it to your list like so:
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 Posted by christomanos on Oct 12, 2010 @ 11:57 AM
Wow you surely know a lot!
I'll try the last two methods and see which works best form me. Thanks!
Stray closed this discussion on Feb 13, 2011 @ 02:59 PM.