tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/755-best-practices-global-static-objectsRobotlegs: Discussion 2012-02-02T10:40:06Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/123231382011-12-22T16:35:42Z2011-12-22T16:35:42ZBest practices global static objects<div><p>Hi Bruno,</p>
<p>Because your description is pretty vague I’m not sure that
my answer will address your real issue. Some more details, maybe?
:)</p>
<p>Short answer: use Commands to access Models or Services, which
will dispatch an event with the data as a payload and all
interested Mediators would listen to it and pass the data to their
Views. Your “static global file“ would become a Model -
in my example below I will call it VisualDescriptor.</p>
<p>Long answer:<br>
Let’s say you have 2 mediated Views in a ViewStack:
SomeViewOne and SomeViewTwo in a parent View, called NavigatorView.
In the NavigatorView you also have a List of colors. If the user
selects a color you want SomeViewOne and SomeViewTwo to change
their background color accordingly.</p>
<p>So, onSelectedListItem the NavigatorView dispaches an event
ColorEvent.<strong>SET_BG_COLOR</strong> with the chosen color as a
payload. NavigatorMediator listens for that event and redispatches
it, triggering the SetColorCommand.</p>
<p>SetColorCommand calls VisualDescriptor.setBGColor=event.payload
(the color) which then dispatches
ColorEvent.<strong>BG_COLOR_CHANGED</strong>.<br>
<strong>VisualDescriptor</strong> extends Actor, so it dispatches
events on the shared eventDispatcher</p>
<p>SomeMediatorOne and SomeMediatorTwo register a listener for this
event on their onRegister() method, and when they are able to hear
it, they call a method on their views,
view.onBGColorChanged(event.payload) which then sets the BG color
to the one selected in NavigatorView.</p>
<p>Now, while SomeMediatorOne is probably able to react to the
event, SomeMediatorTwo is not, because SomeViewTwo hasn’t
been added to the display list yet, and that means SomeMediatorTwo
hasn’t been created yet.</p>
<p>But by the time the user navigates to SomeViewTwo you want it to
have the BG color chosen in NavigatorView, right? So, what do you
do? You want to have access to the settings in VisualDescriptor
from everywhere, at any time. You can achieve this by using
commands:</p>
<p>SomeMediatorTwo onRegister() registers an event listener for
ColorEvent.BG_COLOR_CHANGED and then dispatches
ColorEvent.<strong>GET_BG_COLOR</strong>, which triggers
GetColorCommand.<br>
GetColorCommand reads the value of VisualDescriptor.BGColor and
dispatches ColorEvent.BG_COLOR_CHANGED with the BGColor as a
payload and SomeMediatorTwo reacts to it and calls
view.onBGColorChanged(event.payload).<br>
Of course you can let the Model dispatch the event instead.</p>
<p>So, if you want some of your Views to share the same
configuration data you let their Mediators listen for the same
event (ColorEvent.BG_COLOR_CHANGED) dispatched somewhere in your
application and then pass the payload to their Views.</p>
<p>In the case of deferred instantiation or whenever a View needs
data, you let the Mediator ask for data by dispatching an event
(ColorEvent.GET_BG_COLOR) and let a Command access it (from a Model
or Service), and then again, the Mediator will react to the event
dispatched after the data has been received in a way or another and
access its View’s API..</p>
<p>(Note: in case of deferred instantiation you can use
Stray’s RelaxedEventMap)</p>
<p>I hope it wasn’t too confusing.</p>
<p>Ondina</p></div>Ondina D.F.