KineticJS icon indicating copy to clipboard operation
KineticJS copied to clipboard

Container.remove() error

Open soulBit opened this issue 14 years ago • 6 comments

When a Container is removed before a child, an error is thrown once remove() is called on the child object - "Uncaught TypeError: Cannot read property '_id' of undefined".

Test sample:

this.mStage = jQuery( "#stage_id" );
this.mCanvas = new Kinetic.Stage({ "container": this.mStage.get(0), "width": 300, "height": 300 }); 

this.mTestLayer = new Kinetic.Layer();
this.mTestShape = new Kinetic.Text({ "text": "Hello, world.", "textFill": "white", "fontFamily": "Arial", "fontStyle": "bold", "fontSize": 12 });

this.mTestLayer.add( this.mTestShape );
this.mCanvas.add( this.mTestLayer );

this.mTestLayer.remove();
this.mTestShape.remove();

soulBit avatar Nov 04 '12 21:11 soulBit

I would think this is actually a logical response, since removing the parent also removes all children contained within it. This applies to groups and layers both. Once the parent is removed, assigning the child to a variable or console logging the child would result in an undefined value. I don't think KineticJS should have to test for whether a shape is defined or not before removing it - that's up to the coder using the library.

lazerblade01 avatar Nov 05 '12 12:11 lazerblade01

@lazerblade01 I think I probably didn't provide enough detail here - the issue is that when "container.remove()" is called, it removes the Node from its children, but the reference to the parent is never unset from the child. Then when "child.remove()" is called, an error is thrown because it thinks that it is still the child of the container (and tries to access an out of bounds index in there. The ideal behaviour would be for the container to also clear the reference in each of its children, and simply not do anything when "child.remove()" is called if it has already been removed.

soulBit avatar Nov 05 '12 15:11 soulBit

@soulBit I believe I understand what you're saying, but here's the question I guess - are you saying that when a parent or container is removed, the children should still be accessible? Granted, when you create a group or layer and add children to that layer, each child is a variable (object), and that variable exists in whatever scope it was originally set, but technically if it is added to a container then the container is removed, the child should NOT have a reference within the stage. The variable may still exist, but only as a collection of values, not as an actual reference to an object / container / shape on the stage. Think of it in simpler terms - if this were jQuery and you created a div, then added an image to the div, then removed the div - all using jQuery - if you then tried to reference the image (NOT the variable used to create the image but the "physical" image in the DOM), would you get a similar response?

lazerblade01 avatar Nov 05 '12 16:11 lazerblade01

It's true that when a container is removed the children shouldn't be accessible from the stage, but I think what I was hoping for was that this change would also be reflected in the "Node.parent" property (by setting to null or otherwise). I've already coded around this like you mentioned above, so it isn't too much issue - it just seemed a bit inconsistent that's all.

soulBit avatar Nov 05 '12 19:11 soulBit

have to agree wtih @lazerblade01 on this one - generally, in JavaScript, if you want to delete an object (such as a KineticJS Shape), it's not necessary to null out, or delete all of the object's properties. Instead, you just need to remove all references to it, and the JavaScript garbage collector will do the rest.

ericdrowell avatar Nov 11 '12 16:11 ericdrowell

Actually, here's another issue related to this one that makes a great point:

https://github.com/ericdrowell/KineticJS/issues/29

I'll spend a little time re-investigating this one. Thanks!

ericdrowell avatar Nov 11 '12 16:11 ericdrowell