Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an example of creating a custom array type in Ember-Data (version 10):</p> <pre><code>DS.JSONTransforms.array = # If the outgoing json is already a valid javascript array # then pass it through untouched. In all other cases, replace it # with an empty array. This means null or undefined values # automatically become empty arrays when serializing this type. serialize: (jsonData)-&gt; if Em.typeOf(jsonData) is 'array' then jsonData else [] # If the incoming data is a javascript array, pass it through. # If it is a string, then coerce it into an array by splitting # it on commas and trimming whitespace on each element. # Otherwise pass back an empty array. This has the effect of # turning all other data types (including nulls and undefined # values) into empty arrays. deserialize: (externalData)-&gt; switch Em.typeOf(externalData) when 'array' then return externalData when 'string' then return externalData.split(',').map((item)-&gt; jQuery.trim(item)) else return [] </code></pre> <p>Now you can use the custom type in a model attribute:</p> <pre><code>App.CalenderWeek = DS.Model.extend selected_days = DS.attr('array') </code></pre> <p>And now when you fetch a record with:</p> <pre><code>App.CalendarWeek.find(1) </code></pre> <p>both of these incoming json records will deserialize correctly into an Array:</p> <pre><code>{ selected_days: ['Monday', 'Tuesday', 'Saturday'] } </code></pre> <p>or</p> <pre><code>{ selected_days: 'Monday, Tuesday, Saturday' } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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