Message from discussion
Namespaced DOM Events?
Received: by 10.58.91.165 with SMTP id cf5mr2831414veb.34.1344433384008;
Wed, 08 Aug 2012 06:43:04 -0700 (PDT)
X-BeenThere: mootools-users@googlegroups.com
Received: by 10.52.93.40 with SMTP id cr8ls1153004vdb.8.gmail; Wed, 08 Aug
2012 06:43:01 -0700 (PDT)
Received: by 10.52.95.238 with SMTP id dn14mr1979443vdb.1.1344433381914;
Wed, 08 Aug 2012 06:43:01 -0700 (PDT)
Date: Wed, 8 Aug 2012 06:43:00 -0700 (PDT)
From: verylastminute <cgp...@gmail.com>
To: mootools-users@googlegroups.com
Message-Id: <acf1e75c-7520-47a6-a7ac-4cc356adb553@googlegroups.com>
In-Reply-To: <50226CA2.8080604@gmail.com>
References: <aaf952bc-c682-435e-b79d-42acc74a99db@googlegroups.com>
<50226CA2.8080604@gmail.com>
Subject: Re: [Moo] Namespaced DOM Events?
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_494_2904043.1344433380904"
------=_Part_494_2904043.1344433380904
Content-Type: multipart/alternative;
boundary="----=_Part_495_31351784.1344433380904"
------=_Part_495_31351784.1344433380904
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
You are welcome to port the CS version of my solution to JS (haven't
presently got the time).
> 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
>
------=_Part_495_31351784.1344433380904
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<div>You are welcome to port the CS version of my solution to JS (haven't p=
resently got the time).</div><div> </div><blockquote class=3D"gmail_qu=
ote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padd=
ing-left: 1ex;"><div>do -></div><div><br></div><div> splitt=
er =3D ":" # e.g. foo:click</div><div> key =3D "events.namespa=
ce." # e.g. events.namespace.foo</div><div><br></div><div> # d=
o not edit below this line unless you know what you are doing!</div><div><b=
r></div><div> addNamespaceEvent =3D (name, callback, s) -><=
/div><div> [namespace, type] =3D name.split(s or=
splitter)</div><div> cache =3D @retrieve(key + =
namespace) or {}</div><div><br></div><div> cache=
[type] ?=3D []</div><div> cache[type].push(callb=
ack)</div><div><br></div><div> @addEvent(type, c=
allback)</div><div> @store(key + name, cache)</d=
iv><div><br></div><div> addNamespaceEvents =3D (events, s) -&g=
t;</div><div> @addNamespaceEvent(name, callback,=
s) for own name, callback of events</div><div><br></div><div> =
removeNamespaceEvent =3D (name, s) -></div><div> &n=
bsp; [namespace, type] =3D name.split(s or splitter)</div><div>  =
; cache =3D @retrieve(key + namespace) or {}</div><div> =
cache[type] ?=3D []</div><div><br></div><div> &=
nbsp; for own index, callback of cache[type]</div><div> =
@removeEvent(type, callback)</div><div>=
<br></div><div> delete cache[type]</div><div><br=
></div><div> removeNamespaceEvents =3D (namespace, s) -></d=
iv><div> cache =3D @retrieve(key + namespace) or=
{}</div><div><br></div><div> for own type of ca=
che</div><div> @removeNamespaceEve=
nt(namespace + (s or splitter) + type)</div><div><br></div><div> &nbs=
p; @erase(key + namespace)</div><div><br></div><div> &n=
bsp; Element.implement({</div><div> "addNamespac=
eEvent": addNamespaceEvent</div><div> "addNamesp=
aceEvents": addNamespaceEvents</div><div> "remov=
eNamespaceEvent": removeNamespaceEvent</div><div>  =
; "removeNamespaceEvents": removeNamespaceEvents</div><div> })=
</div><div><br></div><div> Window.implement({</div><div> =
"addNamespaceEvent": addNamespaceEvent</div><div>&nbs=
p; "addNamespaceEvents": addNamespaceEvents</div><div>=
"removeNamespaceEvent": removeNamespaceEvent</d=
iv><div> "removeNamespaceEvents": removeNamespac=
eEvents</div><div> })</div><div><br></div><div> D=
ocument.implement({</div><div> "addNamespaceEven=
t": addNamespaceEvent</div><div> "addNamespaceEv=
ents": addNamespaceEvents</div><div> "removeName=
spaceEvent": removeNamespaceEvent</div><div> "re=
moveNamespaceEvents": removeNamespaceEvents</div><div> })</div=
><div><br></div><div> return</div></blockquote>
------=_Part_495_31351784.1344433380904--
------=_Part_494_2904043.1344433380904--