Getting item renderer's data formatted properly

kamcknig's Avatar

kamcknig

17 Aug, 2015 04:53 PM

OK, let's see if I can explain this.

I have a view that is an ItemRenderer. I'm overriding the ItemRenderer's set data property so that I can format some data that is getting passed into the object. I have a CurrencyFormatter class that is mapped as a singleton. This CurrencyFormatter is used to format some text to display. I don't want to inject the CurrencyFormatter into the view if I can help it, though honestly I would be OK with that since it's not a Model and just a helper class.

If I wanted to do that, how do I "properly" inject a model into a view?

If I don't want to do that the only other way I can think is to create a mediator for the ItemRenderer and inject the CurrencyFormatter into the mediator and then when the view gets added to the stage handle the formatting then,

Any ideas/thoughts?

  1. Support Staff 1 Posted by Ondina D.F. on 18 Aug, 2015 10:43 AM

    Ondina D.F.'s Avatar

    Hi,

    Injection into a view is made possible by the viewProcessorMap:

    https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...

    injector.map(SomeClass).asSingleton();
    viewProcessorMap.map(SomeView).toInjection();
    
    //in SomeView:
    [Inject]
    public var someClass: SomeClass;
    

    Your item renderer:

    var customCurrencyFormatter : CustomCurrencyFormatter = new CustomCurrencyFormatter();
    injector.map(CustomCurrencyFormatter).toValue(customCurrencyFormatter);
    //or
    //injector.map(CustomCurrencyFormatter).toType(CustomCurrencyFormatter);
    
    viewProcessorMap.map(SomeItemRenderer).toInjection();
    

    Be aware of the differences between :

    toValue - Makes the mapping return the given value for each consecutive request.

    toType - Makes the mapping return a newly created instance of the given type for each consecutive request.

    toSingleton - Makes the mapping return a lazily constructed singleton instance of the mapped type for each consecutive request.

    asSingleton - Makes the mapping return a lazily constructed singleton instance of the mapped type for each consecutive request.

    asSingleton(initializeImmediately:Boolean=false) -> initializeImmediately - Determines when the instance should be created, immediately after mapping or after the first request

    If I don't want to do that the only other way I can think is to create a mediator for the ItemRenderer and inject the CurrencyFormatter into the mediator and then when the view gets added to the stage handle the formatting then,

    There are different opinions on whether is it a good practice to mediate item renderers or not. I'm with those who think that mediating item renderers is a bad idea.
    I think, item renderers should not know about any framework, and a framework shouldn't have to know about them either. Item renderers are reusable elements and it should be possible to use them without any framework . For that matter, I also wouldn't inject anything into an item renderer.

    I'm using a ClassFactory to set list's itemRenderer and its properties or/and methods.

    If you inject the CurrencyFormatter into the View containing the List or directly into the List, if the List is a View for itself, then you can pass the CurrencyFormatter to the renderer via a ClassFactory, or you can use the labelFunction of the list and do the formatting there.

    If you don't like to inject into views, you can mediate the View containing the list or the List itself, and inject the dependencies into the mediator, which would pass them on to the View.

    Hope this helps.
    Ondina

  2. 2 Posted by kamcknig on 18 Aug, 2015 11:48 AM

    kamcknig's Avatar

    Thanks for all the insight! I agree, I would really rather not mediate an itemRenderer if possible.

    I'll look into ClassFactory's, I haven't ever really dealt with them yet, but I'm always up for learning new techniques.

    Thanks!

  3. 3 Posted by kamcknig on 18 Aug, 2015 11:56 AM

    kamcknig's Avatar

    ClassFactory would work perfectly! Easier than I thought. I guess technically I was already using it since Flex does it automatically haha. Thanks so much.

  4. Support Staff 4 Posted by Ondina D.F. on 18 Aug, 2015 12:09 PM

    Ondina D.F.'s Avatar

    No problem:)

    Yeah, I forgot to mention that it was a Flex core class that implements IFactory, but it looks like you found it.

    I guess this is resolved -> going to close the discussion. You can re-open it, if need be.

  5. Ondina D.F. closed this discussion on 18 Aug, 2015 12:09 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