Class HasManyReference
publicA HasManyReference
is a low-level API that allows access
and manipulation of a hasMany relationship.
It is especially useful when you're dealing with async
relationships
from @ember-data/model
as it allows synchronous access to
the relationship data if loaded, as well as APIs for loading, reloading
the data or accessing available information without triggering a load.
It may also be useful when using sync
relationships with @ember-data/model
that need to be loaded/reloaded with more precise timing than marking the
relationship as async
and relying on autofetch would have allowed.
However,keep in mind that marking a relationship as async: false
will introduce
bugs into your application if the data is not always guaranteed to be available
by the time the relationship is accessed. Ergo, it is recommended when using this
approach to utilize links
for unloaded relationship state instead of identifiers.
Reference APIs are entangled with the relationship's underlying state, thus any getters or cached properties that utilize these will properly invalidate if the relationship state changes.
References are "stable", meaning that multiple calls to retrieve the reference for a given relationship will always return the same HasManyReference.
ids Array public
Defined in ../packages/model/src/-private/references/has-many.ts:246
- returns
- Array
The ids in this has-many relationship
ids()
returns an array of the record IDs in this relationship.
Example
import Model, { hasMany } from '@ember-data/model';
export default class PostModel extends Model {
@hasMany('comment', { async: true, inverse: null }) 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 public
Defined in ../packages/model/src/-private/references/has-many.ts:285
- 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
// 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();
}
links public
Defined in ../packages/model/src/-private/references/has-many.ts:335
- returns
any links that have been received for this relationship
load (options) Promise public
Defined in ../packages/model/src/-private/references/has-many.ts:618
- 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
import Model, { hasMany } from '@ember-data/model';
export default class PostModel extends Model {
@hasMany('comment', { async: true, inverse: null }) 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) {
//...
});
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|null public
Defined in ../packages/model/src/-private/references/has-many.ts:348
- returns
- Object|null
The meta information for the belongs-to relationship.
The meta data for the has-many relationship.
Example
// models/blog.js
import Model, { hasMany } from '@ember-data/model';
export default Model.extend({
users: hasMany('user', { async: true, inverse: null })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
users: {
links: {
related: {
href: '/articles/1/authors'
},
},
meta: {
lastUpdated: 1458014400000
}
}
}
}
});
let usersRef = blog.hasMany('user');
usersRef.meta() // { lastUpdated: 1458014400000 }
push (doc, skipFetch) Promise<ManyArray | void> public
Defined in ../packages/model/src/-private/references/has-many.ts:397
- doc
- Array|Object
a JSONAPI document object describing the new value of this relationship.
- skipFetch
- Boolean
if
true
, do not attempt to fetch unloaded records- returns
- Promise<ManyArray | void>
push
can be used to update the data in the relationship and EmberData
will treat the new data as the canonical value of this relationship on
the backend. An empty array will signify the canonical value should be
empty.
Example model
import Model, { hasMany } from '@ember-data/model';
export default class PostModel extends Model {
@hasMany('comment', { async: true, inverse: null }) comments;
}
Setup some initial state, note we haven't loaded the comments yet:
const post = store.push({
data: {
type: 'post',
id: '1',
relationships: {
comments: {
data: [{ type: 'comment', id: '1' }]
}
}
}
});
const commentsRef = post.hasMany('comments');
commentsRef.ids(); // ['1']
Update the state using push
, note we can do this even without
having loaded these comments yet by providing resource identifiers.
Both full resources and resource identifiers are supported.
await commentsRef.push({
data: [
{ type: 'comment', id: '2' },
{ type: 'comment', id: '3' },
]
});
commentsRef.ids(); // ['2', '3']
For convenience, you can also pass in an array of resources or resource identifiers
without wrapping them in the data
property:
await commentsRef.push([
{ type: 'comment', id: '4' },
{ type: 'comment', id: '5' },
]);
commentsRef.ids(); // ['4', '5']
When using the data
property, you may also include other resource data via included,
as well as provide new links and meta to the relationship.
await commentsRef.push({
links: {
related: '/posts/1/comments'
},
meta: {
total: 2
},
data: [
{ type: 'comment', id: '4' },
{ type: 'comment', id: '5' },
],
included: [
{ type: 'other-thing', id: '1', attributes: { foo: 'bar' },
]
});
By default, the store will attempt to fetch any unloaded records before resolving the returned promise with the ManyArray.
Alternatively, pass true
as the second argument to avoid fetching unloaded records
and instead the promise will resolve with void without attempting to fetch. This is
particularly useful if you want to update the state of the relationship without
forcing the load of all of the associated records.
reload (options) Promise public
Defined in ../packages/model/src/-private/references/has-many.ts:695
- 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
import Model, { hasMany } from '@ember-data/model';
export default class PostModel extends Model {
@hasMany('comment', { async: true, inverse: null }) 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 public
Defined in ../packages/model/src/-private/references/has-many.ts:195
- returns
- String
The name of the remote type. This should either be
link
orids
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
import Model, { hasMany } from '@ember-data/model';
export default class PostModel extends Model {
@hasMany('comment', { async: true, inverse: null }) 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 public
Defined in ../packages/model/src/-private/references/has-many.ts:560
- returns
- ManyArray
value()
synchronously returns the current value of the has-many
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
import Model, { hasMany } from '@ember-data/model';
export default class PostModel extends Model {
@hasMany('comment', { async: true, inverse: null }) comments;
}
let post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
data: [{ type: 'comment', id: 1 }]
}
}
}
});
let commentsRef = post.hasMany('comments');
post.comments.then(function(comments) {
commentsRef.value() === comments
})