PRIVATE
Defined in: addon/-private/system/snapshot.js:10
Module: ember-data
Object
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.
String
Object Object
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' }
Object (DS.Snapshot|String|null|undefined)
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.
String
[Object]
(DS.Snapshot|String|null|undefined) Object
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!'] }
Object Iterates through all the attributes of the model, calling the passed function on each attribute.
Example
snapshot.eachAttribute(function(name, meta) {
// ...
});
Function
[Object]
Iterates through all the relationships of the model, calling the passed function on each relationship.
Example
snapshot.eachRelationship(function(name, relationship) {
// ...
});
Function
[Object]
(Array|undefined)
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.
String
[Object]
(Array|undefined) Object
Serializes the snapshot using the serializer for the model.
Example
app/adapters/application.jsimport 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())
}
});
Object
Object {Object}
A hash of adapter options
{String}
The id of the snapshot's underlying record
Example
// store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
postSnapshot.id; // => '1'
{String}
The name of the type of the underlying record for this snapshot, as a string.
{DS.Model}
The underlying record for this snapshot. Can be used to access methods and properties defined on the record.
Example
var json = snapshot.record.toJSON();
{DS.Model}
The type of the underlying record for this snapshot, as a DS.Model.
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
http://emberjs.com/api/data/classes/DS.Snapshot.html