Discussion:
Signal & a proposed disconnect signature
Eoghan
2009-05-05 12:43:26 UTC
Permalink
I've previously been surprised by the function signature of
Signal.disconnect ; that it takes an ident, i.e. the return value from
Signal.connect. Usually you'd have to store the ident somewhere and
retrieve it later when you want to disconnect; this isn't always
convenient.

Is the Signal api is missing a version of disconnect which takes the
same function signature as connect?

Here is a possibility:

function disconnectIt(src, sig, objOrFunc, funcOrStr) {
if (typeof(src) == "string") {
src = MochiKit.DOM.getElement(src);
}
var m = MochiKit.Base;
var self = MochiKit.Signal;
var disconnect = self._disconnect;
var observers = self._observers;
var i, ident;
var locked = self._lock;
var dirty = self._dirty;
for (i = observers.length - 1; i >= 0; i--) {
ident = observers[i];
if (ident.source === src && ident.signal === sig &&
ident.objOrFunc === objOrFunc &&
(funcOrStr === null || ident.funcOrStr === funcOrStr)) {
disconnect(ident);
if (!locked) {
observers.splice(i, 1);
} else {
dirty = true;
}
}
}
self._dirty = dirty;
}

This is an amalgam of disconnectAll and disconnectAllTo.

The only disadvantage is that you can't use an inline function for
objOrFunc.

Could the existing 'disconnect' be overloaded in 1.5 with this 3/4
argument version? (There are already a lot of disconnect*s in the
API!)

I can supply a use-case that isn't covered by disconnectAll or
disconnectAllTo if required.

Eoghan

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...