Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing a Child Object Within a Parent Object in JavaScript
    primarykey
    data
    text
    <p>This question is a follow-up to my last question: <a href="https://stackoverflow.com/questions/9668345/javascript-serialization-and-methods">JavaScript Serialization and Methods</a>. While related, I believe this is different which is why I started this thread. Regardless...</p> <p>I have a complex tree of objects that I need to pass around between pages. Because of this, I'm attempting to serialize and deserialize my objects. One of my objects in particular has several collections of child objects. Each of those child object types has a function on it, that I'm trying to call. Unfortunately, I am not having any luck. I setup a test project in an attempt to isolate the problem and get to the bottom of it. Currently, I have my JavaScript Objects defined in a seperate file called objects.js. That file looks like this:</p> <p><strong>objects.js</strong></p> <pre><code>function MyChild() { this.init(); } MyChild.prototype = { data: { id: 0, fullName: "", }, init: function () { }, save: function (key) { }, load: function (key) { }, test: function () { alert("Testing Child functions"); } } function MyParent() { this.init(); } MyParent.prototype = { data: { id: "", children: null }, init: function () { this.data.children = []; }, save: function (key) { window.localStorage.setItem(key, JSON.stringify(this.data)); }, load: function (key) { var temp = window.localStorage.getItem(key); if (temp != null) { this.data = JSON.parse(temp); $.each(this.data.children, function (i, r) { }); } }, test: function () { alert("Testing Parent functions"); } } </code></pre> <p>I am creating, serializing, deserializing, and attempting to interact with these objects in an .html file. That .html file is shown here:</p> <p><strong>test.html</strong></p> <pre><code>&lt;div&gt; &lt;input id="button1" type="button" value="Create Object Tree" onclick="button1Click();" /&gt;&lt;br /&gt; &lt;input id="button2" type="button" value="Retrieve and Execute" onclick="button2Click();" /&gt;&lt;br /&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; function button1Click() { var child = new MyChild(); child.data.id = 1; var p = new MyParent(); p.data.id = "A"; p.data.children.push(child); p.save("A"); } function button2Click() { var storedParent = new MyParent(); storedParent.load("A"); storedParent.test(); // This works alert(storedParent.data.children.length); // This displays "1" like I would expect alert(storedParent.data.children[0].data.id); // This does NOT work. storedParent.data.children[0].test(); // This does NOT work. } &lt;/script&gt; </code></pre> <p>I'm not sure what I'm doing wrong. Can someone please help me understand this? Can somone please help me fix my example. I have a hunch that I'm not serializing MyChild objects properly. But I don't understand how I should be serializing / deserializing them in relation to MyParent.</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload