Class BelongsToReference

public

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

Show:

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.relationship.id.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// models/blog.js
import Model, { belongsTo } from '@ember-data/model';

export default class BlogModel extends Model {
  @belongsto('user', { async: true, inverse: null }) user;
}

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();
 }
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. By default it uses only the "related" resource linkage.

Example

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
// models/blog.js
import Model, { belongsTo } from '@ember-data/model';
export default Model.extend({
   user: belongsTo('user', { async: true, inverse: null })
 });

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();
 }
returns

any links that have been received for this relationship

options
Object
the options to pass in.
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

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
// models/blog.js
import Model, { belongsTo } from '@ember-data/model';

export default class BlogModel extends Model {
  @belongsto('user', { async: true, inverse: null }) user;
}

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
 });

You may also pass in an options object whose properties will be fed forward. This enables you to pass adapterOptions into the request given to the adapter via the reference.

Example

1
2
3
userRef.load({ adapterOptions: { isPrivate: true } }).then(function(user) {
  userRef.value() === user;
});
app/adapters/user.js
1
2
3
4
5
6
7
8
import Adapter from '@ember-data/adapter';

export default class UserAdapter extends Adapter {
  findRecord(store, type, id, snapshot) {
    // In the adapter you will have access to adapterOptions.
    let adapterOptions = snapshot.adapterOptions;
  }
});
returns
Object
The meta information for the belongs-to relationship.

The meta data for the belongs-to relationship.

Example

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
// models/blog.js
import Model, { belongsTo } from '@ember-data/model';
export default Model.extend({
   user: belongsTo('user', { async: true, inverse: null })
 });

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 }
object
Object
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 canonical value of this relationship on the backend.

Example

app/models/blog.js
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
29
30
31
import Model, { belongsTo } from '@ember-data/model';

export default class BlogModel extends Model {
  @belongsto('user', { async: true, inverse: null }) user;
 }

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;
 });
options
Object
the options to pass in.
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// models/blog.js
import Model, { belongsTo } from '@ember-data/model';

export default class BlogModel extends Model {
  @belongsto('user', { async: true, inverse: null }) user;
}

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
 });

You may also pass in an options object whose properties will be fed forward. This enables you to pass adapterOptions into the request given to the adapter via the reference. A full example can be found in the load method.

Example

1
userRef.reload({ adapterOptions: { isPrivate: true } })
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

app/models/post.js
1
2
3
4
5
import Model, { hasMany } from '@ember-data/model';

export default class PostModel extends Model {
  @hasmany('comment', { async: true, inverse: null }) comments;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let post = store.push({
  data: {
    type: 'post',
    id: 1,
    relationships: {
      comments: {
        data: [{ type: 'comment', id: 1 }]
      }
    }
  }
});

let commentsRef = post.hasMany('comments');

// get the identifier of the reference
if (commentsRef.remoteType() === "ids") {
  let ids = commentsRef.ids();
} else if (commentsRef.remoteType() === "link") {
  let link = commentsRef.link();
}
returns
Model
the record in this relationship

value() synchronously returns the current value of the belongs-to relationship. Unlike record.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

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
29
30
31
32
33
34
// models/blog.js
import Model, { belongsTo } from '@ember-data/model';

export default class BlogModel extends Model {
  @belongsto('user', { async: true, inverse: null }) user;
}

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
 });