Not deferred instantiation of the singleton

Nikita's Avatar


27 Jan, 2014 12:40 PM

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 [Inject] public var UserSignal ;

// somewhere in the app 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?

  1. Support Staff 1 Posted by Ondina D.F. on 27 Jan, 2014 01:21 PM

    Ondina D.F.'s Avatar

    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.

    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:


  2. Support Staff 2 Posted by Ondina D.F. on 27 Jan, 2014 01:35 PM

    Ondina D.F.'s Avatar

    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. 3 Posted by matej on 27 Jan, 2014 01:40 PM

    matej's Avatar


    try this.

    after you map your model in your config just do this;
    var model:Model = injector.getInstance(Model);

    that way the model will be instantiated from the beginning.

  4. 4 Posted by Nikita on 27 Jan, 2014 02:01 PM

    Nikita's Avatar

    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.

  5. Support Staff 5 Posted by Ondina D.F. on 27 Jan, 2014 02:17 PM

    Ondina D.F.'s Avatar

    ok, ok, (mis)use a Model however you want :P

    And I can't add listener in constructor of model. So I need to call initialization somewhere later.

    You need to mark a method with PostConstruct

    public class SomeModel
        public var someSignal:SomeSignal;
        public function SomeModel ()
            //don't do anything in the constructor!!!
        public function addSignalListener():void
  6. 6 Posted by matej on 27 Jan, 2014 02:20 PM

    matej's Avatar

    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)

  7. 7 Posted by Nikita on 27 Jan, 2014 02:28 PM

    Nikita's Avatar

    That what I needed. Thank you.

  8. Support Staff 8 Posted by Ondina D.F. on 27 Jan, 2014 02:59 PM

    Ondina D.F.'s Avatar

    @matej thanks for mentioning constructor injection, as well

    An interesting article by Shaun Smith ( the author of Robotlegs):

  9. 9 Posted by matej on 27 Jan, 2014 08:06 PM

    matej's Avatar

    nice read

  10. Ondina D.F. closed this discussion on 03 Feb, 2014 02:09 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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