about hook

foodyi's Avatar

foodyi

04 Mar, 2014 08:48 AM

readme-hooks I had read this article above, as it describes that hook run before or after certain extension actions . how can i make a hook execute after the command ? it always run before the command.

  1. Support Staff 1 Posted by creynders on 04 Mar, 2014 10:17 AM

    creynders's Avatar

    Yes, that's correct. There's no real reason to have hooks called after a
    command IMO. And it would muddy the command mapping API. But maybe you do
    have a valid use case?

    --
    Sent from Gmail Mobile

  2. 2 Posted by ishaban.flash on 05 Mar, 2014 02:46 PM

    ishaban.flash's Avatar

    2foodyi
    Please check https://github.com/alebianco/robotlegs-utilities-macrobot
    I think it will help you.

  3. 3 Posted by foodyi on 05 Mar, 2014 03:47 PM

    foodyi's Avatar

    to ishaban , I had used this utilities in my project, i think i misunderstand hook. :(
    thank you.

  4. Support Staff 4 Posted by Ondina D.F. on 06 Mar, 2014 12:43 PM

    Ondina D.F.'s Avatar

    Hello,

    Let me give you an example of a command with guards and hooks.
    Consider this mapping:

    commandMap.map(SomeEvent.LOAD_IMAGES, SomeEvent)
        .toCommand(LoadImagesCommand)
        .withGuards(UserGuard)
        .withHooks(LoggerHook);
    

    LoadImagesCommand:

    public function execute():void
    {
        someService.loadData();
    }
    

    UserGuard:

    [Inject]
    public var userModel:UserModel;
            
    public function approve():Boolean
    {            
        return (userModel.userName =="Bender");
    }
    

    LoggerHook:

    [Inject]
    public var userModel:UserModel;
    
    [Inject]
    public var dispatcher:IEventDispatcher;
    
    public function hook():void
    {
        userModel.countCalls++;
        dispatcher.dispatchEvent(new LoggerEvent(LoggerEvent.LOG_USER_CALLS, userModel.countCalls));
    }
    

    Now, that's what happens:

    A user logs in with a name and pw.
    When the user hits a button - load images -, a command gets triggered ( view->event->mediator->command),

    The UserGuard runs before the command is instantiated. It checks if the user name is Bender . If the return value is true, the command is instantiated.

    LoggerHook is used to increment a counter on UserModel and to dispatch an event to a LoggerMediator, which will let its LoggerView display the numbers of calls made by the user, named Bender, to load images.
    After that, the LoadImagesCommand's execute runs and accesses a service in order to load the images.
    If the user name had been "foodyi" instead of "Bender", the UserGuard wouldn't have approved the instantiation of the LoadImagesCommand

    The order of execution for user Bender is:

    1. event triggers command
    2. UserGuard approves the instantiation of command based on some condition
    3. Command is instantiated
    4. LoggerHook does something before command's execute()
    5. Command. execute() calls the service
    6. Command gets garbage collected

    The order of execution for user foodyi is:

    1. event triggers command
    2. UserGuard does not approve the instantiation of LoadImagesCommand
      ...nothing else happens

    This is for sure a silly example, but I think you'll get the idea.

    You can think of guards as a doorman to a club or something. The doorman lets you in only if you have a ticket. A hook may be a guest book that you have to sign before going to the club's bar where the bartender (the command's execute) serves you some drinks. Anyway, a hook is an action that runs before a command's execute(), or if it's a mediator, before mediator's initialize()

    guard == condition
    hook == action, data
    You can use hooks for configurations that have to run before other actions.
    In my example, I injected UserModel into LoggerHook. The LoadImagesCommand is injected only with a service. The command doesn't have to care about the logger event or user model. So, if you want to perform some action before a command runs and you want to keep the command clean and simple, you can use a hook.

    As creynders said, it doesn't make much sense to call a hook after a command has been executed.

    I had read this article above, as it describes that hook run before or after certain extension actions

    The documentation on github might be a bit confusing.

    Hopefully, my explanation and silly examples made sense to you:)

    Can you tell us more about your use case, so we can find a solution together?

    Ondina

  5. 5 Posted by foodyi on 06 Mar, 2014 01:20 PM

    foodyi's Avatar

    hook run before or after certain extension actions

    this really confuse me .
    In my project ,it has a sharing document window. after opening a new document or doing some action on document. there needs to post new status to server. so i consider hook could run after actions, i decide to use the hook to do this work. as you know it always run before the command. let's why i ask this question,i want to know if it can run after a command. now i use a command instead of hook to post status.

    thanks for your detailed reply!

  6. Support Staff 6 Posted by Ondina D.F. on 06 Mar, 2014 01:55 PM

    Ondina D.F.'s Avatar

    If I understand correctly, the flow should be:

    1. open document
    2. modify document
    3. dispatch event after document has been modified
    4. trigger a command
    5. access a service
    6. service sends data to server to let it know about the new status of the document
    7. server responds
    8. service gets response from server
    9. server dispatches an event to trigger some other action?

    In this case you don't need a hook.

    i want to know if it can run after a command

    The short answer is no, you can't run a hook after the command executes.

    What should happen after the command executes? Or, rather after the server has updated the status?

  7. 7 Posted by foodyi on 07 Mar, 2014 02:20 AM

    foodyi's Avatar

    hmm, after the command executes nothing will happen, just saving the current status on sever. i consider while using the hook, it doesn't need to map a new command and dispatch event. just some particular command need this action, i think i could use a hook instead of command,assume hook could run after command.

  8. Support Staff 8 Posted by Ondina D.F. on 07 Mar, 2014 09:50 AM

    Ondina D.F.'s Avatar

    Sorry, but I don't quite understand what you are wanting to do.

    Maybe you are after something similar to a ClassReportingCallbackCommand or SelfReportingCallbackCommand, as seen inside the CommandExecutorTest.as:

    https://github.com/robotlegs/robotlegs-framework/blob/master/test/r...

    I haven't studied such use cases yet. Maybe creynders or Shaun can tell you more about their usage.

    Or, see if the Direct Command is more suited for your use case:
    https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...

  9. Support Staff 9 Posted by creynders on 12 Mar, 2014 03:37 PM

    creynders's Avatar

    Hooks should be used for preparing the application to a state that allows a command to be executed (optionally validated with a guard) They are not meant for doing any "real" work like updating a server's status; that's what commands (together with services) are for. So I'd definitely stick to using a command.

    I'm gonna close this, but if you have more questions or remarks let us know!

  10. creynders closed this discussion on 12 Mar, 2014 03:37 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