]> git.r.bdr.sh - rbdr/custom_event_support.lua/blob - README.md
286448288ebd148c012b37c65e346af1a371b2ef
[rbdr/custom_event_support.lua] / README.md
1 # custom_event_support.lua #
2
3 This is a lua port of CustomEventSupport.js by @azendal. It's a
4 simple implementation of the observer pattern.
5
6 ## Dependencies ##
7
8 * [Middleclass](http://github.com/kikito/middleclass)
9
10 ## Usage ##
11
12 Step 1. Require it somewhere
13
14 ```lua
15 CustomEvent = require('custom_event')
16 CustomEventSupport = require('custom_event_support')
17 ```
18
19 Step 2. Include it in a class
20
21 ```
22 local MyClass = class('MyClass')
23 MyClass:include(CustomEventSupport)
24 ```
25
26 Step 3a. Bind events
27
28 ```lua
29 -- Bind them to the class
30 MyClass:bind('click', function (self, ev)
31 print("Hey, I caught the event")
32 end)
33
34
35 -- or bind them to an instance
36 local my_instance = MyClass:new()
37 my_instance:bind('click', function (self, ev)
38 print("Hey, I caught another event: "..tostring(ev.message))
39 end)
40 ```
41
42 Step 3b. Dispatch events
43
44 ```lua
45 -- Dispatch them from the class. You can optionally pass data as a
46 table.
47 MyClass:dispatch('click')
48
49 -- or dispatch them from an instance
50 my_instance:dispatch('click', {message = "Instance event!"})
51 ```
52
53 That's it. Enjoy :)