Last active
September 29, 2015 15:15
-
-
Save luissardon/75515c344baabd60adef to your computer and use it in GitHub Desktop.
Modulo manejador de eventos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
Manejador de eventos | |
@class event_listener | |
@main test/index | |
@author Luis Sardon | |
### | |
yOSON.AppCore.addModule "event_listener", (Sb) -> | |
st = { | |
listeners: [] | |
} | |
fn = { | |
on : (eventType, callback) -> | |
unless callback["listener_id"] | |
callback["listener_id"] = utils.uniqueId() | |
listenerId = callback["listener_id"] | |
match = false | |
for listener in st.listeners | |
if listener.eventType is eventType and listener.listenerId is listenerId | |
match = true | |
return false | |
unless match | |
st.listeners.push | |
eventType: eventType | |
listenerId: listenerId | |
callback: callback | |
return | |
off : (eventType, callback) -> | |
listenerId = callback["listener_id"] | |
$(st.listeners).each (i) -> | |
if @.eventType is eventType and @.listenerId is listenerId | |
st.listeners[eventType].splice(i, 1) | |
return | |
return | |
trigger : (eventType, data) -> | |
unless data then data = {} | |
for listener in st.listeners | |
if listener.eventType is eventType | |
listener.callback.call(@, $.extend({}, listener, data)) | |
return | |
} | |
initialize = () -> | |
Sb.events(["event_listener:on"], fn.on, this) | |
Sb.events(["event_listener:off"], fn.off, this) | |
Sb.events(["event_listener:trigger"], fn.trigger, this) | |
return | |
return { | |
init: initialize | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment