Skip to content

04. Hooks

Pete Shand edited this page Aug 3, 2018 · 1 revision

Hooks run before or after certain extension actions. They are typically used by extensions to run custom actions based on environmental conditions.

Hook Forms

A hook can exist in one of three forms:

  • Function
  • Object
  • Class

Function Hooks

A function hook is simply invoked when the time is right:

function randomHook():Void {
   trace("hooked!");
}

Object Hooks

An object hook must expose a "hook" method:

class SomeHook
{
   public function hook():Void
   {
      trace("hooked!");
   }
}

The "hook" method will be called each time the hook runs. The object will not be injected into.

Class Hooks

A class reference can be used as a hook:

class SomeOtherHook
{
   @inject public var someModel:SomeModel;

   public function hook():Void
   {
      someModel.enabled = false;
   }
}

A new object will be constructed each time the hook is run. The object will be injected into.