tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/90-update-model-best-practiseRobotlegs: Discussion 2018-10-18T16:35:09Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T09:55:55Z2010-03-11T09:55:55Zupdate model best practise<div><p>Hi!</p>
<p>I'm no expert on Robotlegs, but personally I would be tempted to
place a Timers into one of my Mediators which handle the calls to
the Services.</p>
<p>The <a href=
"http://wiki.github.com/robotlegs/robotlegs-framework/best-practices">
Best Practices</a> document states that Models should not be
coupled to the application (in other words, they should not listen
for Framework events or have other Actors injected into them,
whenever possible!)</p>
<p>Jonny.</p></div>Jonny Reevestag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T12:17:01Z2010-03-11T12:17:01Zupdate model best practise<div><p>Runglish! Heheee :)</p>
<p>I don't think you need two timers for this. The service can hold
a timer internally, and do it's remote call every 20 minutes.</p>
<p>Now, depending on how re-usable you want this service to be, you
could either have the model injected directly into the service (in
which case the service will update the model directly) or you can
fire off an event that triggers a command that updates the
model.</p>
<p>Either way, the model should dispatch an event when its data
changes. That is the event the views should be listening for.</p>
<p>I'm not sure why the views would need to check for new data
every 30 seconds if the model is only changing every 20 minutes.
Perhaps I am misunderstanding your needs.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T15:40:54Z2010-03-23T19:42:46Zupdate model best practise<div><p>Shaun, thanks.</p>
<blockquote>
<blockquote>
<p>I'm not sure why the views would need to check for new data
every 30 seconds if the model is only changing every 20 minutes.
Perhaps I am misunderstanding your needs.</p>
</blockquote>
</blockquote>
<p>Views need to be updated every 30 seconds because XML that I
requested from server contains list or coordinates, and some of
views change his positions by its coordinates.</p>
<p>Now I've do it in this way: I create TimerController class that
extends Actor (for inject it where I need it) and inside it I have
methods that start/stop/reset two timers. By timer tick
TimerController fire events and commands mapped to this events do
its jobs (one update model from sevise request and other update
views).<br>
Is this a good solution that one of controller not a command?</p></div>mur4iktag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T15:52:42Z2010-03-11T15:52:42Zupdate model best practise<div><p>Just to help with the mvcs structure, I wonder if your
TimerController is actually more like a TimerService?</p>
<p>Controller implies that it makes decisions in some way, or
contains logic, or initiates actions.</p>
<p>I think it's a good solution - I would just call it a Service
:)</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T16:02:33Z2010-03-23T19:42:46Zupdate model best practise<div><p>And other question is, in StartupCommand I map my
TimerController class as singletone<br>
injector.mapSingleton(TimerController);<br>
but its instance dont create automaticaly, TimerController
instantiate when first injection occurs. But I need instantiate and
call some method from it in my StartupCommand. Now I inject
TimerController in other command and start it, but its little
complex that if I can instantiate it in command where I map it.</p></div>mur4iktag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T16:15:14Z2010-03-11T16:15:14Zupdate model best practise<div><p>In that case, I think the approach is:</p>
<p>var timerController:TimerController =
injector.instantiate(TimerController);</p>
<p>injector.mapValue(TimerController, timerController); (this will
cause it to use your timerController like a singleton).</p>
<p>timerController.doStuff();</p>
<p>You need to use the injector to instantiate your class because
you want the Injections into it to happen (to give you the shared
event dispatcher).</p>
<p>That should work for you.</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T16:34:48Z2010-03-23T19:42:47Zupdate model best practise<div><p>Stray, thanks for help.</p>
<blockquote>
<p>Just to help with the mvcs structure, I wonder if your
TimerController is actually more like a TimerService?
TimerController just contains two timers instanses and some method
to control timers</p>
<p>Controller implies that it makes decisions in some way, or
contains logic, or initiates actions. My TimerController not
contain logic, but its is not just service (if I right understand
role of services). One timer controll model update by calling other
service class that request XML, other controls views update.<br>
If I place timer logic inside service class that load XML its be a
not portable solution. My service just load xml and I can move it
from one project to other.</p>
</blockquote>
<p>Its my first try in RL and in MVCS structure to ;)<br>
So some role of each part of system blow my mind, and I dont
understand wher I should place parts of my code, its littele hard
for me right now :)</p>
<p>But I like RL. And I'll use it in my futher projects.</p>
<blockquote>
<p>var timerController:TimerController =
injector.instantiate(TimerController);
injector.mapValue(TimerController, timerController); (this will
cause it to use your timerController like a singleton).
timerController.doStuff();</p>
</blockquote>
<p>But I need unique instance of TimerController. For example for
change interval of timer tick.</p></div>mur4iktag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T16:38:42Z2010-03-23T19:42:47Zupdate model best practise<div><p>Sorry, something wrong in my last post :)</p>
<blockquote>
<p>Just to help with the mvcs structure, I wonder if your
TimerController is actually more like a TimerService?</p>
</blockquote>
<p>TimerController just contains two timers instanses and some
method to control timers</p>
<blockquote>
<p>Controller implies that it makes decisions in some way, or
contains logic, or initiates actions.</p>
</blockquote>
<p>My TimerController not contain logic, but its is not just
service (if I right understand role of services). One timer
controll model update by calling other service class that request
XML, other controls views update.<br>
If I place timer logic inside service class that load XML its be a
not portable solution. My service just load xml and I can move it
from one project to other.</p></div>mur4iktag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T16:45:56Z2010-03-11T16:45:56Zupdate model best practise<div><p>Ah - it sounds like you're happy with your solution. I don't
think there's anything wrong with it!</p>
<p>I just wondered whether you could put the actual Timing itself
into a TimerService (that has your start / stop functions and fires
the events), and then do the model updating from Commands.</p>
<p>I definitely agree that you shouldn't put your timer into the
XML loading Service! I was thinking that perhaps it was a totally
separate Service in its own right?</p>
<p>What you're describing - the TimerController direct acting on
the models / services is often achieved through the Command pattern
in RL. The Commands would act upon the other services / models, and
the TimerService would just have one job: keep time!</p>
<p>I'm sure it works fine how you're doing it, it's just another
level of decoupling that the RL CommandMap can help you with.</p>
<p>Regarding this:</p>
<blockquote>
<blockquote>
<p>var timerController:TimerController =
injector.instantiate(TimerController);
injector.mapValue(TimerController, timerController); (this will
cause it to use your timerController like a singleton).
timerController.doStuff();</p>
</blockquote>
<p>But I need unique instance of TimerController. For example for
change interval of timer tick.</p>
</blockquote>
<p>If you mean that you need just one single instance (like a
singleton) then this will give you that. Using mapValue rather than
mapSingleton essentially does the same but without the lazy
instantiation - you have to instantiate the value before you map
it, which sounds like what you wanted? If that's not what you
wanted then maybe you could explain what you need in a bit more
detail?</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/11937232010-03-11T17:10:37Z2010-03-23T19:42:47Zupdate model best practise<div><blockquote>
<p>Ah - it sounds like you're happy with your solution. I don't
think there's anything wrong with it! Not exactly, I just dont
understanf if I do it right :)</p>
</blockquote>
<p>If U think that TimerController its a service I'll do it (remove
from controller package to service package and rename it to
TimerService). ;)</p>
<blockquote>
<p>var timerController:TimerController =
injector.instantiate(TimerController);
injector.mapValue(TimerController, timerController);
timerController.doStuff();</p>
</blockquote>
<p>Yeah, mapValue its exactly what I need!<br>
I misunderstand it, I thought that it inject new instance in each
injection, but I was wrong.</p>
<p>Thanks a lot to All. Question is solved.</p></div>mur4ik