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