Mediators and commandMap

Enrique's Avatar

Enrique

10 Jun, 2011 06:12 AM

HI! why commandMap is not Injected into Mediator?
is a bad practice to map commands inside our mediators? I have some commands that I only need if my mediator is alive, how can I solve this?

Thanks !

  1. 1 Posted by Michal Wroblews... on 10 Jun, 2011 08:21 AM

    Michal Wroblewski's Avatar

    Hi Enrique,

    I see no problem about that. Just inject command map:

    [Inject]
    public var commandMap:ICommandMap
    

    and map commands the same as in Context class.

  2. Support Staff 2 Posted by Stray on 10 Jun, 2011 10:13 AM

    Stray's Avatar

    Hi Enrique,

    this is an interesting use case - it has never come up before as far as I remember.

    What Michal says will work - so you can go ahead and do that.

    But, what's your use case?

    Commands are bound to events, and presumably the events are dispatched from this mediator? So aren't the Commands only going to be activated when this mediator is active anyway? Or do you have an unusual use case where the events aren't coming from this mediator but execution is dependent on this mediator existing?

    One other option is to fire a custom event from the onRegister of this mediator (and another from onRemove) which is bound to a Command which maps and unmaps these commands on the commandMap for you. Generally you shouldn't be acting on the commandMap in a mediator... but it will work and you can always refactor to a cleaner solution later.

    Stray

  3. 3 Posted by Enrique on 10 Jun, 2011 09:38 PM

    Enrique's Avatar

    Hi @Stray, my code is unusual very often :) .... but that is not a good thing :(

    I'm dispatching an event from a model, this event executes a command, this command is creating some VO and Arrays for dataproviders (using many models, well only 2 right now), and the data created by the command is dispatched in another event which is listened by mediators.
    Now, if I don't have the mediator, I don't nee to prepare the data for that mediator and I don't need the command.
    That's why I want to map the command inside my mediator.

    @Michal said that is OK, and you say that is unusual, I say why me.

    Maybe I need to redesign everything, what do you think? :(

  4. Support Staff 4 Posted by creynders on 11 Jun, 2011 05:26 AM

    creynders's Avatar

    Mapping the command doesn't instantiate it. Commands are short-lived anyway, they only exist for the duration of execution of their code.
    This all means you can map your commands up-front even if the mediators don't exist yet. In other words there's no need to map your commands in your mediators.
    The reason why it's better to avoid that is because you create a dependency on those commands in your mediator and the whole purpose is to limit the number of dependencies and other-tier-awareness to a minimum.

  5. 5 Posted by Enrique on 11 Jun, 2011 02:55 PM

    Enrique's Avatar

    yes, but my command IS deppendent of my mediator.
    and if I map the command in the context, then everytime I dispatch an event from my model, will create and execute that command, even if my mediator is not alive, and if my mediator is not alive I don't need that command...
    The command is creating an array from multiple models that I can use as DataProvider for my view.
    I don't need that if my view is not in the stage.

    But maybe I'm using worng the command, I can [Inject] the modles inside my mediator and build the dataProvider then, but I was trying to avoid injecting models inside mediator after reading a post written by @Stray.
    I don't know...

  6. 6 Posted by Stray on 11 Jun, 2011 02:58 PM

    Stray's Avatar

    Hi Enrique,

    what is the event that triggers this view to come and go from the stage?

    This is the event you should bind to Commands to map / unmap this command.

    Stray

  7. 7 Posted by Enrique on 11 Jun, 2011 03:25 PM

    Enrique's Avatar

    ah, that makes sense @Stray.
    I don't have any event at the moment, my app is not ready yet. But I'm trying to put everything in its place.

    but what happens for example if my view is in the stage, but I block it from my mediator, or make it invisible, etc. or the view is a window and is minimized, I mean, I have the mediator, but I don't need to update the dataProvider.
    I don't need to execute a command to minimize a window.

    Although maybe I should to have a model which saves the state of the application.

    How do you manage that? Do you have a model for saving the state of the views?
    I was fighting with that the last days.
    Because, suppose I have a model with "Products", and a view that permits select products.
    Where I should sabe the selected products?
    I finish putting that info in the "ProductModel" (the ProductVO has a selected property) :(

  8. Support Staff 8 Posted by Ondina D.F. on 02 Nov, 2011 03:38 PM

    Ondina D.F.'s Avatar

    Hi Enrique,
    Feel free to reopen this discussion in case you have more questions or you need further assistance with this issue. Please open new threads for new issues.
    Thank you for posting.
    Ondina

  9. Ondina D.F. closed this discussion on 02 Nov, 2011 03:38 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