Class HasManyReference
Extends: | Reference |
---|---|
Defined in: | ../store/addon/-private/system/references/has-many.js:15 |
Module: | @ember-data/store |
ids Array
Module: | @ember-data/store |
---|
Defined in ../store/addon/-private/system/references/has-many.js:88
- returns
- Array
- The ids in this has-many relationship
ids()
returns an array of the record IDs in this relationship.
Example
app/models/post.js
import Model, { hasMany } from '@ember-data/model'; export default class PostModel extends Model { @hasMany({ async: true }) comments; }
let post = store.push({ data: { type: 'post', id: 1, relationships: { comments: { data: [{ type: 'comment', id: 1 }] } } } }); let commentsRef = post.hasMany('comments'); commentsRef.ids(); // ['1']
link String
Module: | @ember-data/store |
---|
Inherited from Reference ../store/addon/-private/system/references/reference.ts:109
- 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 import Model, { belongsTo } from '@ember-data/model'; export default Model.extend({ user: 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 (options) Promise
Module: | @ember-data/store |
---|
Defined in ../store/addon/-private/system/references/has-many.js:269
- options
- Object
- the options to pass in.
- returns
- Promise
- a promise that resolves with the ManyArray in this has-many relationship.
Loads the relationship if it is not already loaded. If the relationship is already loaded this method does not trigger a new load. This causes a request to the specified relationship link or reloads all items currently in the relationship.
Example
app/models/post.js
import Model, { hasMany } from '@ember-data/model'; export default class PostModel extends Model { @hasMany({ async: true }) comments; }
let post = store.push({ data: { type: 'post', id: 1, relationships: { comments: { data: [{ type: 'comment', id: 1 }] } } } }); let commentsRef = post.hasMany('comments'); commentsRef.load().then(function(comments) { //... });
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
commentsRef.load({ adapterOptions: { isPrivate: true } }) .then(function(comments) { //... });
app/adapters/comment.js
export default ApplicationAdapter.extend({ findMany(store, type, id, snapshots) { // In the adapter you will have access to adapterOptions. let adapterOptions = snapshots[0].adapterOptions; } });
meta Object
Module: | @ember-data/store |
---|
Inherited from Reference ../store/addon/-private/system/references/reference.ts:165
- returns
- Object
- The meta information for the belongs-to relationship.
The meta data for the belongs-to relationship.
Example
// models/blog.js import Model, { belongsTo } from '@ember-data/model'; export default Model.extend({ user: 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) ManyArray
Module: | @ember-data/store |
---|
Defined in ../store/addon/-private/system/references/has-many.js:133
- objectOrPromise
- Array|Promise
- a promise that resolves to a JSONAPI document object describing the new value of this relationship.
- returns
- ManyArray
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/post.js
import Model, { hasMany } from '@ember-data/model'; export default class PostModel extends Model { @hasMany({ async: true }) comments; }
let post = store.push({ data: { type: 'post', id: 1, relationships: { comments: { data: [{ type: 'comment', id: 1 }] } } } }); let commentsRef = post.hasMany('comments'); commentsRef.ids(); // ['1'] commentsRef.push([ [{ type: 'comment', id: 2 }], [{ type: 'comment', id: 3 }], ]) commentsRef.ids(); // ['2', '3']
reload (options) Promise
Module: | @ember-data/store |
---|
Defined in ../store/addon/-private/system/references/has-many.js:337
- options
- Object
- the options to pass in.
- returns
- Promise
- a promise that resolves with the ManyArray in this has-many relationship.
Reloads this has-many relationship. This causes a request to the specified relationship link or reloads all items currently in the relationship.
Example
app/models/post.js
import Model, { hasMany } from '@ember-data/model'; export default class PostModel extends Model { @hasMany({ async: true }) comments; }
let post = store.push({ data: { type: 'post', id: 1, relationships: { comments: { data: [{ type: 'comment', id: 1 }] } } } }); let commentsRef = post.hasMany('comments'); commentsRef.reload().then(function(comments) { //... });
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
commentsRef.reload({ adapterOptions: { isPrivate: true } })
remoteType String
Module: | @ember-data/store |
---|
Defined in ../store/addon/-private/system/references/has-many.js:38
- returns
- String
- The name of the remote type. This should either be `link` or `ids`
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
import Model, { hasMany } from '@ember-data/model'; export default class PostModel extends Model { @hasMany({ async: true }) comments; }
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(); }
value ManyArray
Module: | @ember-data/store |
---|
Defined in ../store/addon/-private/system/references/has-many.js:220
- returns
- ManyArray
value()
synchronously returns the current value of the has-many 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
app/models/post.js
import Model, { hasMany } from '@ember-data/model'; export default class PostModel extends Model { @hasMany({ async: true }) comments; }
let post = store.push({ data: { type: 'post', id: 1, relationships: { comments: { data: [{ type: 'comment', id: 1 }] } } } }); let commentsRef = post.hasMany('comments'); post.get('comments').then(function(comments) { commentsRef.value() === comments })
© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember-data/3.25/classes/HasManyReference/methods