about hook
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.
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 creynders on 04 Mar, 2014 10:17 AM
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 Posted by ishaban.flash on 05 Mar, 2014 02:46 PM
2foodyi
Please check https://github.com/alebianco/robotlegs-utilities-macrobot
I think it will help you.
3 Posted by foodyi on 05 Mar, 2014 03:47 PM
to ishaban , I had used this utilities in my project, i think i misunderstand hook. :(
thank you.
Support Staff 4 Posted by Ondina D.F. on 06 Mar, 2014 12:43 PM
Hello,
Let me give you an example of a command with guards and hooks.
Consider this mapping:
LoadImagesCommand:
UserGuard:
LoggerHook:
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:
The order of execution for user foodyi is:
...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.
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 Posted by foodyi on 06 Mar, 2014 01:20 PM
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!
Support Staff 6 Posted by Ondina D.F. on 06 Mar, 2014 01:55 PM
If I understand correctly, the flow should be:
In this case you don't need a hook.
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 Posted by foodyi on 07 Mar, 2014 02:20 AM
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.
Support Staff 8 Posted by Ondina D.F. on 07 Mar, 2014 09:50 AM
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...
Support Staff 9 Posted by creynders on 12 Mar, 2014 03:37 PM
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!
creynders closed this discussion on 12 Mar, 2014 03:37 PM.