DS.HasManyReference

DS.HasManyReference Class

Defined in: addon/-private/system/references/has-many.js:16

Module: ember-data

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

Defined in addon/-private/system/references/has-many.js:83

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

Example

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
let post = store.push({
  data: {
    type: 'post',
    id: 1,
    relationships: {
      comments: {
        links: {
          related: '/posts/1/comments'
        }
      }
    }
  }
});

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

commentsRef.link(); // '/posts/1/comments'

Returns:

String
The link Ember Data will use to fetch or reload this has-many relationship.

loadPromise

Defined in addon/-private/system/references/has-many.js:365

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

Example

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
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) {
  //...
});

Returns:

Promise
a promise that resolves with the ManyArray in this has-many relationship.

metaObject

Defined in addon/-private/system/references/has-many.js:162

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

Example

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
let post = store.push({
  data: {
    type: 'post',
    id: 1,
    relationships: {
      comments: {
        links: {
          related: {
            href: '/posts/1/comments',
            meta: {
              count: 10
            }
          }
        }
      }
    }
  }
});

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

commentsRef.meta(); // { count: 10 }

Returns:

Object
The meta information for the has-many relationship.

push (objectOrPromise) DS.ManyArray

Defined in addon/-private/system/references/has-many.js:206

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
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
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']

Parameters:

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

Returns:

DS.ManyArray

reloadPromise

Defined in addon/-private/system/references/has-many.js:410

Reloads this has-many relationship.

Example

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
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) {
  //...
});

Returns:

Promise
a promise that resolves with the ManyArray in this has-many relationship.

remoteTypeArray

Inherited from DS.HasManyReference but overwritten in addon/-private/system/references/has-many.js:122

ids() returns an array of the record ids in this relationship.

Example

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
let post = store.push({
  data: {
    type: 'post',
    id: 1,
    relationships: {
      comments: {
        data: [{ type: 'comment', id: 1 }]
      }
    }
  }
});

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

commentsRef.ids(); // ['1']

Returns:

Array
The ids in this has-many relationship

valueDS.ManyArray

Defined in addon/-private/system/references/has-many.js:319

value() sycronously 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
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
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
})

Returns:

DS.ManyArray

© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/data/classes/DS.HasManyReference.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部