DS.Model#hasMany()

hasMany (name) HasManyReference

Defined in addon/-private/system/model/model.js:883
Available since 2.5.0

Get the reference for the specified hasMany 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// models/blog.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
 
var blog = store.push({
  type: 'blog',
  id: 1,
  relationships: {
    comments: {
      data: [
        { type: 'comment', id: 1 },
        { type: 'comment', id: 2 }
      ]
    }
  }
});
var commentsRef = blog.hasMany('comments');
 
// check if the comments are loaded already
var isLoaded = commentsRef.value() !== null;
 
// get the records of the reference (null if not yet available)
var comments = commentsRef.value();
 
// get the identifier of the reference
if (commentsRef.remoteType() === "ids") {
  var ids = commentsRef.ids();
} else if (commentsRef.remoteType() === "link") {
  var link = commentsRef.link();
}
 
// load comments (via store.findMany or store.findHasMany)
commentsRef.load().then(...)
 
// or trigger a reload
commentsRef.reload().then(...)
 
// provide data for reference
commentsRef.push([{ type: 'comment', id: 1 }, { type: 'comment', id: 2 }]).then(function(comments) {
  commentsRef.value() === comments;
});

Parameters:

name String
of the relationship

Returns:

HasManyReference
reference for this relationship
doc_EmberJs
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.