tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/9436-not-deferred-instantiation-of-the-singletonRobotlegs: Discussion 2018-10-18T16:35:54Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T12:40:26Z2014-01-27T12:40:28ZNot deferred instantiation of the singleton<div><p>Hello. I'll try to explain what I want to do. I have some
UserModel that holds some user related data. I have some UserSignal
that fires when I receive some data from server. I'm injecting
signal into the model - it's easy:</p>
<p>// in UserModel.as [Inject] public var UserSignal ;</p>
<p>// somewhere in the app injector.map( UserSignal
).asSingleton();</p>
<p>Now I want to use my moder everywhere I want - just using
[inject]. But if a signal was fired before first use of a model
then my model hold wrong data - simply because it wasn't
instantiated when signal triggered.</p>
<p>So, my question - how can I be sure that my model created and
listens for a signal?</p></div>Nikitatag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T13:21:37Z2014-01-27T13:21:37ZNot deferred instantiation of the singleton<div><p>Hi Nikita,</p>
<p>If you want to use Models following the robotlegs' MVCS pattern,
you <strong>should not</strong> listen for signals or events in
your Models, even if this is technically possible. Models should
only dispatch signals or events, when their data changes.</p>
<p><a href=
"http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/what-are-the-responsibilities-of-the-model-class-in-mvcs">
http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/wha...</a></p>
<p>You need to say more about your use case, so we can help you
find a better solution.</p>
<p>Have you looked at some examples and read the Best Practices
already?</p>
<p>Best Practices with code for rl1, but the principles are the
same in rl2:<br>
<a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/Best-Practices#wiki-modelandmodels">
https://github.com/robotlegs/robotlegs-framework/wiki/Best-Practice...</a></p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T13:35:27Z2014-01-27T13:35:56ZNot deferred instantiation of the singleton<div><p>If you need to update your Model when the Service gets the
results from a Server, the Service can:</p>
<ul>
<li>
<p>either trigger a Command, by dispatching an event or signal, and
the Command can update the Model, calling an API of the injected
Model</p>
</li>
<li>
<p>or the Service can call the Model's API, that was injected into
the Service, directly and set the results on the Model</p>
</li>
</ul>
<p>It depends on your project, which approach you choose.<br>
The Model can dispatch an event or signal, when its data changes. A
Mediator, for example, could listen for that event/signal, and pass
the data on to its view.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T13:40:25Z2014-01-27T13:40:25ZNot deferred instantiation of the singleton<div><p>Hello,</p>
<p>try this.</p>
<p>after you map your model in your config just do this</p>
<p>injector.map(Model).asSignleton();<br>
var model:Model = injector.getInstance(Model);</p>
<p>that way the model will be instantiated from the beginning.</p></div>matejtag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T14:01:25Z2014-01-27T14:01:27ZNot deferred instantiation of the singleton<div><p>Thank you. I'll look at best practices. Currently I'm using
matej's approach. But the problem that I need to listen for a
signal that is singleton too and injected in model. And I can't add
listener in constructor of model. So I need to call initialization
somewhere later.<br>
I thought about Ondina's approach. It's good but I don't want to
have possibility to change model from public method - that's the
reason I'm listening for changes inside model itself.</p></div>Nikitatag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T14:17:50Z2014-01-27T14:17:50ZNot deferred instantiation of the singleton<div><p>ok, ok, (mis)use a Model however you want :P</p>
<blockquote>
<p>And I can't add listener in constructor of model. So I need to
call initialization somewhere later.</p>
</blockquote>
<p>You need to mark a method with PostConstruct</p>
<pre>
<code>public class SomeModel
{
[Inject]
public var someSignal:SomeSignal;
public function SomeModel ()
{
//don't do anything in the constructor!!!
}
[PostConstruct]
public function addSignalListener():void
{
someSignal.add(onSomeSignalDispatched);
}
...</code>
</pre></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T14:20:58Z2014-01-27T14:20:58ZNot deferred instantiation of the singleton<div><p>or use constructor injection<br>
if constructor parameters are mapped, they will be populated on
construction of the class.</p>
<p>public class SomeModel<br>
{ private var _someSignal:SomeSignal; public function SomeModel
(signal:SomeSignal) { _someSignal=signal; }</p></div>matejtag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T14:28:16Z2014-01-27T14:28:17ZNot deferred instantiation of the singleton<div><p>That what I needed. Thank you.</p></div>Nikitatag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T14:59:07Z2014-01-27T14:59:07ZNot deferred instantiation of the singleton<div><p>@matej thanks for mentioning constructor injection, as well</p>
<p>An interesting article by Shaun Smith ( the author of
Robotlegs):<br>
<a href=
"http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-vs-setter-injection/">
http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-v...</a></p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/313164632014-01-27T20:06:05Z2014-01-27T20:06:05ZNot deferred instantiation of the singleton<div><p>THX<br>
nice read</p></div>matej