Updating injected value

Omer Hassan's Avatar

Omer Hassan

02 Mar, 2012 10:21 AM


I have defined a few mediators which require a string "userId". userId is only known after user authentication is complete. I have a command mapped to the event "authenticaionComplete", whose execute() method only has this statement:

injector.mapValue(String, userId, 'userId');

In my mediator, which gets created before authentication is complete, I have this variable:

public var userId:String;

To prevent errors such as "No rule defined for 'userId'", I call injector.mapValue(String, null, 'userId') in the context.

Now, the problem is that after user authentication is complete and my command has executed, the value of userId property in my mediator doesn't change. I'm guessing I may have to do something to trigger all injections named "userId". Please advise if there's a better way to accomplish what I'm trying to do. Thanks.

  1. Support Staff 1 Posted by Shaun Smith on 02 Mar, 2012 11:08 AM

    Shaun Smith's Avatar

    There are no "live" injections in Robotlegs (or SwiftSuspenders). In other words, injected values will not change after injection even if the mapping changes, only new injections will receive the new values.

    But it's generally not advised to map "primitives" into the injector from a design perspective anyway (they are too fine grained to be "flattened" into a container).

    In your situation I would make a model called UserModel. That model would have a property called "userId". When the property changes (via the model's setter) the model would dispatch an event to inform any mediators that the value has changed.

    You would map the UserModel as a singleton at startup and inject it into any mediators that need access to it. Those mediators would register listeners so that they can be informed when the property changes.

    Hope that helps!

  2. 2 Posted by Omer Hassan on 02 Mar, 2012 11:15 AM

    Omer Hassan's Avatar

    Thanks, Shaun. That UserModel approach is what I originally implemented but then I thought, "Hey, injecting user ID would look neater." But okay, I'll go back to having UserModel.

  3. Support Staff 3 Posted by Shaun Smith on 02 Mar, 2012 11:25 AM

    Shaun Smith's Avatar

    Perhaps, but a plain string doesn't give you much control or protection. Having a proper model allows you to encapsulate the information properly and guard access to it.

  4. 4 Posted by Omer Hassan on 02 Mar, 2012 05:20 PM

    Omer Hassan's Avatar

    Several of my services also require user ID but I don't know if it would be a good idea to have them depend upon model. I usually try to make services independent of the rest of the application which allows me to use them in multiple projects. I would love to hear your thoughts on this.

  5. Support Staff 5 Posted by Shaun Smith on 02 Mar, 2012 05:36 PM

    Shaun Smith's Avatar

    You're right that the services should not depend on the UserModel, but they should not depend (statefully) on userIds either. Surely those services would have methods that accept userIds as parameters rather than actually depending on userIds? Something like service.loadContacts(userId). Services should be stateless.

  6. 6 Posted by Omer Hassan on 02 Mar, 2012 06:25 PM

    Omer Hassan's Avatar

    Well, these services extend a subclass of mx.rpc.remoting.RemoteObject class, so they aren't entirely stateless to begin with. That subclass has a public property userId. All the remote methods require user ID as their first argument and I am using the RemoteObject.convertParametersHandler property to prepend userId to all argument lists of remote method calls. So this service doesn't require me to explicitly pass userId to its methods like service.loadContacts(userId) but instead allows me to set its userId property and call its methods without having to specify user ID every time. This is why I was thinking of injecting user ID directly into my service class' userId property. Please point out any design blunders I am making here. Thanks!

  7. Support Staff 7 Posted by Shaun Smith on 02 Mar, 2012 06:59 PM

    Shaun Smith's Avatar

    In that case, instead of injecting the userId via metadata, perhaps just expose a userId setter on your abstract service class. When the userId property changes on the UserModel an event is dispatched that is bound to a command. That command can have the UserModel injected into it and can set the userId property on each service.

  8. 8 Posted by Omer Hassan on 02 Mar, 2012 07:13 PM

    Omer Hassan's Avatar

    Thanks for the nice solution, Shaun, and for taking the time to discuss this with me.

  9. Support Staff 9 Posted by Shaun Smith on 02 Mar, 2012 08:06 PM

    Shaun Smith's Avatar

    It's a pleasure! Hope it works out nicely. Feel free to close this ticket if you are satisfied.

  10. Omer Hassan closed this discussion on 03 Mar, 2012 02:41 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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