Changes from Version 1 of documentation/LuaManual/AttachingToEvents

Show
Ignore:
Author:
gambini (IP: 96.38.104.126)
Timestamp:
05/19/12 08:02:05 (5 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • documentation/LuaManual/AttachingToEvents

    v0 v1  
     1= Attaching To Events = 
     2 
     3 
     4== Statically in RML == 
     5The easiest way to attach to events with Lua is to write your code directly into the RML files, using the on* attributes. When the event is fired three global variables are set up, ''document'', ''event'' and ''element''. 
     6 
     7||[wiki:documentation/LuaManual/Elements element]||The element that is currently being processed|| 
     8||[wiki:documentation/LuaManual/Documents document]||The document the element that is currently being processed belongs to|| 
     9||[wiki:documentation/LuaManual/Events event]||The event that is currently being processed|| 
     10 
     11Example: 
     12{{{ 
     13<button onclick="print('Clicked!')"/> 
     14}}} 
     15 
     16All standard Lua semantics apply to inline event scripts. You can have multiple statements on one line separated by a space or a semicolon. 
     17 
     18Example: 
     19{{{ 
     20<button onclick="print('Line 1');print('Line 2') print('Line 3')"/> 
     21}}} 
     22 
     23 
     24== Dynamically from Lua Code == 
     25 
     26The Lua version of AddEventListener is modeled directly on Javascript. This allows you to bind any callable Lua object (free function or method) or string to an event. 
     27 
     28Method 1: 
     29{{{ 
     30element = document.GetElementById('button') 
     31element.AddEventListener('click', "print('Line 1');print('Line 2') print('Line 3')", true) 
     32}}} 
     33 
     34Method 2: 
     35{{{ 
     36function OnClick() 
     37  local i = 0 
     38  while i < 10 do 
     39    print('Line ' .. i) 
     40    i = i + 1 
     41  end 
     42end 
     43 
     44element = document.GetElementById('button') 
     45element.AddEventListener('click', OnClick, true) 
     46}}} 
     47 
     48The same rules for inline event scripts apply when a string is passed as the second argument.  
     49 
     50However, if a function is passed in, then there is one caveat. If the function requires one of the local variables (event,element,document), then that variable ''must'' be in the correct position in the parameters list. The order is the order in the previous sentence: event first, element second, document third. If you only need event, then you can have the function only take one argument. If you need only document, then you must have the other two parameter slots before the document parameter. And, because it is in Lua, they may have any name.  
     51 
     52{{{ 
     53--a function needing only event 
     54function printEvent(event) print(event) end 
     55 
     56--a function needing only element, and wanting to use its own name 
     57function printElement(_, invader) print(invader) end 
     58 
     59--a function needing only document, but naming the other arguments just in case they are needed later 
     60function printDocument(event,element,document) print(document) end 
     61}}}