Skip to content

Instantly share code, notes, and snippets.

@darscan
Created September 13, 2011 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darscan/1213863 to your computer and use it in GitHub Desktop.
Save darscan/1213863 to your computer and use it in GitHub Desktop.
InjectingMediators
A view Class to mediator Class mapping means that there may be multiple mediator instances for a given view type. How would RL know which instance to inject?
mediatorMap.mapView(SomeView, SomeMediator);
..
addChild(new SomeView());
addChild(new SomeView());
addChild(new SomeView());
..
[Inject]
public var view:SomeMediator; // which one of the 3 mediator instances should we inject? it makes no sense.
If you want to inject a mediator, and you know there will only be one instance, you need to map it yourself. You can do that in the mediator's onRegister hook:
function onRegister():void {
injector.mapValue(SomeMediator, this);
}
@s9tpepper
Copy link

I've been using Robotlegs long enough now that I understand your example. In my case, and how I use Mediator objects, this solution works perfectly for me. I tend to make mediators such that there should never be more than one instance of a single mediator. The only view objects that I typically have more of one instance of are usually item renderers, and I usually prefer to just bubble an event from those to a view that has only one instance around. Thanks again for that!

@darscan
Copy link
Author

darscan commented Oct 8, 2011

It's a pleasure! Glad it helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment