Created
August 16, 2012 13:50
-
-
Save assertchris/3370241 to your computer and use it in GitHub Desktop.
Namespaced Events (Moo/CS)
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
| do -> | |
| splitter = ":" # e.g. foo:click | |
| key = "events.namespace." # e.g. events.namespace.foo | |
| # do not edit below this line unless you know what you are doing! | |
| addNamespaceEvent = (name, callback, s) -> | |
| [namespace, type] = name.split(s or splitter) | |
| cache = @retrieve(key + namespace) or {} | |
| cache[type] ?= [] | |
| cache[type].push(callback) | |
| @addEvent(type, callback) | |
| @store(key + name, cache) | |
| addNamespaceEvents = (events, s) -> | |
| @addNamespaceEvent(name, callback, s) for own name, callback of events | |
| removeNamespaceEvent = (name, s) -> | |
| [namespace, type] = name.split(s or splitter) | |
| cache = @retrieve(key + namespace) or {} | |
| cache[type] ?= [] | |
| for own index, callback of cache[type] | |
| @removeEvent(type, callback) | |
| delete cache[type] | |
| removeNamespaceEvents = (namespace, s) -> | |
| cache = @retrieve(key + namespace) or {} | |
| for own type of cache | |
| @removeNamespaceEvent(namespace + (s or splitter) + type) | |
| @erase(key + namespace) | |
| Element.implement({ | |
| "addNamespaceEvent": addNamespaceEvent | |
| "addNamespaceEvents": addNamespaceEvents | |
| "removeNamespaceEvent": removeNamespaceEvent | |
| "removeNamespaceEvents": removeNamespaceEvents | |
| }) | |
| Window.implement({ | |
| "addNamespaceEvent": addNamespaceEvent | |
| "addNamespaceEvents": addNamespaceEvents | |
| "removeNamespaceEvent": removeNamespaceEvent | |
| "removeNamespaceEvents": removeNamespaceEvents | |
| }) | |
| Document.implement({ | |
| "addNamespaceEvent": addNamespaceEvent | |
| "addNamespaceEvents": addNamespaceEvents | |
| "removeNamespaceEvent": removeNamespaceEvent | |
| "removeNamespaceEvents": removeNamespaceEvents | |
| }) | |
| return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment