1 # custom_event_support.lua #
3 This is a lua port of CustomEventSupport.js by @azendal. It's a
4 simple implementation of the observer pattern.
8 [Middleclass][https://github.com/kikito/middleclass]
12 Step 1. Require it somewhere
15 CustomEvent = require('custom_event')
16 CustomEventSupport = require('custom_event_support')
19 Step 2. Include it in a class
22 local MyClass = class('MyClass')
23 MyClass:include(CustomEventSupport)
29 -- Bind them to the class
30 MyClass:bind('click', function (self, ev)
31 print("Hey, I caught the event")
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))
42 Step 3b. Dispatch events
45 -- Dispatch them from the class. You can optionally pass data as a
47 MyClass:dispatch('click')
49 -- or dispatch them from an instance
50 my_instance:dispatch('click', {message = "Instance event!"})