Node.off() error
Event listeners are attached using the jQuery .on() method. These seem to work fine. However, when removing the event listeners, Kinetic.Node requires that the events be listed in a space-separated string. Since default listeners are idChange and nameChange (not sure where these get set but if they're in a variable somewhere already, that makes it even easier), I looked into a way to remove all event listeners previously attached by simply using Node.off().
I know off() just got some work done for the latest version, but apparently this didn't get addressed. After looking over the Kinetic.Node.off section, I came up with this quick and easy fix. Feel free to modify accordingly:
off: function(typesStr) {
if(!typesStr){
typesStr = '';
for(var key in this.eventListeners) {
if(key !== 'idChange' && key !== 'nameChange') {
if(typesStr.length) {
typesStr = typesStr + ' ' + key;
} else {
typesStr = key;
}
}
}
}
var types = typesStr.split(' ');
...
don't use jquery to add events to KineticJS - KineticJS nodes are virtual nodes, not DOM nodes. jQuery is a DOM library. you need to use KineticJS event binding, e.g.
shape.on('click', ...);
Sorry, Eric, I think we both misunderstood. Of course Kinetic doesn't use jQuery, what I meant was Kinetic has similar functions to jQuery in that it uses both on() and off() for event handlers. What I was proposing was that Kinetic not require a space-separated list of events previously bound using on() in order to remove them using off(), and instead it simply assumes that if no string is supplied (typesStr) that it remove all user-attached events from the Node.
ah interesting okay, that makes more sense. Does jQuery do this? I suppose this is needed in order to make destructors? Reopening ticket, thanks.
Yes. jQuery uses off() to clear all, or off(event) to clear a single listener. Ironically, because jQuery also uses selectors, your event.nameworks in the same way jQuery does as well. I ran into this issue when removing a group that shared its name with another group where the removed group had events attached. My functions were still listening, so I tried off() prior to removing and because I didn't pass named events it threw an error. So I tried my fix but before doing so I checked the eventListeners node so I didn't remove something I hadn't set myself. Sorry for the confusion. On Jan 15, 2013 4:43 PM, "Eric Rowell" [email protected] wrote:
ah interesting okay, that makes more sense. Does jQuery do this? I suppose this is needed in order to make destructors? Reopening ticket, thanks.
— Reply to this email directly or view it on GitHubhttps://github.com/ericdrowell/KineticJS/issues/229#issuecomment-12290998.
close by https://github.com/ericdrowell/KineticJS/commit/16511a9aefa28eb1102fe85b39335359e4cf3c6a