home
  • Blog
  • Home
  • Projects
    • Ember
    • EmberData
    • Ember CLI
2.18
  • Packages
    • ember-data
  • Classes
    • DS.AbortError
    • DS.Adapter
    • DS.AdapterError
    • DS.AdapterPopulatedRecordArray
    • DS.BelongsToReference
    • DS.BooleanTransform
    • DS.BuildURLMixin
    • DS.ConflictError
    • DS.DateTransform
    • DS.EmbeddedRecordsMixin
    • DS.Errors
    • DS.FilteredRecordArray
    • DS.ForbiddenError
    • DS.HasManyReference
    • DS.InvalidError
    • DS.JSONAPIAdapter
    • DS.JSONAPISerializer
    • DS.JSONSerializer
    • DS.ManyArray
    • DS.Model
    • DS.NotFoundError
    • DS.NumberTransform
    • DS.PromiseArray
    • DS.PromiseManyArray
    • DS.PromiseObject
    • DS.RESTAdapter
    • DS.RESTSerializer
    • DS.RecordArray
    • DS.RecordReference
    • DS.RootState
    • DS.Serializer
    • DS.ServerError
    • DS.Store
    • DS.StringTransform
    • DS.TimeoutError
    • DS.Transform
    • DS.UnauthorizedError
    • Ember.Inflector

Class DS.BelongsToReference


Extends: DS.Reference
Defined in: addon/-private/system/references/belongs-to.js:9
Module: ember-data

A BelongsToReference is a low level API that allows users and addon author to perform meta-operations on a belongs-to relationship.


Methods

id : String

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:74

returns
String

The id of the record in this belongsTo relationship.

The id of the record that this reference refers to. Together, the type() and id() methods form a composite key for the identity map. This can be used to access the id of an async relationship without triggering a fetch that would normally happen if you attempted to use record.get('relationship.id').

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   data: {
     type: 'blog',
     id: 1,
     relationships: {
       user: {
         data: { type: 'user', id: 1 }
       }
     }
   }
 });
 let userRef = blog.belongsTo('user');

 // get the identifier of the reference
 if (userRef.remoteType() === "id") {
   let id = userRef.id();
 }

link : String

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:116

returns
String

The link Ember Data will use to fetch or reload this belongs-to relationship.

The link Ember Data will use to fetch or reload this belongs-to relationship.

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   data: {
     type: 'blog',
     id: 1,
     relationships: {
       user: {
         links: {
           related: '/articles/1/author'
         }
       }
     }
   }
 });
 let userRef = blog.belongsTo('user');

 // get the identifier of the reference
 if (userRef.remoteType() === "link") {
   let link = userRef.link();
 }

load : Promise

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:323

returns
Promise

a promise that resolves with the record in this belongs-to relationship.

Loads a record in a belongs to relationship if it is not already loaded. If the relationship is already loaded this method does not trigger a new load.

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   data: {
     type: 'blog',
     id: 1,
     relationships: {
       user: {
         data: { type: 'user', id: 1 }
       }
     }
   }
 });
 let userRef = blog.belongsTo('user');

 userRef.value(); // null

 userRef.load().then(function(user) {
   userRef.value() === user
 });

meta : Object

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:156

returns
Object

The meta information for the belongs-to relationship.

The meta data for the belongs-to relationship.

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   data: {
     type: 'blog',
     id: 1,
     relationships: {
       user: {
         links: {
           related: {
             href: '/articles/1/author',
             meta: {
               lastUpdated: 1458014400000
             }
           }
         }
       }
     }
   }
 });

 let userRef = blog.belongsTo('user');

 userRef.meta() // { lastUpdated: 1458014400000 }

push (objectOrPromise) : Promise<record>

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:198

objectOrPromise
Object|Promise

a promise that resolves to a JSONAPI document object describing the new value of this relationship.

returns
Promise<record>

A promise that resolves with the new value in this belongs-to relationship.

push can be used to update the data in the relationship and Ember Data will treat the new data as the conanical value of this relationship on the backend.

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   data: {
     type: 'blog',
     id: 1,
     relationships: {
       user: {
         data: { type: 'user', id: 1 }
       }
     }
   }
 });
 let userRef = blog.belongsTo('user');

 // provide data for reference
 userRef.push({
   data: {
     type: 'user',
     id: 1,
     attributes: {
       username: "@user"
     }
   }
 }).then(function(user) {
   userRef.value() === user;
 });

reload : Promise

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:371

returns
Promise

a promise that resolves with the record in this belongs-to relationship after the reload has completed.

Triggers a reload of the value in this relationship. If the remoteType is "link" Ember Data will use the relationship link to reload the relationship. Otherwise it will reload the record by its id.

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   data: {
     type: 'blog',
     id: 1,
     relationships: {
       user: {
         data: { type: 'user', id: 1 }
       }
     }
   }
 });
 let userRef = blog.belongsTo('user');

 userRef.reload().then(function(user) {
   userRef.value() === user
 });

remoteType : String

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:31

returns
String

The name of the remote type. This should either be "link" or "id"

This returns a string that represents how the reference will be looked up when it is loaded. If the relationship has a link it will use the "link" otherwise it defaults to "id".

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   type: 'blog',
   id: 1,
   relationships: {
     user: {
       data: { type: 'user', id: 1 }
     }
   }
 });
 let userRef = blog.belongsTo('user');

 // get the identifier of the reference
 if (userRef.remoteType() === "id") {
   let id = userRef.id();
 } else if (userRef.remoteType() === "link") {
   let link = userRef.link();
 }

value : DS.Model

Module: ember-data

Defined in addon/-private/system/references/belongs-to.js:266

returns
DS.Model

the record in this relationship

value() synchronously returns the current value of the belongs-to relationship. Unlike record.get('relationshipName'), calling value() on a reference does not trigger a fetch if the async relationship is not yet loaded. If the relationship is not loaded it will always return null.

Example

 // models/blog.js
 export default DS.Model.extend({
   user: DS.belongsTo({ async: true })
 });

 let blog = store.push({
   data: {
     type: 'blog',
     id: 1,
     relationships: {
       user: {
         data: { type: 'user', id: 1 }
       }
     }
   }
 });
 let userRef = blog.belongsTo('user');

 userRef.value(); // null

 // provide data for reference
 userRef.push({
   data: {
     type: 'user',
     id: 1,
     attributes: {
       username: "@user"
     }
   }
 }).then(function(user) {
   userRef.value(); // user
 });
On this page


Methods

  • id
  • link
  • load
  • meta
  • push
  • reload
  • remoteType
  • value
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2025 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue Created with Sketch.