Corrected behaviour of the collections apply method.

Changed collections each-method to use same syntax as jQuery - function(index, element) and also being able to use 'this' to get the element.
Added test for apply('on',...) since it needs more than one argument.
This commit is contained in:
David Johansson 2012-09-24 00:05:34 +02:00
parent 9f5d0fe351
commit 45d61a0eb6
5 changed files with 69 additions and 19 deletions

18
dist/kinetic-core.js vendored
View File

@ -1052,11 +1052,15 @@ Kinetic.Collection.prototype = new Array();
* @param {String} method
* @param val
*/
Kinetic.Collection.prototype.apply = function(method, val) {
for (var n=0; n<this.length; n++) {
this[n][method](val);
}
};
Kinetic.Collection.prototype.apply = function(method) {
args = [].slice.call(arguments);
args.shift();
for (var n=0; n<this.length; n++) {
if(Kinetic.Type._isFunction(this[n][method])) {
this[n][method].apply(this[n],args);
}
}
};
/**
* iterate through node array
* @name each
@ -1065,7 +1069,7 @@ Kinetic.Collection.prototype.apply = function(method, val) {
*/
Kinetic.Collection.prototype.each = function(func) {
for (var n=0; n<this.length; n++) {
func(this[n]);
func.call(this[n],n,this[n]);
}
};
///////////////////////////////////////////////////////////////////////
@ -2689,7 +2693,7 @@ Kinetic.Container.prototype = {
* get all shapes inside container
*/
_getNodes: function(sel) {
var arr = [];
var arr = new Kinetic.Collection();
function traverse(cont) {
var children = cont.getChildren();
for(var n = 0; n < children.length; n++) {

File diff suppressed because one or more lines are too long

View File

@ -229,7 +229,7 @@ Kinetic.Container.prototype = {
* get all shapes inside container
*/
_getNodes: function(sel) {
var arr = [];
var arr = new Kinetic.Collection();
function traverse(cont) {
var children = cont.getChildren();
for(var n = 0; n < children.length; n++) {

View File

@ -21,11 +21,15 @@ Kinetic.Collection.prototype = new Array();
* @param {String} method
* @param val
*/
Kinetic.Collection.prototype.apply = function(method, val) {
for (var n=0; n<this.length; n++) {
this[n][method](val);
}
};
Kinetic.Collection.prototype.apply = function(method) {
args = [].slice.call(arguments);
args.shift();
for (var n=0; n<this.length; n++) {
if(Kinetic.Type._isFunction(this[n][method])) {
this[n][method].apply(this[n],args);
}
}
};
/**
* iterate through node array
* @name each
@ -34,6 +38,6 @@ Kinetic.Collection.prototype.apply = function(method, val) {
*/
Kinetic.Collection.prototype.each = function(func) {
for (var n=0; n<this.length; n++) {
func(this[n]);
func.call(this[n],n,this[n]);
}
};

View File

@ -618,7 +618,7 @@ Test.prototype.tests = {
test(Kinetic.Global.shapes[circleColorKey] === undefined, 'circle color key should not be in shapes hash');
test(Kinetic.Global.shapes[rectColorKey] === undefined, 'rect color key should not be in shapes hash');
},
'*SELECTOR - show and hide an array of nodes': function(containerId) {
'SELECTOR - set x on an array of nodes': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
@ -658,10 +658,52 @@ Test.prototype.tests = {
layer.draw();
shapes.each(function(shape) {
test(shape.getX() === 200, 'shape x should be 200');
shapes.each(function() {
test(this.getX() === 200, 'shape x should be 200');
});
},
'SELECTOR - add listener to an array of nodes': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'myShape'
});
var rect = new Kinetic.Rect({
x: 300,
y: 100,
width: 100,
height: 50,
fill: 'purple',
stroke: 'black',
strokeWidth: 4,
name: 'myShape'
});
layer.add(circle);
layer.add(rect);
stage.add(layer);
var shapes = layer.get('.myShape');
test(shapes.length === 2, 'shapes array should have 2 elements');
var a = 0;
shapes.apply('on', 'mouseover', function () {a++;});
circle.simulate('mouseover');
test(a === 1, 'listener should have fired for circle');
rect.simulate('mouseover');
test(a === 2, 'listener should have fired for rect');
},
'STAGE - test ids and names hashes': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,