Injectors

(This tag must be placed inside an <EventMap> tag)

An Injectors tag defined in the Event Map is a container for InjectorPropertys that will inject properties coming from a source to a target, but they can also be used for other purposes.

<Injectors target="{MyTargetClass}">
   ...
</Injectors>

The Injectors tag are similar to the EventHandlers, and when executed, they will run all inner tags in order. EventHandlers are executed when an event of the matching type is dispatched. Injectors, on the other hand, are executed when an object of the class defined in the target attribute is created. In order for the Injectors to run, the object of that class needs to be created within the display list, or be instantiated by the ObjectBuilder or MethodInvoker tags.

Attributes

target

Class
required

The class that, when instantiated, should trigger the inner tags to run. This target must be supplied using bindings, but this binding is only executed once, when the event map is created.

includeDerivatives

Boolean

If true, then subclasses of specified class will also trigger injection. If specified class is an interface, then classes implementing interface will trigger injection. If false, only instances of the specific class will trigger inner tags to run. Default is false.

debug

Boolean

Whether to show debugging information for this Injectors block. If  true, Console output will show debugging information as the injectors are applied.

start

Event handler

In the start attribute you can supply an event handler for the event of type ActionListEvent.START. This event is dispatched right before the actions (normally PropertyInjector items) are called, when the list starts execution.

Example:

<Injectors target="{MyTargetClass}" start="trace('Execution of injectors list started!')">
...
</Injectors>

end

Event handler

In the end attribute you can supply an event handler for the event of type ActionListEvent.END. This event is dispatched right after all the items in the list have been executed.

Example:

<Injectors target="{MyTargetClass}" end="trace('Execution of injectors list ended!')">
...
</Injectors>

Inner tags

PropertyInjector

PropertyInjector s defined in the Injectors tag will apply or bind properties specified in the source to the target specified in the parent Injectors target. See more information in the PropertyInjector documentation.

ListenerInjector

A ListenerInjector tag will add an event listener for a specific event type and call the specified method on the target specified in the parent Injectors target. See more information in the ListenerInjector documentation.

Other tags

All other tags allowed inside EventHandlers and MessageHandlers are allowed inside the Injectors. This allows you to inject other properties to the target, such as data coming from a server call (one-time only).

