AliasIO / Raphael.JSON

Convert Raphaël paper objects to JSON and back

Home Page:http://elbertf.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

paper.toJSON() does not work for id attributes

ryanm opened this issue · comments

Draw any element and assign it an id:

var c = paper.circle(10,10,5);
c.node.id = "mycircle";

Run:

var json = paper.toJSON();
paper.clear();
paper.fromJSON(json);

The json object will be missing the id="mycircle" on the circle node.

toJSON omits quite a bit of data, it basically saves just enough to be able to redraw the shapes later. You can add id: node.id under the line that contains nodes.push. Since it's only a small change I'll add this in when I get the chance (or you can issue a pull request).

This has been fixed.

Thank you.

I think your fix saves Raphael's internal id for the node, as a style attribute:

<circle style="id='5'"></circle>

But I am trying to save this id:

<circle id="mycircle"></circle>

Which in Raphael is set like:

var c = paper.circle(10,10,5);
c.node.id = "mycircle";

I said it was saving to the style attribute: my mistake, it's saving to the id attribute. Trouble is, it's saving Raphael's internal id for the element instead of the value for the element's id attribute. Thus later when I restore using fromJSON() I see:

<circle id="5"></circle>

Instead of:

<circle id="mycircle"></circle>

(This assumes we add .id(json[i].id) below line 26 in fromJSON().)

Ah yes, keeping the actual HTML id attribute makes sense. I'll look into that.

Thanks!

Should be working now.