DS.Store#push()

push (data) DS.Model|Array

Defined in addon/-private/system/store.js:1962

Push some data for a given type into the store.

This method expects normalized JSON API document. This means you have to follow JSON API specification with few minor adjustments: - record's type should always be in singular, dasherized form - members (properties) should be camelCased

Your primary data should be wrapped inside data property:

store.push({
  data: {
    // primary data for single record of type `Person`
    id: '1',
    type: 'person',
    attributes: {
      firstName: 'Daniel',
      lastName: 'Kmak'
    }
  }
});

Demo.

data property can also hold an array (of records):

store.push({
  data: [
    // an array of records
    {
      id: '1',
      type: 'person',
      attributes: {
        firstName: 'Daniel',
        lastName: 'Kmak'
      }
    },
    {
      id: '2',
      type: 'person',
      attributes: {
        firstName: 'Tom',
        lastName: 'Dale'
      }
    }
  ]
});

Demo.

There are some typical properties for JSONAPI payload: * id - mandatory, unique record's key * type - mandatory string which matches model's dasherized name in singular form * attributes - object which holds data for record attributes - DS.attr's declared in model * relationships - object which must contain any of the following properties under each relationships' respective key (example path is relationships.achievements.data): - links - data - place for primary data - meta - object which contains meta-information about relationship

For this model:

app/models/person.js
import DS from 'ember-data';

export default DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),

  children: DS.hasMany('person')
});

To represent the children as IDs:

{
  data: {
    id: '1',
    type: 'person',
    attributes: {
      firstName: 'Tom',
      lastName: 'Dale'
    },
    relationships: {
      children: {
        data: [
          {
            id: '2',
            type: 'person'
          },
          {
            id: '3',
            type: 'person'
          },
          {
            id: '4',
            type: 'person'
          }
        ]
      }
    }
  }
}

Demo.

To represent the children relationship as a URL:

{
  data: {
    id: '1',
    type: 'person',
    attributes: {
      firstName: 'Tom',
      lastName: 'Dale'
    },
    relationships: {
      children: {
        links: {
          related: '/people/1/children'
        }
      }
    }
  }
}

If you're streaming data or implementing an adapter, make sure that you have converted the incoming data into this form. The store's normalize method is a convenience helper for converting a json payload into the form Ember Data expects.

store.push(store.normalize('person', data));

This method can be used both to push in brand new records, as well as to update existing records.

Parameters:

data Object

Returns:

DS.Model|Array
the record(s) that was created or updated.
doc_EmberJs
2016-11-30 16:50:53
Comments
Leave a Comment

Please login to continue.