Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring object references in JSON
    text
    copied!<p>I'm currently building a pretty complex AngularJS application that stores data in a sqlite database. My application data contains references to objects that are shared throughout the app. Angular is able to render this data, compare it, and everything has been fine so far.</p> <p>What do I mean by all of this? Check out this example for the "boilerplate" of the objects in my application. </p> <pre><code>var Person = { name:'some person', owns:[] } var Cat = { name:'some cat' } var project = { name:'some project', people:[], cats:[] } </code></pre> <p>My application will allow users to create and manipulate these objects, and create relationships between them. Instances of <code>Person</code> may be created, and each <code>Person</code> object may store references to instances of <code>Cat</code>. </p> <p>As you may have already guessed, my <code>project</code> will eventually look like this after the user is done manipulating it.</p> <pre><code>var project = { name:'some project', people:[person, person, person], cats:[cat, cat cat, cat, cat, cat] } console.log(project.people[0].owns) //logs something like "cat, cat, cat". These are references. </code></pre> <p>My application then has views set up to view each <code>person</code> and will list out the <code>owns</code> property, which contains the instances of <code>Cat</code>.</p> <p>All is fine in dandy until I realized that there may be complications storing this as JSON in a database. <code>JSON.Stringify()</code> and <code>angular.toJSON()</code> do not treat references as references, instead they read them as individual objects. </p> <p>I am hoping to gain some insights on the best way to preserve these relationships/references. </p> <p>Here are the two options that I believe I have.</p> <p><strong>Option 1:</strong> Scrap the idea of storing this in JSON, and use a relational database to store everything. This isn't ideal because it starts to eat away at the flexible nature of object rendering in AngularJS. Additionally, this data will be stored in multiple places (online &amp; locally) which may present differences in the database scheme which would be a debugging nightmare.</p> <p><strong>Option 2:</strong> Store a unique identifier with each instance of <code>Person</code> and <code>Cat</code>. I can then use this identifier when rendering and creating the associations. This will work if I create custom filters in Angular, but globally removing references when objects are deleted could be a nightmare.</p>
 

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