DS.Snapshot#belongsTo()

belongsTo (keyName, options) (DS.Snapshot|String|null|undefined)

Defined in addon/-private/system/snapshot.js:161

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]

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.
doc_EmberJs
2016-11-30 16:50:41
Comments
Leave a Comment

Please login to continue.