Note that there are some explanatory texts on larger screens.

plurals
  1. POAlternative To Associations with EmberJS/Data
    primarykey
    data
    text
    <p>I've been working on an EmberJS application for about a week now, and have written my own localStorage adapter for EmberJS/Data to allow for data persistence. The app is a small note-taking application. Ideally, I'd like to have a Notebook model that has many Note models associated with it.</p> <p>However, I haven't found a way to do this that results in a mutable array. While you can "implement" a hasMany relation...</p> <pre class="lang-js prettyprint-override"><code>Noted.Note = DS.Model.extend({ title: DS.attr('string'), note_text: DS.attr('string'), }); Noted.Notebook = DS.Model.extend({ title: DS.attr("string"), notes: DS.hasMany(Noted.Note) }); </code></pre> <p>...the ModelArray created by the hasMany method is immutable in EmberJS. It appears that this is currently being worked on - there is an "associations" branch of EmberJS/Data <a href="https://github.com/emberjs/data/tree/associations" rel="nofollow">here</a> - but modifying the ModelArray is critical to this application, to i.e. create a new Note:</p> <pre class="lang-js prettyprint-override"><code>var notebook = Noted.store.createResource(Noted.Notebook, { title: "Foo Notebook" }); notebook.get("notes").pushObject(Noted.store.createResource(Noted.Note, { title: "Bar Note", note_text: "Blah blah blah." })); notebook.get("notes").objectAt(0).get("title") //returns undefined </code></pre> <p>Although the very latest commit of EmberJS/Data does include array mutability, it still lacks any sort of observation - the adapter isn't notified when changes are made, and they are not saved.</p> <p>I'm wondering: what's the best alternative to a hasMany association until this ability is added to EmberJS/Data? I've been looking into different patterns, such as having a note IDs array in the Notebook model with some sort of fetch/retrieve interface to go with it, but I've gotten absolutely nowhere. I've spent quite a good chunk of today trying to figure out the best way to do this, and all I have is a lot of frustration. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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