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:

1
2
3
4
5
6
7
8
9
10
11
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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
1
2
3
4
5
6
7
8
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
  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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
  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.

1
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
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.