0

I've an Object which contains circular references in it (references to DOM nodes). I've to save this object in local or session storage. To do this I was trying to do JSON.stringify and because of circular references I'm unable to stringify it.

This is how I'm getting the object that I've to save.

var elements = $("#content").children().detach();

After doing some research online most of the people suggested using cycle.js.

So I've tried using the cycle.js on elements variable, which will decycle the circular reference object. I've also tried circular-json.js, stringify-object.js

All these are throwing an Maximum call stack size exceeded error. I don't understand why I'm getting this error and how I could fix it.

Is there a way to get around this and save the object?

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • DomNodes are complex objects, with many methods and properties that will not be JSON valid. Perhaps you would like to get the DOM Html? This would return a string that you could immediately save. Otherwise I would advise that you determine what "Object" you are actually trying to store (what information from the dom element are you looking to reuse later) and pull that off into a simple object for the storage. – Gabriel Jun 18 '14 at 18:59
  • Why are you converting a dom html collection of nodes to JSON??? Makes no sense. – epascarello Jun 18 '14 at 19:00
  • I've a datatable inside a ajax loaded div called content. I've to restore the datatable as it is, after switching one page to another. If I'm doing .html() to save the dom and put it back as string the javascript events on the datatable(click) are not working. Also it is making duplicate search boxes. Detach and append are working fine, but I'm not able to save them. – user3605695 Jun 18 '14 at 19:06
  • Well JSON is not the solution in the first place,,,that would make a string and would not copy events. Local Storage is also a string so that is not going to hold info either. You are going to have to rethink your design. – epascarello Jun 18 '14 at 19:08
  • I didn't know that. Thanks for the help. Is there any other way to restore the dom which has events attached with it? – user3605695 Jun 18 '14 at 19:14
  • To save accross full page loads? no...once the document is destroyed it is bye bye. – epascarello Jun 18 '14 at 19:21

0 Answers0