Obtain an associated mediator from a view object
We have the following problem and we don't know how to solve it using robotlegs:
We have a client/server Chat application that allows you to have several ChatView instances, each private chat between users create a ChatView instance. The servers generates a Event handled by a Command. How can we update just the ChatView instance related to the user that is sending the message? Because the mediator is instantianted automatically for each view and is generic. Is there a way of obtaining a view's mediator with the view object? How could we approach this problem?
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 30 May, 2013 04:08 PM
There are many ways to tackle this, but here is a pretty simple solution.
Create an auto-incrementing static counter on your mediators that will be used to generate unique
id
s. Supply this when making service calls, and return it along with the results. Then each mediator can just ignore messages that are not relevant to it (where theid
doesn't match).This assumes that the mediator is listening for service results directly. If you want to differentiate the mediators in a command you'd need to change things a little, but that's the basic idea.
2 Posted by victor.bolinche... on 31 May, 2013 09:34 AM
According to the best practices of using Robotlegs, mediators should not include any logic (that includes if-for-while ...).
The solution you proposed includes logic in the mediator and performs a broadcast (since all mediators are subscribed to that event) and I'm worried about the efficiency.
You said that there are other solutions. Can you explain any other that doesn't perform a broadcast calling?
Thank you for your answer.
Support Staff 3 Posted by Shaun Smith on 31 May, 2013 02:08 PM
Hi Victor,
I think that's taking it a little too far. The idea is to avoid putting "business logic" in your mediators. A mediator is supposed to be a bridge between a view and the rest of the application. Part of this role involves determining if a message from the application relates to the view that is being mediated.
The mediator is not broadcasting, it is subscribing to messages from the app. This is what mediators are designed to do.
This would only be a real concern if you had a couple of hundred thousand chat views. In reality, the performance impact would be negligible for a few hundred views.
Indeed! And the solution I proposed is my least favourite. I only mentioned it because it's something you could implement quickly with your current design.
Your initial question was actually how to "Obtain an associated mediator from a view object"?
The framework does not provide a way to do this. Even if it did it would be difficult to work with as a particular view might have any number of mediators. One solution is to manually associate views with mediators in a model. It's a similar approach to the one above.
Essentially you create a model that allows you to associate views with mediators. Mediators add themselves to this model in their
initialize
method. The command can use this model to retrieve mediators for views.Psuedocode:
Again, this is just one solution.
4 Posted by victor.bolinche... on 31 May, 2013 02:16 PM
Thank you for explaining!
Support Staff 5 Posted by Shaun Smith on 31 May, 2013 02:25 PM
My pleasure! As I said there are other approaches, but they might require a complete redesign in terms of your service layer. Personally, I use promises, instead of the awkwardly decoupled
event->command->service->event->etc
flow. See:https://github.com/CodeCatalyst/promise-as3
http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/
Ondina D.F. closed this discussion on 05 Jun, 2013 11:42 AM.