Documentation


SEARCH

TABLE OF CONTENT

    1. Getting started 2. Basic concepts 3. Request dispatching 4. Context container 5. Dual MVC 6. Component model: 7. Security 8. Configuration 9. Session handling 10. I18n 11. Cache 12. Logging 13. Error handling 14. Advanced Topics 15. API reference

      Tutorials: Frequently Asqued Questions

      See also:


      Event Handlers

      Handling component events

      Table of Contents

      The Event Management System

      The event management system is one of the most important pieces within the component model since is the one in charge of performing the following tasks:

      • To notify to the server all the events raised in the client (i.e. a click on a button or a text control change)
      • To synchronize the component status between the client and the server (i.e. update the component text property once it has been changed in the client)

      From the server point of view, it's transparent the fact that an event has been raised from the client side.

      Life-cycle of the event management system

      The following figure ilustrates the life-cycle of the event management system:

      1. When an event is raised from a component in the client-side, it is sent to the server (usually via AJAX).
      2. Once the request is received, the Front Controller delegates to the associated Event Handler, a subclass of __EventHandler which executes some logic associated to the given event.
      3. Finally, components are updated in the client-side according to the server status after the event has been handled.

      The communication process between client and server is performed usually via Ajax plus JSON.
      However, this communication is handled entirely by the framework, so we don't need to care about low-level details regarding how this process is being done.

      The Event Handler

      The way that lion proposes to handle client events is by defining Event Handlers associated to views. An event handler is a class in charge of events management for concrete views.

      We just need to define subclasses of __EventHandler and follow a naming convention for methods.

      Naming convention and location

      Lion is enough flexible to allow whatever name and location for your event handlers. There are not restrictions in that sense.
      However there are some naming conventions and predefined locations that ease the definition and enablement of event handlers:

      • Use the view code plus the suffix 'EventHandler' to define an event handler associated to a concrete view.
      • Define each event handler in a single file with the same name of the event handler class followed by the suffix '.class.php'
      • Place your event handlers in the /libs/eventhandlers directory

      Associating an event handler to a view

      First thing that we need to know in order to define event handlers is the fact that each event handle need to be associated to a view at least.

      We can do it declarativelly in the view-definitions configuration section by defining the eventHandlerClass view property.

      i.e.

      1. <?xml version "1.0" standalone="yes"?>
      2.  
      3. <configuration>
      4.  
      5.   <view-definitions>
      6.   
      7.     <view code="fooBarView" class="__SmartyView">
      8.       <property name="template" value="fooBar.tpl"/>
      9.       <property name="eventHandlerClass" value="FooBarEventHandler"/> 
      10.     </view>
      11.   
      12.   </view-definitions>
      13.   
      14. </configuration>
      In this example, we are associating the class FooBarEventHandler to the view identified by the code fooBar.

      However, there is a rule in the views.xml by default that simplifies this task by just following a naming convention:

      1. <?xml version "1.0" standalone="yes"?>
      2.  
      3. <configuration>
      4.  
      5.   <view-definitions>
      6.   
      7.     <view code="*" class="__SmartyView">
      8.       <property name="template" value="*.tpl"/>
      9.       <property name="eventHandlerClass" value="*EventHandler"/> 
      10.     </view>
      11.   
      12.   </view-definitions>
      13.   
      14. </configuration>
      This rule associates an event handler to a view if we define the name of the event handler as the view code plus the suffix EventHandler.

      i.e., for the view fooBar we'll define the event handler class as FooBarEventHandler. Note that the mapping between the view code and the class name is case insensitive.


      Event handler file and location

      The file app/config/includepath.xml contains the following mapping rule:

      1. <?xml version "1.0" standalone="yes"?>
      2. <classes>
      3.  
      4.   ...
      5.  
      6.   <cluster name="Event Handlers" path="/libs/eventhandlers">
      7.     <class name="*" file="*.class.php"/>
      8.   </cluster>
      9.  
      10.   ...
      11.   
      12. </classes>

      This mapping rule let Lion know that any file placed in libs/eventhandlers with the suffix '.class.php' corresponds to a class with the same name without the suffix.

      So, by placing event handler files in that directory and following the naming convention, it's not needed to declare explicitly the location of your event handlers in the includepath.xml file.


      Defining Event Hadlers

      The way to define event handlers is by implementing the __IEventHandler class. However, Lion proposes a class that already implements this interface: The __EventHandler class.

      By subclassing the __EventHandler class, we just need to define a method for each component event that we need to handle taking into account the following naming convention for the method name: 'componentName' + '_' + 'eventName' (i.e. myButton_click, myTextBox_blur, ...).

      And again: this naming convention belong to the __EventHandler. If you implement the __IEventHandler then you should develop your own ways to handle events.

      i.e.

      1. <?php
      2.  
      3. class FooBarEventHandler extends __EventHandler {
      4.  
      5.     /**
      6.      * Set the 'FoO BaR!' string to the myLabel text
      7.      * property when click the myButton component
      8.      *
      9.      */
      10.     public function myButton_click({
      11.         //retrieve the component myLabel
      12.         $my_label $this->getComponent('myLabel');
      13.         //assign the 'FoO BaR!' to the text property
      14.         $my_label->setText('FoO BaR!');
      15.     }
      16.  
      17.     
      18. }
      In this example, we have define how to handle the click event for a button component.

      As we can also see in this example, one of the most interesting things of __EventHandler class is that allows to retrieve whatever component within the same view by calling to the __EventHandler::getComponent() method.

      Try The Hello World Deluxe. It contains a practical tutorial regarding components and event handlers.

      To see information regarding events handled in lion, see the Lion Events section