Off can remove all listeners in the same name space by .off(".foobar")

This commit is contained in:
David Johansson
2012-10-04 00:50:04 +02:00
parent 9ba21651a9
commit fa2c8fb88d
4 changed files with 70 additions and 31 deletions

36
dist/kinetic-core.js vendored
View File

@@ -3,7 +3,7 @@
* http://www.kineticjs.com/ * http://www.kineticjs.com/
* Copyright 2012, Eric Rowell * Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses. * Licensed under the MIT or GPL Version 2 licenses.
* Date: Oct 03 2012 * Date: Oct 04 2012
* *
* Copyright (C) 2011 - 2012 by Eric Rowell * Copyright (C) 2011 - 2012 by Eric Rowell
* *
@@ -1341,7 +1341,8 @@ Kinetic.Node.prototype = {
* event types delimmited by a space to remove multiple event * event types delimmited by a space to remove multiple event
* bindings at once such as 'mousedown mouseup mousemove'. * bindings at once such as 'mousedown mouseup mousemove'.
* include a namespace to remove an event binding by name * include a namespace to remove an event binding by name
* such as 'click.foobar'. * such as 'click.foobar'. If you only give a name like '.foobar',
* all events in that namespace will be removed.
* @name off * @name off
* @methodOf Kinetic.Node.prototype * @methodOf Kinetic.Node.prototype
* @param {String} typesStr * @param {String} typesStr
@@ -1356,17 +1357,14 @@ Kinetic.Node.prototype = {
var parts = event.split('.'); var parts = event.split('.');
var baseEvent = parts[0]; var baseEvent = parts[0];
if(this.eventListeners[baseEvent] && parts.length > 1) { if(parts.length > 1) {
var name = parts[1]; if (baseEvent){
if (this.eventListeners[baseEvent]) {
for(var i = 0; i < this.eventListeners[baseEvent].length; i++) { this._off(baseEvent, parts[1]);
if(this.eventListeners[baseEvent][i].name === name) { }
this.eventListeners[baseEvent].splice(i, 1); } else {
if(this.eventListeners[baseEvent].length === 0) { for(var type in this.eventListeners) {
delete this.eventListeners[baseEvent]; this._off(type, parts[1]);
break;
}
i--;
} }
} }
} }
@@ -2035,6 +2033,18 @@ Kinetic.Node.prototype = {
this.setAttr('scale', pos); this.setAttr('scale', pos);
}, },
_off: function(type, name) {
for(var i = 0; i < this.eventListeners[type].length; i++) {
if(this.eventListeners[type][i].name === name) {
this.eventListeners[type].splice(i, 1);
if(this.eventListeners[type].length === 0) {
delete this.eventListeners[type];
break;
}
i--;
}
}
},
_clearTransform: function() { _clearTransform: function() {
var trans = { var trans = {
x: this.attrs.x, x: this.attrs.x,

File diff suppressed because one or more lines are too long

View File

@@ -118,7 +118,8 @@ Kinetic.Node.prototype = {
* event types delimmited by a space to remove multiple event * event types delimmited by a space to remove multiple event
* bindings at once such as 'mousedown mouseup mousemove'. * bindings at once such as 'mousedown mouseup mousemove'.
* include a namespace to remove an event binding by name * include a namespace to remove an event binding by name
* such as 'click.foobar'. * such as 'click.foobar'. If you only give a name like '.foobar',
* all events in that namespace will be removed.
* @name off * @name off
* @methodOf Kinetic.Node.prototype * @methodOf Kinetic.Node.prototype
* @param {String} typesStr * @param {String} typesStr
@@ -133,17 +134,14 @@ Kinetic.Node.prototype = {
var parts = event.split('.'); var parts = event.split('.');
var baseEvent = parts[0]; var baseEvent = parts[0];
if(this.eventListeners[baseEvent] && parts.length > 1) { if(parts.length > 1) {
var name = parts[1]; if (baseEvent){
if (this.eventListeners[baseEvent]) {
for(var i = 0; i < this.eventListeners[baseEvent].length; i++) { this._off(baseEvent, parts[1]);
if(this.eventListeners[baseEvent][i].name === name) { }
this.eventListeners[baseEvent].splice(i, 1); } else {
if(this.eventListeners[baseEvent].length === 0) { for(var type in this.eventListeners) {
delete this.eventListeners[baseEvent]; this._off(type, parts[1]);
break;
}
i--;
} }
} }
} }
@@ -812,6 +810,18 @@ Kinetic.Node.prototype = {
this.setAttr('scale', pos); this.setAttr('scale', pos);
}, },
_off: function(type, name) {
for(var i = 0; i < this.eventListeners[type].length; i++) {
if(this.eventListeners[type][i].name === name) {
this.eventListeners[type].splice(i, 1);
if(this.eventListeners[type].length === 0) {
delete this.eventListeners[type];
break;
}
i--;
}
}
},
_clearTransform: function() { _clearTransform: function() {
var trans = { var trans = {
x: this.attrs.x, x: this.attrs.x,

View File

@@ -665,11 +665,11 @@ Test.prototype.tests = {
test(stage.names['myRect'][0].getName() === 'myRect', 'rect name not in names hash'); test(stage.names['myRect'][0].getName() === 'myRect', 'rect name not in names hash');
circle.setId('newCircleId'); circle.setId('newCircleId');
test(stage.ids['newCircleId'].getId() === 'newCircleId', 'circle not in ids hash'); test(stage.ids['newCircleId'] !== undefined, 'circle not in ids hash');
test(stage.ids['myCircle'] === undefined, 'old circle id key is still in ids hash'); test(stage.ids['myCircle'] === undefined, 'old circle id key is still in ids hash');
rect.setName('newRectName'); rect.setName('newRectName');
test(stage.names['newRectName'][0].getName() === 'newRectName', 'new rect name not in names hash'); test(stage.names['newRectName'][0] !== undefined, 'new rect name not in names hash');
test(stage.names['myRect'] === undefined, 'old rect name is still in names hash'); test(stage.names['myRect'] === undefined, 'old rect name is still in names hash');
}, },
'STAGE - set shape and layer opacity to 0.5': function(containerId) { 'STAGE - set shape and layer opacity to 0.5': function(containerId) {
@@ -3955,6 +3955,25 @@ Test.prototype.tests = {
circle.off('click.bar'); circle.off('click.bar');
test(circle.eventListeners['click'] === undefined, 'circle should have no click listeners'); test(circle.eventListeners['click'] === undefined, 'circle should have no click listeners');
/*
* test remove all events in name space
*/
circle.on('click.foo', function() { });
circle.on('click.foo', function() { });
circle.on('touch.foo', function() { });
circle.on('click.bar', function() { });
circle.on('touch.bar', function() { });
test(circle.eventListeners['click'].length === 3, 'circle should have 3 click listeners');
test(circle.eventListeners['touch'].length === 2, 'circle should have 2 touch listeners');
circle.off('.foo');
test(circle.eventListeners['click'].length === 1, 'circle should have 1 click listener');
test(circle.eventListeners['touch'].length === 1, 'circle should have 2 touch listeners');
circle.off('.bar');
test(circle.eventListeners['click'] === undefined, 'circle should have no click listeners');
test(circle.eventListeners['touch'] === undefined, 'circle should have no touch listeners');
stage.add(layer); stage.add(layer);
layer.add(circle); layer.add(circle);
layer.draw(); layer.draw();