diff --git a/CHANGELOG.md b/CHANGELOG.md index 08de3bef..0294b13e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - context wrapper is more capable with native context. So you can use `context.fillStyle` property in your `sceneFunc` without accessing native context. - `toDataURL` now handle pixelRatio. So image for stage 400x400 for retina will be 800x800. +- Correct `clone()` for custom nodes ## [0.9.0][2015-02-27] diff --git a/src/Node.js b/src/Node.js index 9e098a95..b7f89a8d 100644 --- a/src/Node.js +++ b/src/Node.js @@ -1340,7 +1340,7 @@ attrs[key] = obj[key]; } - var node = new Konva[className](attrs); + var node = new this.constructor(attrs); // copy over listeners for(key in this.eventListeners) { allListeners = this.eventListeners[key]; diff --git a/src/Util.js b/src/Util.js index 97637c14..f3b42cd2 100644 --- a/src/Util.js +++ b/src/Util.js @@ -835,18 +835,20 @@ } }, extend: function(child, parent) { - function Ctor() { - this.constructor = child; - } + function Ctor() { + this.constructor = child; + } Ctor.prototype = parent.prototype; - var old_proto = child.prototype; - child.prototype = new Ctor(); - for (var key in old_proto) { - if (old_proto.hasOwnProperty(key)) { - child.prototype[key] = old_proto[key]; - } + var old_proto = child.prototype; + child.prototype = new Ctor(); + for (var key in old_proto) { + if (old_proto.hasOwnProperty(key)) { + child.prototype[key] = old_proto[key]; } - child.__super__ = parent.prototype; + } + child.__super__ = parent.prototype; + // create reference to parent + child.super = parent; }, /** * adds methods to a constructor prototype diff --git a/test/unit/Shape-test.js b/test/unit/Shape-test.js index 1788a859..04c86cfb 100644 --- a/test/unit/Shape-test.js +++ b/test/unit/Shape-test.js @@ -1178,4 +1178,28 @@ suite('Shape', function() { }); + test('clone custom shape', function() { + var className = 'myCustomName' + var CustomShape = function() { + CustomShape.super.apply(this, arguments); + this.className = className; + } + + CustomShape.prototype.foo = function() {}; + + Konva.Util.extend(CustomShape, Konva.Shape); + + var myShape = new CustomShape({ + fill : 'grey' + }); + + var clone = myShape.clone(); + assert.equal(clone instanceof CustomShape, true); + assert.equal(clone instanceof Konva.Shape, true); + assert.equal(clone.className, className); + assert.equal(clone.fill(), 'grey'); + assert.equal(clone.foo, CustomShape.prototype.foo); + }); + + }); \ No newline at end of file