Guards and Hooks

Seth Gilmer's Avatar

Seth Gilmer

20 Jan, 2013 05:04 AM

Guards and Hooks… what are they? For what reason we should use them? Is there any articles/examples to see them in action? I've tried to find it, but didn't understand at all.

  1. Support Staff 1 Posted by Ondina D.F. on 21 Jan, 2013 04:10 PM

    Ondina D.F.'s Avatar

    Hi Seth,

    You’re right, there are no examples (yet) showing how to use guards and hooks.

    For now, you could take a look at the readme files and the tests on github to get a general idea about guards and hooks:

    https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...

    https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...

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

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

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

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

    I haven’t used guards and hooks until now. A really silly example here:

    In context config:

    const expectedViewWidth:Number = 100;
    const expectedViewHeight:Number = 200;
                
    injector.map(Number, 'rectHeight').toValue(expectedViewHeight);
    injector.map(Number, 'rectWidth').toValue(expectedViewWidth);
                                    
    _viewProcessorMap.map(HookedView).toNoProcess().withHooks(HookedMediator).withGuards(HookedMediator);
    
    public class HookedMediator extends Mediator
    {       
        [Inject]
        public var view:HookedView;
            
        [Inject(name="rectWidth")]
        public var rectWidth:Number;
            
        [Inject(name="rectHeight")]
        public var rectHeight:Number;
                    
        override public function initialize():void
        {
            trace("HookedMediator.initialize()");//[3]        
        }
        public function hook():void
        {//HOOK 
            trace("HookedMediator.hook()");//[2]      
            view.graphics.beginFill(0x00CC00);
            view.graphics.drawRect(0, 0, rectWidth, rectHeight);
        }
        public function approve():Boolean
        {//GUARD
            trace("HookedMediator.approve()");//[1]       
            return (view.name =="Bender");
        }
    }
    

    In plain text: if View’s name is Bender, draw a rectangle before running Mediator’s initialize().
    The guard checks if a certain condition has been met (view.name =="Bender")
    I created a guard inside the Mediator, but any class with an approve() method can be used to check against different conditions.

    _viewProcessorMap.map(HookedView).toNoProcess().withHooks(HookedMediator).withGuards(IsViewVisible);

    IsViewVisible could have an aprove() method like this:

    public function approve():Boolean
    { return (view.visible); }

    HTH
    Ondina

    P.S. Adding Shaun as a guard to approve my example or to talk about more complex scenarios ;-)
    discussionProcessorMap.map(MySillyExample).toNoProcess().withGuards(ShaunSmith);

  2. 2 Posted by Seth Gilmer on 22 Jan, 2013 07:41 AM

    Seth Gilmer's Avatar

    Thank you, Ondina.
    It was really helpful. Now I see basic idea.

  3. Support Staff 3 Posted by Ondina D.F. on 22 Jan, 2013 10:00 AM

    Ondina D.F.'s Avatar

    Seth, you’re welcome!!
    I’m closing this discussion for now, but please don’t hesitate to ask more questions in another thread or to re-open this, if need be.

    Ondina

  4. Ondina D.F. closed this discussion on 22 Jan, 2013 10:00 AM.

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