Not deferred instantiation of the singleton
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:
// in UserModel.as [Inject] public var UserSignal ;
// somewhere in the app injector.map( UserSignal ).asSingleton();
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.
So, my question - how can I be sure that my model created and listens for a signal?
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 Ondina D.F. on 27 Jan, 2014 01:21 PM
Hi Nikita,
If you want to use Models following the robotlegs' MVCS pattern, you should not 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.
http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/wha...
You need to say more about your use case, so we can help you find a better solution.
Have you looked at some examples and read the Best Practices already?
Best Practices with code for rl1, but the principles are the same in rl2:
https://github.com/robotlegs/robotlegs-framework/wiki/Best-Practice...
Ondina
Support Staff 2 Posted by Ondina D.F. on 27 Jan, 2014 01:35 PM
If you need to update your Model when the Service gets the results from a Server, the Service can:
either trigger a Command, by dispatching an event or signal, and the Command can update the Model, calling an API of the injected Model
or the Service can call the Model's API, that was injected into the Service, directly and set the results on the Model
It depends on your project, which approach you choose.
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.
3 Posted by matej on 27 Jan, 2014 01:40 PM
Hello,
try this.
after you map your model in your config just do this
injector.map(Model).asSignleton();
var model:Model = injector.getInstance(Model);
that way the model will be instantiated from the beginning.
4 Posted by Nikita on 27 Jan, 2014 02:01 PM
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.
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.
Support Staff 5 Posted by Ondina D.F. on 27 Jan, 2014 02:17 PM
ok, ok, (mis)use a Model however you want :P
You need to mark a method with PostConstruct
6 Posted by matej on 27 Jan, 2014 02:20 PM
or use constructor injection
if constructor parameters are mapped, they will be populated on construction of the class.
public class SomeModel
{
private var _someSignal:SomeSignal;
public function SomeModel (signal:SomeSignal)
{
_someSignal=signal;
}
7 Posted by Nikita on 27 Jan, 2014 02:28 PM
That what I needed. Thank you.
Support Staff 8 Posted by Ondina D.F. on 27 Jan, 2014 02:59 PM
@matej thanks for mentioning constructor injection, as well
An interesting article by Shaun Smith ( the author of Robotlegs):
http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-v...
9 Posted by matej on 27 Jan, 2014 08:06 PM
THX
nice read
Ondina D.F. closed this discussion on 03 Feb, 2014 02:09 PM.