in addition to Collection each() and apply(), I wanted to make it easier to attach event listeners to node collections, so I went with David Johansson's approach for whitelisting on() and off().

The difference now is that the on() and off() methods are dynamically
added to the Collections prototype from Node, which acts as a wrapper
around each()
This commit is contained in:
ericdrowell
2012-09-25 13:38:36 -07:00
parent d522b9d9d0
commit ff926b34af
4 changed files with 129 additions and 96 deletions

34
dist/kinetic-core.js vendored
View File

@@ -1426,11 +1426,11 @@ Kinetic.Node.prototype = {
* @param {Object} config
*/
setAttrs: function(config) {
if (config) {
for (var key in config) {
if(config) {
for(var key in config) {
var method = 'set' + key.charAt(0).toUpperCase() + key.slice(1);
// use setter if available
if (Kinetic.Type._isFunction(this[method])) {
if(Kinetic.Type._isFunction(this[method])) {
this[method](config[key]);
}
// otherwise set directly
@@ -1507,6 +1507,7 @@ Kinetic.Node.prototype = {
addChildren(nodes);
}
}
if(that.nodeType !== 'Stage') {
addChildren(that.getStage().getChildren());
}
@@ -2030,10 +2031,10 @@ Kinetic.Node.prototype = {
*/
setOffset: function() {
var pos = Kinetic.Type._getXY([].slice.call(arguments));
if (pos.x === undefined) {
if(pos.x === undefined) {
pos.x = this.getOffset().x;
}
if (pos.y === undefined) {
if(pos.y === undefined) {
pos.y = this.getOffset().y;
}
this.setAttr('offset', pos);
@@ -2048,10 +2049,10 @@ Kinetic.Node.prototype = {
setScale: function() {
var pos = Kinetic.Type._getXY([].slice.call(arguments));
if (pos.x === undefined) {
if(pos.x === undefined) {
pos.x = this.getScale().x;
}
if (pos.y === undefined) {
if(pos.y === undefined) {
pos.y = this.getScale().y;
}
this.setAttr('scale', pos);
@@ -2104,7 +2105,7 @@ Kinetic.Node.prototype = {
});
},
setAttr: function(key, val) {
if (val !== undefined) {
if(val !== undefined) {
var oldVal = this.attrs[key];
this._fireBeforeChangeEvent(key, oldVal, val);
this.attrs[key] = val;
@@ -2280,6 +2281,22 @@ Kinetic.Node.prototype.isListening = Kinetic.Node.prototype.getListening;
*/
Kinetic.Node.prototype.isDraggable = Kinetic.Node.prototype.getDraggable;
// collection mappings
(function() {
var collectionMappings = ['on', 'off'];
for(var n = 0; n < collectionMappings.length; n++) {
// induce scope
(function(i) {
var method = collectionMappings[i];
Kinetic.Collection.prototype[method] = function() {
var args = [].slice.call(arguments);
args.unshift(method);
this.apply.apply(this, args);
};
})(n);
}
})();
/**
* set node x position
* @name setX
@@ -2413,7 +2430,6 @@ Kinetic.Node.prototype.isDraggable = Kinetic.Node.prototype.getDraggable;
* @name getListening
* @methodOf Kinetic.Node.prototype
*/
///////////////////////////////////////////////////////////////////////
// Container
///////////////////////////////////////////////////////////////////////

File diff suppressed because one or more lines are too long

View File

@@ -203,11 +203,11 @@ Kinetic.Node.prototype = {
* @param {Object} config
*/
setAttrs: function(config) {
if (config) {
for (var key in config) {
if(config) {
for(var key in config) {
var method = 'set' + key.charAt(0).toUpperCase() + key.slice(1);
// use setter if available
if (Kinetic.Type._isFunction(this[method])) {
if(Kinetic.Type._isFunction(this[method])) {
this[method](config[key]);
}
// otherwise set directly
@@ -284,6 +284,7 @@ Kinetic.Node.prototype = {
addChildren(nodes);
}
}
if(that.nodeType !== 'Stage') {
addChildren(that.getStage().getChildren());
}
@@ -807,10 +808,10 @@ Kinetic.Node.prototype = {
*/
setOffset: function() {
var pos = Kinetic.Type._getXY([].slice.call(arguments));
if (pos.x === undefined) {
if(pos.x === undefined) {
pos.x = this.getOffset().x;
}
if (pos.y === undefined) {
if(pos.y === undefined) {
pos.y = this.getOffset().y;
}
this.setAttr('offset', pos);
@@ -825,10 +826,10 @@ Kinetic.Node.prototype = {
setScale: function() {
var pos = Kinetic.Type._getXY([].slice.call(arguments));
if (pos.x === undefined) {
if(pos.x === undefined) {
pos.x = this.getScale().x;
}
if (pos.y === undefined) {
if(pos.y === undefined) {
pos.y = this.getScale().y;
}
this.setAttr('scale', pos);
@@ -881,7 +882,7 @@ Kinetic.Node.prototype = {
});
},
setAttr: function(key, val) {
if (val !== undefined) {
if(val !== undefined) {
var oldVal = this.attrs[key];
this._fireBeforeChangeEvent(key, oldVal, val);
this.attrs[key] = val;
@@ -1057,6 +1058,22 @@ Kinetic.Node.prototype.isListening = Kinetic.Node.prototype.getListening;
*/
Kinetic.Node.prototype.isDraggable = Kinetic.Node.prototype.getDraggable;
// collection mappings
(function() {
var collectionMappings = ['on', 'off'];
for(var n = 0; n < collectionMappings.length; n++) {
// induce scope
(function(i) {
var method = collectionMappings[i];
Kinetic.Collection.prototype[method] = function() {
var args = [].slice.call(arguments);
args.unshift(method);
this.apply.apply(this, args);
};
})(n);
}
})();
/**
* set node x position
* @name setX

View File

@@ -742,7 +742,7 @@ Test.prototype.tests = {
test(shapes.length === 2, 'shapes array should have 2 elements');
var a = 0;
shapes.apply('on', 'mouseover', function() {
shapes.on('mouseover', function() {
a++;
});
circle.simulate('mouseover');