#

API Docs for: v3.8.1
Show: Inherited Protected Private Deprecated

# DS.Snapshot Class

Defined in: `addon/-private/system/snapshot.js:10`
Module: ember-data

## Constructor

### `DS.Snapshot`

(

- `internalModel`

)
private

Defined in `addon/-private/system/snapshot.js:10`

#### Parameters:

- `internalModel` DS.Model

  The model to create a snapshot from

- Index
- Methods
- Properties

## Item Index

### Methods

- attr
- attributes
- belongsTo
- changedAttributes
- eachAttribute
- eachRelationship
- hasMany
- serialize

### Properties

- adapterOptions
- id
- modelName
- record
- type

## Methods

### `attr`

(

- `keyName`

)
Object

Defined in `addon/-private/system/snapshot.js:114`

Returns the value of an attribute.

Example

    // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
    postSnapshot.attr('author'); // => 'Tomster'
    postSnapshot.attr('title'); // => 'Ember.js rocks'

Note: Values are loaded eagerly and cached when the snapshot is created.

#### Parameters:

- `keyName` String

#### Returns:

Object:

The attribute value or undefined

### `attributes`

() Object

Defined in `addon/-private/system/snapshot.js:140`

Returns all attributes and their corresponding values.

Example

    // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
    postSnapshot.attributes(); // => { author: 'Tomster', title: 'Ember.js rocks' }

#### Returns:

Object:

All attributes of the current snapshot

### `belongsTo`

(

- `keyName`
- `[options]`

)
(DS.Snapshot | String | Null | Undefined)

Defined in `addon/-private/system/snapshot.js:183`

Returns the current value of a belongsTo relationship.

`belongsTo` takes an optional hash of options as a second parameter, currently supported options are:

- `id`: set to `true` if you only want the ID of the related record to be returned.

Example

    // store.push('post', { id: 1, title: 'Hello World' });
    // store.createRecord('comment', { body: 'Lorem ipsum', post: post });
    commentSnapshot.belongsTo('post'); // => DS.Snapshot
    commentSnapshot.belongsTo('post', { id: true }); // => '1'

    // store.push('comment', { id: 1, body: 'Lorem ipsum' });
    commentSnapshot.belongsTo('post'); // => undefined

Calling `belongsTo` will return a new Snapshot as long as there's any known data for the relationship available, such as an ID. If the relationship is known but unset, `belongsTo` will return `null`. If the contents of the relationship is unknown `belongsTo` will return `undefined`.

Note: Relationships are loaded lazily and cached upon first access.

#### Parameters:

- `keyName` String
- `[options]` Object optional

#### Returns:

(DS.Snapshot | String | Null | Undefined):

A snapshot or ID of a known relationship or null if the relationship is known but unset. undefined will be returned if the contents of the relationship is unknown.

### `changedAttributes`

() Object

Defined in `addon/-private/system/snapshot.js:157`

Returns all changed attributes and their old and new values.

Example

    // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
    postModel.set('title', 'Ember.js rocks!');
    postSnapshot.changedAttributes(); // => { title: ['Ember.js rocks', 'Ember.js rocks!'] }

#### Returns:

Object:

All changed attributes of the current snapshot

### `eachAttribute`

(

- `callback`
- `[binding]`

)

Defined in `addon/-private/system/snapshot.js:353`

Iterates through all the attributes of the model, calling the passed function on each attribute.

Example

    snapshot.eachAttribute(function(name, meta) {
      // ...
    });

#### Parameters:

- `callback` Function

  the callback to execute

- `[binding]` Object optional

  the value to which the callback's `this` should be bound

### `eachRelationship`

(

- `callback`
- `[binding]`

)

Defined in `addon/-private/system/snapshot.js:373`

Iterates through all the relationships of the model, calling the passed function on each relationship.

Example

    snapshot.eachRelationship(function(name, relationship) {
      // ...
    });

#### Parameters:

- `callback` Function

  the callback to execute

- `[binding]` Object optional

  the value to which the callback's `this` should be bound

### `hasMany`

(

- `keyName`
- `[options]`

)
(Array | Undefined)

Defined in `addon/-private/system/snapshot.js:272`

Returns the current value of a hasMany relationship.

`hasMany` takes an optional hash of options as a second parameter, currently supported options are:

- `ids`: set to `true` if you only want the IDs of the related records to be returned.

Example

    // store.push('post', { id: 1, title: 'Hello World', comments: [2, 3] });
    postSnapshot.hasMany('comments'); // => [DS.Snapshot, DS.Snapshot]
    postSnapshot.hasMany('comments', { ids: true }); // => ['2', '3']

    // store.push('post', { id: 1, title: 'Hello World' });
    postSnapshot.hasMany('comments'); // => undefined

Note: Relationships are loaded lazily and cached upon first access.

#### Parameters:

- `keyName` String
- `[options]` Object optional

#### Returns:

(Array | Undefined):

An array of snapshots or IDs of a known relationship or an empty array if the relationship is known but unset. undefined will be returned if the contents of the relationship is unknown.

### `serialize`

(

- `options`

)
Object

Defined in `addon/-private/system/snapshot.js:393`

Serializes the snapshot using the serializer for the model.

Example

    import DS from 'ember-data';

    export default DS.Adapter.extend({
      createRecord(store, type, snapshot) {
        var data = snapshot.serialize({ includeId: true });
        var url = /${type.modelName};

        return fetch(url, {
          method: 'POST',
          body: data,
        }).then((response) => response.json())
      }
    });

#### Parameters:

- `options` Object

#### Returns:

Object:

an object whose values are primitive JSON values only

## Properties

### `adapterOptions`

Object

Defined in `addon/-private/system/snapshot.js:52`

A hash of adapter options

### `id`

String

Defined in `addon/-private/system/snapshot.js:37`

The id of the snapshot's underlying record

Example

    // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
    postSnapshot.id; // => '1'

### `modelName`

String

Defined in `addon/-private/system/snapshot.js:60`

The name of the type of the underlying record for this snapshot, as a string.

### `record`

DS.Model

Defined in `addon/-private/system/snapshot.js:71`

The underlying record for this snapshot. Can be used to access methods and properties defined on the record.

Example

    let json = snapshot.record.toJSON();

### `type`

DS.Model

Defined in `addon/-private/system/snapshot.js:101`

The type of the underlying record for this snapshot, as a DS.Model.