PropertyInjector

(This tag must be placed inside an <Injectors> 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.

<Injectors target="{MyTargetClass}">
   <PropertyInjector targetKey="propertyToPopulate" source="{MyModel}" sourceKey="propertyFromMyModel" />
</Injectors>

Attributes

targetKey

String

required

The property of the target (defined in the parent Injectors tag) that needs to be injected.

source

Class or Object

The object that will be the source that will populate the target property. If this is an object, it will used as is. If this is a class, then an already instantiated object will be retrieved from the cache (instantiated by MethodInvoker or ObjectBuilder). If cache does not contain an object of this class, a new object will be created and added to the cache.

sourceKey

String

Property of the source that will be used to populate the target property. Mate will attempt to bind this property to the target property so that whenever the property on the source changes its value, the target will be updated with the new value.

If this attribute is not supplied, then the source object will be injected into the target (instead of a property of the source)

targetId

String

If you use this attribute, only targets that have this id will be injected the property. This id is the one defined in MXML.

Injecting views

You can inject values to properties declared in views. If you use the default settings in the tag InjectionSettings, then you only need to create a property in your view. Then you need a source that will store the value the view will get. For example, if you have a UserManager class that stores the current user, and you want to show information about that user in a view, you can do the following:

  1. Have a view called UserInformation
  2. In that view, have a property called "user".
  3. In your UserManager class, have a public bindable property called currentUser ( this property can be read-only by the use of getters).
  4. Inject this property into the view.

<Injectors target="{UserInformation}">
   <PropertyInjector targetKey="user" source="{UserManager}" sourceKey="currentUser" />
</Injectors>

When the view is instantiated, the property user will be injected into it with the value coming from the UserManager. If the property on the UserManager is bindable, the view will get the latest value any time it changes.

The source can be any "model" that stores data. We use "Managers", but the data could also be stored in a ModelLocator. For example:

<Injectors target="{UserInformation}">
   <PropertyInjector targetKey="user" source="{ModelLocator.getInstance()}" sourceKey="currentUser" />
</Injectors>

Our source will be an already instantiated object (the instance of the ModelLocator), and then we'll bind the property currentUser to the target property.

Injecting view adapters

You can also inject other objects into your views that are not data coming from a model.

As an example, you can inject a "view adapter" into your view. You would then send data to the adapter, which the adapter would format, massage and filter specifically for the view. Data would be send to the adapter also via injection.

<Injectors target="{UserInformation}">
   <!-- create the adapter -->
   <ObjectBuilder generator="{UserInformationAdapter}" registerTarget="true" />
   <!-- inject the adapter into the view -->
   <PropertyInjector targetKey="myAdapter" source="{lastReturn}" />
</Injectors>

In the code above, the view will contain a public property called "myAdapter". Then the view can use it to access the adapter properties to populate its controls.

In order to populate the adapter with the data, we can use injectors just like we did in the model-to-view example, but instead of injecting the view, we'll inject the adapter:

<Injectors target="{UserInformationAdapter}">
   <PropertyInjector targetKey="user" source="{UserManager}" sourceKey="currentUser" />
</Injectors>

23 responses so far

  1. The PropertyInjector API docs say "PropertyInjector sets a value from an object (source) to a destination (target). If the source object is an IEventDispatcher, the PropertyInjector will bind the source with the target. Otherwise, it will only set the property once."

    Because the [Bindable] tag causes the object to implement IEventDispatcher, the docs should be rewritten to say something like: "PropertyInjector sets a value from an object (source) to a destination (target). If the source object is [Bindable] or impements IIEventDispatcher, the PropertyInjector will bind the source with the target. Otherwise, it will only set the property once."
  2. Hi Mike,
    I updated the API explaining better the code.
    Now it says "If the source key is bindable, the PropertyInjector will bind the source to the targetKey"
  3. What if I wanted to inject a value into a view that contains a ProgressBar. The only way to update a progressBar value is to physically use the setValue() function. There is no bindable property on the progressBar that reflects it's current progress value.

    Hope that made sense :-\
  4. Hi Kyle,
    If that information is outside the view and you want to inject it, then use a public setter, instead of a plain public property. Then use setValue when the binding calls your setter.
    ie:
    public function set progress(value:Number):void {
       myProgressBar.setValue(value);
    }
  5. Thanks, Laura for the response. What I realized after reading the documentation further was that I could pass an object that is announcing the progress events to the ProgressBar and it takes care of the rest. My ApplicationManager contains the appUpdater as a bindable property which is then Injected into the view that contains the progressbar.
  6. hi,

    is there a way to inject objects directly. because i see that you always need a provide a source( the class to be used) and the sourceKey( the object).
    How do you inject an object which is not part of Model nor singleton.
    [Ex, if you want to inject an implementation like spring, how do achive this?]
  7. pramod,
    As stated in the docs above, if the sourceKey is not supplied, then the source object will be injected into the target. You don't have to provide the sourceKey, only the source is required.
  8. Is there anyay to inject a property using a custom method, rather than the setter?

    Some sort of method invocator that works on a targetKey, instead of a generator. Because the methid invoker creates a new object everytime to invoke the method, i need an existing object like the targetKey

    thanks!
    Peter

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