Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To persist a Many to Many relationship on the server, I'm hacking the <code>hasMany</code> method of the serializer to add an option <code>serialized</code> in the mapping.</p> <p>Here is the serializer (CoffeeScript) :</p> <pre><code>App.Serializer = DS.RESTSerializer.extend # redefine to make it possible to serialize a relationship by an array of ids # to enable it, a 'serialized' property should be set to 'ids' in the adapter mapping # the key in the hash will be the type of the object in the relation ship + _ids' # example : for an hasMany('App.Category'), the key will be category_ids addHasMany: (hash, record, key, relationship) -&gt; type = record.constructor name = relationship.key serializedType = @serializedType type, name if serializedType is 'ids' key = @singularize(@keyForAttributeName(type, name)) + "_ids" manyArray = record.get name serializedIds = [] manyArray.forEach (record) -&gt; serializedIds.push record.get('id') hash[key] = serializedIds else return @_super hash, record, key, relationship # method to access the serialized option in adapter mapping serializedType: (type, name) -&gt; @mappingOption type, name, 'serialized' </code></pre> <p>And here is a mapping configuration exemple :</p> <pre><code>App.Adapter.map App.Product, categories: serialized: 'ids' </code></pre> <p>With this in place, when I commit a <code>Product</code> the json would contain a key <code>category_ids</code> wich contain the array of associated categories ids.</p> <p>UPDATE:</p> <p>To properly use the <code>App.Serializer</code> defined, you should add it to the adapter of your application :</p> <pre><code>App.Adapter = DS.RESTAdapter.extend serializer: App.Serializer </code></pre> <p>And to use this adapter in your application, you have to set it in the store :</p> <pre><code>App.Store = DS.Store.extend adapter: 'App.Adapter' </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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