28 responses so far

  1. I'm really liking mate, but I'm not a big fan of the verbosity of using injectors. I guess compared to Cairngorm / j2ee it's not so bad, but the other half of my brain is ruby so you can see where I'm coming from.

    I understand needing an explicit getter and setter when you're doing more than just moving properties from the business logic to the view, but most of the time that's all I'm doing. Having to define code like this:

    [Bindable (event="heightChange")]
    public function get main_image_height():Number
    {
    return _main_image_height;
    }

    public function set main_image_height(h:Number):void
    {
    _main_image_height = h;
    dispatchEvent( new Event('heightChange'))
    }

    instead of just something like this:

    public [Bindable] main_image_height (need that event communication though...)

    is really painful.

    I need to dig in your code and see if I can contribute a solution to my problem, but I figured I would see if I'm just missing something here (I only started playing with Mate yesterday).

    Keep up the great work. Mate looks to be shaping up to be my framework of choice.
  2. Mike,
    setters and getter are the equivalent to a property. Nowhere here it says that you have to use getters and setters. The targetKey for PropertyInjectors needs just to be a public property, which doesn't even need to be bindable. How you expose that property (via a public property or via setters) is up to you.

    You can see an example of that here: http://mate.asfusion.com/assets/content/examples/stockQuoteExample/srcview/

    I know this section of the docs is a little thin, so that is probably why you are missing that piece. I am working on writing more for this.
    If you have more questions, please refer to the forums.

    Thanks!
  3. Do you have an example using START and END for its true purpose, thus not using trace() method.

    You documentation is difficult to understand. Do you mean

    start="handlerMyEventHandler"

    or

    start="ActionListEvent.START"
  4. You use a function as your event handler for start or end events:
    start="yourFunction()" or start="yourFunction(event)" or start="yourFunction('some value')"
  5. Laura, Can you pls elaborate on what you mean when you wrote "Injectors, on the other hand, are *executed* when an object of the class defined in the target attribute is created."? I don't understand that because I think of injectors and their inner tags also as (as3) objects, which are instantiated and initialized (just after the parent eventmap object is instantiated) and then injectors have some properties and some methods which may be called. To try understand what you mean, I looked at the FB3 generated as3 source.
  6. Emmett,
    It is true that the whole event map is defined once, all the objects are created in the beginning and that's it. But those objects are registered to listen to events. If you take the example of the EventHandlers, each EventHandlers in the EventMap gets created in the beginning, but when an event is dispatched that matches the type attribute, those EventHandlers "run", in the sense that they "execute" the steps defined by their inner tags (which also were only defined once). So in this sense, EventHandlers and Injectors contain a recipe with steps that they will "execute" any time needed. For the EventHandlers, that time is when an event is dispatched, for the Injectors is when an object is created (an object that matches the class specified in the target). Does that clarifies it?
  7. Laura, Thank you. That does explain a couple of things. Firstly it was never obvious to me that Injector tags (objects) listen for events. For Eventhandlers this always was assumed (good naming of course). For injectors I think I understand now. You also made me realize why Injectors are defined in the EventMap. Secondly it would seem that unlike typical EventHandlers, Injectors might be executed very rarely or even just once. eg. Look at the StockQuoteExample app. If I understand Flex right, then only one single QuotePanel injection target object is defined, so the single app Injector is I assume executed just once when the view is assembled. To me it looks like that Injector simply sets itself up as some kind of data binding middle point, and then after that, its "execution" job is done forever! Right??
  8. That's right.
  9. Hi Laura, thanks for clarifying this point for us. To exploit injection properly/fully, I believe it's essential this "event time of binding" concept is understood correctly.
  10. Laura, you said that : "for the Injectors is when an object is created (an object that matches the class specified in the target)"
    i'm currently working on a project with three views.
    on the first view, you have a list of person,
    when you select a person (person1), a view pop and show informations.
    if you select another person (person2), you'll get a new view with the second person informations. the two views (person1 & person2) are display at the same time on the screen.
    at this moment, all is ok,
    so, let's complicate it : the information view is a viewStack, with two views : viewA and viewB, on viewB, i have a list.
    how can i populate the list in viewB on person1 without making change to the list in viewB on person2 ? is it possible with Injectors ?
  11. Is there a way to get a reference back to the object that the injector is dealing with from inside the tag block? Say, for example, whenever an instance of ClassA is created an ObjectBuilder runs, creates an instance of ClassB, and passes a reference of ObjA to ObjB. Many thanks!
  12. event.injectorTarget seemed to do the trick :)
  13. {MyTargetClass} is instantiated and cached when it is first created, but is there a way to remove it from the cached when it being destroyed or no longer being referenced in the application?
  14. I have the same problem that @loop. What is the solution for multiple instances of the same view ??
  15. I've posted this within the propertyInjector forum as well but it is related to this too ...

    <Injectors target="{MyTargetClass}">

    You reference the Class and not an instance of the class. What if I'm using multiple instances and need to inject into a specific one?
  16. You can use targetId in your inner tags to distinguish instances.
  17. Hi Mate Team!

    I just started using Mate and I think it's great!

    I have a question and would appreciate it if you could shed some light on the matter.

    The last sentence of the second paragraph reads:

    "In order for the Injectors to run, the object of that class needs to be created within the display list, or be instantiated by the ObjectBuilder tag with attribute registerTarget="true"."

    However, the registerTarget property doesn't seem to be documented in ObjectBuilder page. Am I missing something?

    I also come across another probable discrepancy. In the MethodInvoker page http://mate.asfusion.com/page/documentation/tags/methodinvoker, it is explained that the cache attribute can take either of the values "global", "inherit", "local" or "none". But an example, a few paragraphs down, suggests that the cache attribute is of Boolean type:

    <MethodInvoker generator="{MyWorker}" method="doWork" cache="false" />

    Could you please shed some light on this one too?

    Cheers,

    Naso.
  18. Can there be only one injector in an event map or we can have multiple injectors?
  19. You can have as many Injectors as you want.
  20. I'd like to inject all classes in a class hierarchy. E.g., if I have:

    class FooBase

    and
    class FooSub1 extends FooBase
    class FooSub2 extends FooBase

    ...I want to inject any instances of FooBase, FooSub1, and FooSub2 using a single Injectors block.

    Is it possible to do this without explicitly listing every subclass in the 'targets' attribute of the Injectors? Simply listing FooBase doesn't seem to do the trick.

    (The end user can create custom subclasses of FooBase, and it's a bit inconvenient to update the Injectors definition with these custom classes.)

    Cheers,
    Aron
  21. Aron,
    You can set the target to be the super class, and use the includeDerivatives attributes as true and then subclasses will also be injected.
  22. Brilliant! Thanks for the quick response.

    Cheers,
    Aron
  23. Hi!

    I have question about parameter target.

    My code MyEventMap
    <Injectors target="{MyView}">
    <PropertyInjector targetKey="model" source="{MyViewModel}"/>
    </Injectors>

    When instance of class MyViewModel is injecting in property model of MyView?
    1. Only when MyEventMap is created. And if at this moment i don't create yet MyView - injecting don't work.
    2. When MyView is added to display list?
  24. I think i found bug - if added view in ViewStack in Flex 4 app after creating EventMap. Injector is not work.

    May be event of creation view in some reasons don't dispatch
  25. I am new to Mate and have been using Flex with ColdFusion for about a year. I am having a bit of trouble understanding how to get information back to the view correctly. For example, I want to store a users session in a object (user_id,first_name,last_name, etc) so I can access these class properties from my views.

    I think I understand how to populate the class properties correctly, but I'm not sure how to pull them into my views for later use. I have researched Injectors/Setters/Getters and I'm not sure which is the correct way to handle this sort of information.

    Any advice or examples from anyone would be much appreciated!

    Thanks to everyone and the asFusion team for sharing their knowledge!
  26. First, i'm new with Flex and Mate and i found it great.

    Is it possible, with injectors, to applicate a skin on each Button that are instantiate.. ? Here I say Button, but it could be any other component.

    I would like that because i have a lot of component with skins and it would be great.

    Thank for the answer
  27. if have a component called filter. and when i press it to send data to my result view or data to my search view.

    It depends on wich view i put my component.
    the problem is if inject the data will go to my 2 views

    any solution ? ^^
  28. I have a view that i want to dynamically add a child UIComponent element after an event i have handled in the eventhandler.
    With the injector how do i do this if i don't have access to the view.

    model doesn't know about the view? so can't add a component, it can use dependency injection to know about the change in data in the view but can't add an element to itself or can it?

Leave a response

If you need help or want to comment on something not related to this page, please post in the forums. Thanks!

Leave this field empty