Permalink
Browse files

use jQuery 1.7.1 Callbacks object to implement event firers

  • Loading branch information...
1 parent 14abb3c commit 19aa1e1a8d35912b75d24a1ee89033739ca26f8e @jgsmith committed May 15, 2012
Showing with 23 additions and 48 deletions.
  1. +23 −48 src/core.coffee
View
@@ -275,46 +275,53 @@ MITHGrid.initSynchronizer = (callbacks) ->
#
# * isPreventable - true if a listener can prevent further listeners from receiving the event
# * isUnicast - true if only one listener receives the event
+# * hasMemory - true if the event firer fires for each value it has been fired for when a new listener is added
#
# Returns:
#
# The EventFirer object.
#
-MITHGrid.initEventFirer = (isPreventable, isUnicast) ->
- that = {}
- that.isPreventable = isPreventable
- that.isUnicast = isUnicast
- listeners = []
+MITHGrid.initEventFirer = (isPreventable, isUnicast, hasMemory) ->
+ that =
+ isPreventable: !!isPreventable
+ isUnicast: !!isUnicast
+ hasMemory: !!hasMemory
+
+ callbackFlags = []
+
+ if that.isPreventable
+ callbackFlags.push "stopOnFalse"
+ if that.isUnicast
+ callbackFlags.push "unique"
+ if that.hasMemory
+ callbackFlags.push "memory"
+ callbacks = $.Callbacks(callbackFlags.join(" "))
+
# ### #addListener
#
# Adds a listener to an event.
#
# Parameters:
#
# * listener - function to call when event fires
- # * namespace - optional namespace parameter that can be used to remove listeners
#
# Returns: Nothing.
#
- that.addListener = (listener, namespace) ->
- listeners.push [listener, namespace]
+ that.addListener = (listener) -> callbacks.add listener
+
# ### #removeListener
#
# Removes a listener (or set of listeners) from an event firer.
#
# Parameters:
#
- # * listener - function or string to remove from list of listeners
+ # * listener - function to remove from list of listeners
#
# Returns: Nothing.
#
- that.removeListener = (listener) ->
- if typeof listener == "string"
- listeners = (l for l in listeners when l[1] != listener)
- else
- listeners = (l for l in listeners when l[0] != listener)
+ that.removeListener = (listener) -> callbacks.remove listener
# ### #fire
#
@@ -344,39 +351,7 @@ MITHGrid.initEventFirer = (isPreventable, isUnicast) ->
#
# If neither unicast nor preventabe, then fire() will return "true" regardless of how many listeners are called.
#
- if isUnicast
- that.fire = (args...) ->
- if listeners.length > 0
- try
- listeners[0][0](args...)
- catch e
- console.log e
- else
- true
- else if isPreventable
-
- that.fire = (args...) ->
- r = true
- for listener in listeners
- l = listener[0]
- try
- r = l(args...)
- catch e
- console.log e
- if r == false
- return false
- true
- else
-
- that.fire = (args...) ->
- for listener in listeners
- try
- listener[0](args...)
- catch e
- console.log listener[0], args, e
- true
-
-
+ that.fire = (args...) -> callbacks.fire args...
that
@@ -442,7 +417,7 @@ MITHGrid.initInstance = (args...) ->
c = [ c ]
else
c = []
- that.events[k] = MITHGrid.initEventFirer( ("preventable" in c), ("unicast" in c))
+ that.events[k] = MITHGrid.initEventFirer( ("preventable" in c), ("unicast" in c), ("memory" in c) )
# ### #addVariable
#

0 comments on commit 19aa1e1

Please sign in to comment.