You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/lib/events.coffee

27 lines
799 B

11 years ago
@Events =
on: (event, callback) ->
if event.indexOf(' ') >= 0
@on name, callback for name in event.split(' ')
else
((@_callbacks ?= {})[event] ?= []).push callback
@
off: (event, callback) ->
if event.indexOf(' ') >= 0
@off name, callback for name in event.split(' ')
else if (callbacks = @_callbacks?[event]) and (index = callbacks.indexOf callback) >= 0
callbacks.splice index, 1
delete @_callbacks[event] unless callbacks.length
@
trigger: (event, args...) ->
if callbacks = @_callbacks?[event]
callback? args... for callback in callbacks.slice(0)
@trigger 'all', event, args... unless event is 'all'
@
removeEvent: (event) ->
if @_callbacks?
delete @_callbacks[name] for name in event.split(' ')
@