Dispatching signals from the model and having mediators listen to them.

Ryan Yacyshyn's Avatar

Ryan Yacyshyn

17 Jul, 2011 07:12 AM

Hi all,

I'm exploring a little more with Signals in RL. What I'm trying to achieve is to dispatch a signal from my model to update my view (via it's mediator). I'm not sure how to get this linked up.

I'm thinking to create the signal in my model and then have it dispatch in a method, and then inject the signal into my mediator and add a listener to it. I'm pretty sure I also need to add a line in my context class to be able to inject the signal as well but I'm not sure how to do this. Or even if this is the right procedure? I have this working with using events, should I be sticking to using events in this case?

Thanks!

  1. Support Staff 2 Posted by Stray on 17 Jul, 2011 08:32 AM

    Stray's Avatar

    Hi Ryan,

    It all depends what you're trying to achieve - but certainly it's worth knowing how to do it so you can choose the right approach for your app.

    If your signal is a property of the model you'll need to map it for injection in your context or a command. A command is easier in some ways as injecting the model into the command will cause it to be created.

    Your signal will need to be strongly typed for injection. And you mediator will need to extend SignalMediator: https://github.com/Stray/robotlegs-utilities-SignalMediator

    SignalMediator gives you a SignalMap equivalent to EventMap and means that your Signal listeners will automatically get cleaned up when your view leaves the stage - just like with events.

    To map the Signal as a singleton (given that it is a property of your model):

    injector.mapValue(StrongTypedSignal, someModel.aSignal);
    

    Then you just inject this into your mediator and use it as per the instructions on the SignalMediator page.

    If you choose to do this in a Command you can map the command to the STARTUP_COMPLETE event. If you choose to do it in the context you'll need to make sure the model is awake, like this:

    injector.mapSingletonOf(ISomeModel, SomeModel);
    var modelInstance:ISomeModel = injector.getInstance(ISomeModel);
    injector.mapValue(StrongSignalType, modelInstance.aSignal);
    

    Personally I mix Signals and Events in my projects. I think they each have strengths and I like the flexibility I get from choosing between them for each use case.

    I hope this helps, let me know if you're still fuzzy - I can expand on any areas that didn't make sense to you.

    Stray

  2. 3 Posted by Ryan Yacyshyn on 18 Jul, 2011 03:59 AM

    Ryan Yacyshyn's Avatar

    Hi Stray,

    This is great, thanks a lot for the explanation! I'll give these a try and
    see how it works.

    I wasn't sure if I should be sticking to one method only (signals or events,
    and not mixing), it's more clear now and I'd also prefer to choose what's
    most appropriate for the situation. Knowing how to do both now is awesome.

    I'll let you know if I have any problems.

    Thanks,
    Ryan

    On Sun, Jul 17, 2011 at 1:32 AM, Stray <
    [email blocked]> wrote:

  3. 4 Posted by Ryan Yacyshyn on 18 Jul, 2011 08:42 PM

    Ryan Yacyshyn's Avatar

    Hi Stray,

    I understand how to map the signal for injection in my context; however I
    just tried to do the same thing in a command but I'm getting an error
    (Injector is missing a rule to handle injection into property
    "dataReadySignal" of object "[object FlickrAuthorsMediator]".) I must be
    missing something when trying to map the injector by using a command but
    can't seem to figure out where the problem is.

    In my MainContext class I'm injecting other classes and mapping mediators as
    well as mapping a command to the STARTUP_COMPLETE event. In my command
    that's mapped to the STARTUP_COMPLETE event I'm injecting my model and have
    one line in my execute function: injector.mapValue(DataReadySignal,
    model.dataReadySignal);.

    Could the problem be order of execution? The mediator that uses the signal
    is mapped before the signal is injected? My code is on github if it helps to
    see what I'm trying to achieve: https://github.com/ryac/rl-flickr

    Thanks!
    Ryan

  4. 5 Posted by Stray on 18 Jul, 2011 08:48 PM

    Stray's Avatar

    Hi Ryan, if the view already exists and the mediator is mapped before the signal is mapped then yes, that would cause a problem.

    I'll take a peek tomorrow, but in the mean time order of operations is key.

    Stray

  5. 6 Posted by Ryan Yacyshyn on 18 Jul, 2011 09:14 PM

    Ryan Yacyshyn's Avatar

    Thanks Stray, I re-arranged the order and now it's all working as expected.

    Really appreciate your help!

    Ryan

    On Mon, Jul 18, 2011 at 1:48 PM, Stray <
    [email blocked]> wrote:

  6. Support Staff 7 Posted by Stray on 21 Jul, 2011 03:12 PM

    Stray's Avatar

    Great - I'll close this for now then. Feel free to reopen this (or a new thread) if you hit any more bumps.

  7. Stray closed this discussion on 21 Jul, 2011 03:13 PM.

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