Updating injected value
Hi,
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:
[Inject(name='userId')]
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.
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
Support Staff 1 Posted by Shaun Smith on 02 Mar, 2012 11:08 AM
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 Posted by Omer Hassan on 02 Mar, 2012 11:15 AM
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.
Support Staff 3 Posted by Shaun Smith on 02 Mar, 2012 11:25 AM
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 Posted by Omer Hassan on 02 Mar, 2012 05:20 PM
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.
Support Staff 5 Posted by Shaun Smith on 02 Mar, 2012 05:36 PM
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 Posted by Omer Hassan on 02 Mar, 2012 06:25 PM
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 theRemoteObject.convertParametersHandler
property to prependuserId
to all argument lists of remote method calls. So this service doesn't require me to explicitly pass userId to its methods likeservice.loadContacts(userId)
but instead allows me to set itsuserId
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!Support Staff 7 Posted by Shaun Smith on 02 Mar, 2012 06:59 PM
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 Posted by Omer Hassan on 02 Mar, 2012 07:13 PM
Thanks for the nice solution, Shaun, and for taking the time to discuss this with me.
Support Staff 9 Posted by Shaun Smith on 02 Mar, 2012 08:06 PM
It's a pleasure! Hope it works out nicely. Feel free to close this ticket if you are satisfied.
Omer Hassan closed this discussion on 03 Mar, 2012 02:41 AM.