@ember-data/model 4.11.0 → 4.12.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/addon/{-private/diff-array.ts → -private.js} +41 -13
- package/addon/-private.js.map +1 -0
- package/addon/has-many-a275fe0d.js +6283 -0
- package/addon/has-many-a275fe0d.js.map +1 -0
- package/addon/index.js +1 -0
- package/addon/index.js.map +1 -0
- package/addon-main.js +90 -0
- package/package.json +43 -15
- package/addon/-private/attr.js +0 -162
- package/addon/-private/belongs-to.js +0 -268
- package/addon/-private/debug/assert-polymorphic-type.js +0 -71
- package/addon/-private/deprecated-promise-proxy.ts +0 -76
- package/addon/-private/errors.ts +0 -425
- package/addon/-private/has-many.js +0 -280
- package/addon/-private/index.ts +0 -14
- package/addon/-private/legacy-data-fetch.js +0 -395
- package/addon/-private/legacy-data-utils.ts +0 -92
- package/addon/-private/legacy-relationships-support.ts +0 -774
- package/addon/-private/many-array.ts +0 -401
- package/addon/-private/model-for-mixin.ts +0 -38
- package/addon/-private/model.js +0 -2516
- package/addon/-private/notify-changes.ts +0 -72
- package/addon/-private/promise-belongs-to.ts +0 -73
- package/addon/-private/promise-many-array.ts +0 -425
- package/addon/-private/promise-proxy-base.js +0 -4
- package/addon/-private/record-state.ts +0 -468
- package/addon/-private/references/belongs-to.ts +0 -624
- package/addon/-private/references/has-many.ts +0 -669
- package/addon/-private/relationship-meta.ts +0 -98
- package/addon/-private/util.ts +0 -31
- package/addon/index.ts +0 -39
- package/blueprints/model/HELP.md +0 -26
- package/blueprints/model/files/__root__/__path__/__name__.js +0 -5
- package/blueprints/model/index.js +0 -158
- package/blueprints/model/native-files/__root__/__path__/__name__.js +0 -5
- package/blueprints/model-test/index.js +0 -33
- package/blueprints/model-test/mocha-files/__root__/__path__/__test__.js +0 -18
- package/blueprints/model-test/mocha-rfc-232-files/__root__/__path__/__test__.js +0 -15
- package/blueprints/model-test/qunit-files/__root__/__path__/__test__.js +0 -14
- package/index.js +0 -46
|
@@ -1,624 +0,0 @@
|
|
|
1
|
-
import { deprecate } from '@ember/debug';
|
|
2
|
-
import { dependentKeyCompat } from '@ember/object/compat';
|
|
3
|
-
import { DEBUG } from '@glimmer/env';
|
|
4
|
-
import { cached, tracked } from '@glimmer/tracking';
|
|
5
|
-
|
|
6
|
-
import type { Object as JSONObject, Value as JSONValue } from 'json-typescript';
|
|
7
|
-
import { resolve } from 'rsvp';
|
|
8
|
-
|
|
9
|
-
import { DEPRECATE_PROMISE_PROXIES } from '@ember-data/private-build-infra/deprecations';
|
|
10
|
-
import type { Graph } from '@ember-data/record-data/-private/graph/graph';
|
|
11
|
-
import type BelongsToRelationship from '@ember-data/record-data/-private/relationships/state/belongs-to';
|
|
12
|
-
import type Store from '@ember-data/store';
|
|
13
|
-
import { recordIdentifierFor } from '@ember-data/store/-private';
|
|
14
|
-
import type { NotificationType } from '@ember-data/store/-private/managers/record-notification-manager';
|
|
15
|
-
import type {
|
|
16
|
-
LinkObject,
|
|
17
|
-
Links,
|
|
18
|
-
SingleResourceDocument,
|
|
19
|
-
SingleResourceRelationship,
|
|
20
|
-
} from '@ember-data/types/q/ember-data-json-api';
|
|
21
|
-
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
|
|
22
|
-
import type { RecordInstance } from '@ember-data/types/q/record-instance';
|
|
23
|
-
import type { Dict } from '@ember-data/types/q/utils';
|
|
24
|
-
|
|
25
|
-
import { assertPolymorphicType } from '../debug/assert-polymorphic-type';
|
|
26
|
-
import type { LegacySupport } from '../legacy-relationships-support';
|
|
27
|
-
import { LEGACY_SUPPORT } from '../model';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
@module @ember-data/model
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
interface ResourceIdentifier {
|
|
34
|
-
links?: {
|
|
35
|
-
related?: string | LinkObject;
|
|
36
|
-
};
|
|
37
|
-
meta?: JSONObject;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function isResourceIdentiferWithRelatedLinks(
|
|
41
|
-
value: SingleResourceRelationship | ResourceIdentifier | null
|
|
42
|
-
): value is ResourceIdentifier & { links: { related: string | LinkObject | null } } {
|
|
43
|
-
return Boolean(value && value.links && value.links.related);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
A `BelongsToReference` is a low-level API that allows users and
|
|
48
|
-
addon authors to perform meta-operations on a belongs-to
|
|
49
|
-
relationship.
|
|
50
|
-
|
|
51
|
-
@class BelongsToReference
|
|
52
|
-
@public
|
|
53
|
-
*/
|
|
54
|
-
export default class BelongsToReference {
|
|
55
|
-
declare key: string;
|
|
56
|
-
declare belongsToRelationship: BelongsToRelationship;
|
|
57
|
-
declare type: string;
|
|
58
|
-
___identifier: StableRecordIdentifier;
|
|
59
|
-
declare store: Store;
|
|
60
|
-
declare graph: Graph;
|
|
61
|
-
|
|
62
|
-
// unsubscribe tokens given to us by the notification manager
|
|
63
|
-
___token!: object;
|
|
64
|
-
___relatedToken: object | null = null;
|
|
65
|
-
|
|
66
|
-
@tracked _ref = 0;
|
|
67
|
-
|
|
68
|
-
constructor(
|
|
69
|
-
store: Store,
|
|
70
|
-
graph: Graph,
|
|
71
|
-
parentIdentifier: StableRecordIdentifier,
|
|
72
|
-
belongsToRelationship: BelongsToRelationship,
|
|
73
|
-
key: string
|
|
74
|
-
) {
|
|
75
|
-
this.graph = graph;
|
|
76
|
-
this.key = key;
|
|
77
|
-
this.belongsToRelationship = belongsToRelationship;
|
|
78
|
-
this.type = belongsToRelationship.definition.type;
|
|
79
|
-
this.store = store;
|
|
80
|
-
this.___identifier = parentIdentifier;
|
|
81
|
-
|
|
82
|
-
this.___token = store._notificationManager.subscribe(
|
|
83
|
-
parentIdentifier,
|
|
84
|
-
(_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
|
|
85
|
-
if (bucket === 'relationships' && notifiedKey === key) {
|
|
86
|
-
this._ref++;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
// TODO inverse
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
destroy() {
|
|
95
|
-
// TODO @feature we need the notification manager often enough
|
|
96
|
-
// we should potentially just expose it fully public
|
|
97
|
-
this.store._notificationManager.unsubscribe(this.___token);
|
|
98
|
-
this.___token = null as unknown as object;
|
|
99
|
-
if (this.___relatedToken) {
|
|
100
|
-
this.store._notificationManager.unsubscribe(this.___relatedToken);
|
|
101
|
-
this.___relatedToken = null;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The identifier of the record that this reference refers to.
|
|
107
|
-
* `null` if no related record is known.
|
|
108
|
-
*
|
|
109
|
-
* @property {StableRecordIdentifier | null} identifier
|
|
110
|
-
* @public
|
|
111
|
-
*/
|
|
112
|
-
@cached
|
|
113
|
-
@dependentKeyCompat
|
|
114
|
-
get identifier(): StableRecordIdentifier | null {
|
|
115
|
-
this._ref; // consume the tracked prop
|
|
116
|
-
if (this.___relatedToken) {
|
|
117
|
-
this.store._notificationManager.unsubscribe(this.___relatedToken);
|
|
118
|
-
this.___relatedToken = null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
let resource = this._resource();
|
|
122
|
-
if (resource && resource.data) {
|
|
123
|
-
const identifier = this.store.identifierCache.getOrCreateRecordIdentifier(resource.data);
|
|
124
|
-
this.___relatedToken = this.store._notificationManager.subscribe(
|
|
125
|
-
identifier,
|
|
126
|
-
(_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
|
|
127
|
-
if (bucket === 'identity' || (bucket === 'attributes' && notifiedKey === 'id')) {
|
|
128
|
-
this._ref++;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
return identifier;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
The `id` of the record that this reference refers to. Together, the
|
|
141
|
-
`type()` and `id()` methods form a composite key for the identity
|
|
142
|
-
map. This can be used to access the id of an async relationship
|
|
143
|
-
without triggering a fetch that would normally happen if you
|
|
144
|
-
attempted to use `record.relationship.id`.
|
|
145
|
-
|
|
146
|
-
Example
|
|
147
|
-
|
|
148
|
-
```javascript
|
|
149
|
-
// models/blog.js
|
|
150
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
151
|
-
|
|
152
|
-
export default class BlogModel extends Model {
|
|
153
|
-
@belongsTo('user', { async: true, inverse: null }) user;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
let blog = store.push({
|
|
157
|
-
data: {
|
|
158
|
-
type: 'blog',
|
|
159
|
-
id: 1,
|
|
160
|
-
relationships: {
|
|
161
|
-
user: {
|
|
162
|
-
data: { type: 'user', id: 1 }
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
let userRef = blog.belongsTo('user');
|
|
168
|
-
|
|
169
|
-
// get the identifier of the reference
|
|
170
|
-
if (userRef.remoteType() === "id") {
|
|
171
|
-
let id = userRef.id();
|
|
172
|
-
}
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
@method id
|
|
176
|
-
@public
|
|
177
|
-
@return {String} The id of the record in this belongsTo relationship.
|
|
178
|
-
*/
|
|
179
|
-
id(): string | null {
|
|
180
|
-
return this.identifier?.id || null;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
The link Ember Data will use to fetch or reload this belongs-to
|
|
185
|
-
relationship. By default it uses only the "related" resource linkage.
|
|
186
|
-
|
|
187
|
-
Example
|
|
188
|
-
|
|
189
|
-
```javascript
|
|
190
|
-
// models/blog.js
|
|
191
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
192
|
-
export default Model.extend({
|
|
193
|
-
user: belongsTo('user', { async: true, inverse: null })
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
let blog = store.push({
|
|
197
|
-
data: {
|
|
198
|
-
type: 'blog',
|
|
199
|
-
id: 1,
|
|
200
|
-
relationships: {
|
|
201
|
-
user: {
|
|
202
|
-
links: {
|
|
203
|
-
related: '/articles/1/author'
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
let userRef = blog.belongsTo('user');
|
|
210
|
-
|
|
211
|
-
// get the identifier of the reference
|
|
212
|
-
if (userRef.remoteType() === "link") {
|
|
213
|
-
let link = userRef.link();
|
|
214
|
-
}
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
@method link
|
|
218
|
-
@public
|
|
219
|
-
@return {String} The link Ember Data will use to fetch or reload this belongs-to relationship.
|
|
220
|
-
*/
|
|
221
|
-
link(): string | null {
|
|
222
|
-
let resource = this._resource();
|
|
223
|
-
|
|
224
|
-
if (isResourceIdentiferWithRelatedLinks(resource)) {
|
|
225
|
-
if (resource.links) {
|
|
226
|
-
let related = resource.links.related;
|
|
227
|
-
return !related || typeof related === 'string' ? related : related.href;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return null;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* any links that have been received for this relationship
|
|
235
|
-
*
|
|
236
|
-
* @method links
|
|
237
|
-
* @public
|
|
238
|
-
* @returns
|
|
239
|
-
*/
|
|
240
|
-
links(): Links | null {
|
|
241
|
-
let resource = this._resource();
|
|
242
|
-
|
|
243
|
-
return resource && resource.links ? resource.links : null;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
The meta data for the belongs-to relationship.
|
|
248
|
-
|
|
249
|
-
Example
|
|
250
|
-
|
|
251
|
-
```javascript
|
|
252
|
-
// models/blog.js
|
|
253
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
254
|
-
export default Model.extend({
|
|
255
|
-
user: belongsTo('user', { async: true, inverse: null })
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
let blog = store.push({
|
|
259
|
-
data: {
|
|
260
|
-
type: 'blog',
|
|
261
|
-
id: 1,
|
|
262
|
-
relationships: {
|
|
263
|
-
user: {
|
|
264
|
-
links: {
|
|
265
|
-
related: {
|
|
266
|
-
href: '/articles/1/author'
|
|
267
|
-
},
|
|
268
|
-
},
|
|
269
|
-
meta: {
|
|
270
|
-
lastUpdated: 1458014400000
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
let userRef = blog.belongsTo('user');
|
|
278
|
-
|
|
279
|
-
userRef.meta() // { lastUpdated: 1458014400000 }
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
@method meta
|
|
283
|
-
@public
|
|
284
|
-
@return {Object} The meta information for the belongs-to relationship.
|
|
285
|
-
*/
|
|
286
|
-
meta() {
|
|
287
|
-
let meta: Dict<JSONValue> | null = null;
|
|
288
|
-
let resource = this._resource();
|
|
289
|
-
if (resource && resource.meta && typeof resource.meta === 'object') {
|
|
290
|
-
meta = resource.meta;
|
|
291
|
-
}
|
|
292
|
-
return meta;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
_resource() {
|
|
296
|
-
return this.store._instanceCache
|
|
297
|
-
.getRecordData(this.___identifier)
|
|
298
|
-
.getRelationship(this.___identifier, this.key) as SingleResourceRelationship;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
This returns a string that represents how the reference will be
|
|
303
|
-
looked up when it is loaded. If the relationship has a link it will
|
|
304
|
-
use the "link" otherwise it defaults to "id".
|
|
305
|
-
|
|
306
|
-
Example
|
|
307
|
-
|
|
308
|
-
```app/models/post.js
|
|
309
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
310
|
-
|
|
311
|
-
export default class PostModel extends Model {
|
|
312
|
-
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
313
|
-
}
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
```javascript
|
|
317
|
-
let post = store.push({
|
|
318
|
-
data: {
|
|
319
|
-
type: 'post',
|
|
320
|
-
id: 1,
|
|
321
|
-
relationships: {
|
|
322
|
-
comments: {
|
|
323
|
-
data: [{ type: 'comment', id: 1 }]
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
let commentsRef = post.hasMany('comments');
|
|
330
|
-
|
|
331
|
-
// get the identifier of the reference
|
|
332
|
-
if (commentsRef.remoteType() === "ids") {
|
|
333
|
-
let ids = commentsRef.ids();
|
|
334
|
-
} else if (commentsRef.remoteType() === "link") {
|
|
335
|
-
let link = commentsRef.link();
|
|
336
|
-
}
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
@method remoteType
|
|
340
|
-
@public
|
|
341
|
-
@return {String} The name of the remote type. This should either be `link` or `id`
|
|
342
|
-
*/
|
|
343
|
-
remoteType(): 'link' | 'id' {
|
|
344
|
-
let value = this._resource();
|
|
345
|
-
if (isResourceIdentiferWithRelatedLinks(value)) {
|
|
346
|
-
return 'link';
|
|
347
|
-
}
|
|
348
|
-
return 'id';
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
`push` can be used to update the data in the relationship and Ember
|
|
353
|
-
Data will treat the new data as the canonical value of this
|
|
354
|
-
relationship on the backend.
|
|
355
|
-
|
|
356
|
-
Example
|
|
357
|
-
|
|
358
|
-
```app/models/blog.js
|
|
359
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
360
|
-
|
|
361
|
-
export default class BlogModel extends Model {
|
|
362
|
-
@belongsTo('user', { async: true, inverse: null }) user;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
let blog = store.push({
|
|
366
|
-
data: {
|
|
367
|
-
type: 'blog',
|
|
368
|
-
id: 1,
|
|
369
|
-
relationships: {
|
|
370
|
-
user: {
|
|
371
|
-
data: { type: 'user', id: 1 }
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
});
|
|
376
|
-
let userRef = blog.belongsTo('user');
|
|
377
|
-
|
|
378
|
-
// provide data for reference
|
|
379
|
-
userRef.push({
|
|
380
|
-
data: {
|
|
381
|
-
type: 'user',
|
|
382
|
-
id: 1,
|
|
383
|
-
attributes: {
|
|
384
|
-
username: "@user"
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}).then(function(user) {
|
|
388
|
-
userRef.value() === user;
|
|
389
|
-
});
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
@method push
|
|
393
|
-
@public
|
|
394
|
-
@param {Object|Promise} objectOrPromise a promise that resolves to a JSONAPI document object describing the new value of this relationship.
|
|
395
|
-
@return {Promise<record>} A promise that resolves with the new value in this belongs-to relationship.
|
|
396
|
-
*/
|
|
397
|
-
async push(data: SingleResourceDocument | Promise<SingleResourceDocument>): Promise<RecordInstance> {
|
|
398
|
-
let jsonApiDoc: SingleResourceDocument = data as SingleResourceDocument;
|
|
399
|
-
if (DEPRECATE_PROMISE_PROXIES) {
|
|
400
|
-
if ((data as { then: unknown }).then) {
|
|
401
|
-
jsonApiDoc = await resolve(data);
|
|
402
|
-
if (jsonApiDoc !== data) {
|
|
403
|
-
deprecate(
|
|
404
|
-
`You passed in a Promise to a Reference API that now expects a resolved value. await the value before setting it.`,
|
|
405
|
-
false,
|
|
406
|
-
{
|
|
407
|
-
id: 'ember-data:deprecate-promise-proxies',
|
|
408
|
-
until: '5.0',
|
|
409
|
-
since: {
|
|
410
|
-
enabled: '4.7',
|
|
411
|
-
available: '4.7',
|
|
412
|
-
},
|
|
413
|
-
for: 'ember-data',
|
|
414
|
-
}
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
let record = this.store.push(jsonApiDoc);
|
|
420
|
-
|
|
421
|
-
if (DEBUG) {
|
|
422
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
423
|
-
assertPolymorphicType(
|
|
424
|
-
this.belongsToRelationship.identifier,
|
|
425
|
-
this.belongsToRelationship.definition,
|
|
426
|
-
recordIdentifierFor(record),
|
|
427
|
-
this.store
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
const { identifier } = this.belongsToRelationship;
|
|
432
|
-
this.store._join(() => {
|
|
433
|
-
this.graph.push({
|
|
434
|
-
op: 'replaceRelatedRecord',
|
|
435
|
-
record: identifier,
|
|
436
|
-
field: this.key,
|
|
437
|
-
value: recordIdentifierFor(record),
|
|
438
|
-
});
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
return record;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
`value()` synchronously returns the current value of the belongs-to
|
|
446
|
-
relationship. Unlike `record.relationshipName`, calling
|
|
447
|
-
`value()` on a reference does not trigger a fetch if the async
|
|
448
|
-
relationship is not yet loaded. If the relationship is not loaded
|
|
449
|
-
it will always return `null`.
|
|
450
|
-
|
|
451
|
-
Example
|
|
452
|
-
|
|
453
|
-
```javascript
|
|
454
|
-
// models/blog.js
|
|
455
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
456
|
-
|
|
457
|
-
export default class BlogModel extends Model {
|
|
458
|
-
@belongsTo('user', { async: true, inverse: null }) user;
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
let blog = store.push({
|
|
462
|
-
data: {
|
|
463
|
-
type: 'blog',
|
|
464
|
-
id: 1,
|
|
465
|
-
relationships: {
|
|
466
|
-
user: {
|
|
467
|
-
data: { type: 'user', id: 1 }
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
let userRef = blog.belongsTo('user');
|
|
473
|
-
|
|
474
|
-
userRef.value(); // null
|
|
475
|
-
|
|
476
|
-
// provide data for reference
|
|
477
|
-
userRef.push({
|
|
478
|
-
data: {
|
|
479
|
-
type: 'user',
|
|
480
|
-
id: 1,
|
|
481
|
-
attributes: {
|
|
482
|
-
username: "@user"
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}).then(function(user) {
|
|
486
|
-
userRef.value(); // user
|
|
487
|
-
});
|
|
488
|
-
```
|
|
489
|
-
|
|
490
|
-
@method value
|
|
491
|
-
@public
|
|
492
|
-
@return {Model} the record in this relationship
|
|
493
|
-
*/
|
|
494
|
-
value(): RecordInstance | null {
|
|
495
|
-
let resource = this._resource();
|
|
496
|
-
return resource && resource.data ? this.store.peekRecord(resource.data) : null;
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
/**
|
|
500
|
-
Loads a record in a belongs-to relationship if it is not already
|
|
501
|
-
loaded. If the relationship is already loaded this method does not
|
|
502
|
-
trigger a new load.
|
|
503
|
-
|
|
504
|
-
Example
|
|
505
|
-
|
|
506
|
-
```javascript
|
|
507
|
-
// models/blog.js
|
|
508
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
509
|
-
|
|
510
|
-
export default class BlogModel extends Model {
|
|
511
|
-
@belongsTo('user', { async: true, inverse: null }) user;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
let blog = store.push({
|
|
515
|
-
data: {
|
|
516
|
-
type: 'blog',
|
|
517
|
-
id: 1,
|
|
518
|
-
relationships: {
|
|
519
|
-
user: {
|
|
520
|
-
data: { type: 'user', id: 1 }
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
});
|
|
525
|
-
let userRef = blog.belongsTo('user');
|
|
526
|
-
|
|
527
|
-
userRef.value(); // null
|
|
528
|
-
|
|
529
|
-
userRef.load().then(function(user) {
|
|
530
|
-
userRef.value() === user
|
|
531
|
-
});
|
|
532
|
-
```
|
|
533
|
-
|
|
534
|
-
You may also pass in an options object whose properties will be
|
|
535
|
-
fed forward. This enables you to pass `adapterOptions` into the
|
|
536
|
-
request given to the adapter via the reference.
|
|
537
|
-
|
|
538
|
-
Example
|
|
539
|
-
|
|
540
|
-
```javascript
|
|
541
|
-
userRef.load({ adapterOptions: { isPrivate: true } }).then(function(user) {
|
|
542
|
-
userRef.value() === user;
|
|
543
|
-
});
|
|
544
|
-
```
|
|
545
|
-
```app/adapters/user.js
|
|
546
|
-
import Adapter from '@ember-data/adapter';
|
|
547
|
-
|
|
548
|
-
export default class UserAdapter extends Adapter {
|
|
549
|
-
findRecord(store, type, id, snapshot) {
|
|
550
|
-
// In the adapter you will have access to adapterOptions.
|
|
551
|
-
let adapterOptions = snapshot.adapterOptions;
|
|
552
|
-
}
|
|
553
|
-
});
|
|
554
|
-
```
|
|
555
|
-
|
|
556
|
-
@method load
|
|
557
|
-
@public
|
|
558
|
-
@param {Object} options the options to pass in.
|
|
559
|
-
@return {Promise} a promise that resolves with the record in this belongs-to relationship.
|
|
560
|
-
*/
|
|
561
|
-
load(options?: Dict<unknown>) {
|
|
562
|
-
const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
|
|
563
|
-
this.___identifier
|
|
564
|
-
)!;
|
|
565
|
-
return support.getBelongsTo(this.key, options);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
Triggers a reload of the value in this relationship. If the
|
|
570
|
-
remoteType is `"link"` Ember Data will use the relationship link to
|
|
571
|
-
reload the relationship. Otherwise it will reload the record by its
|
|
572
|
-
id.
|
|
573
|
-
|
|
574
|
-
Example
|
|
575
|
-
|
|
576
|
-
```javascript
|
|
577
|
-
// models/blog.js
|
|
578
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
579
|
-
|
|
580
|
-
export default class BlogModel extends Model {
|
|
581
|
-
@belongsTo('user', { async: true, inverse: null }) user;
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
let blog = store.push({
|
|
585
|
-
data: {
|
|
586
|
-
type: 'blog',
|
|
587
|
-
id: 1,
|
|
588
|
-
relationships: {
|
|
589
|
-
user: {
|
|
590
|
-
data: { type: 'user', id: 1 }
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
});
|
|
595
|
-
let userRef = blog.belongsTo('user');
|
|
596
|
-
|
|
597
|
-
userRef.reload().then(function(user) {
|
|
598
|
-
userRef.value() === user
|
|
599
|
-
});
|
|
600
|
-
```
|
|
601
|
-
|
|
602
|
-
You may also pass in an options object whose properties will be
|
|
603
|
-
fed forward. This enables you to pass `adapterOptions` into the
|
|
604
|
-
request given to the adapter via the reference. A full example
|
|
605
|
-
can be found in the `load` method.
|
|
606
|
-
|
|
607
|
-
Example
|
|
608
|
-
|
|
609
|
-
```javascript
|
|
610
|
-
userRef.reload({ adapterOptions: { isPrivate: true } })
|
|
611
|
-
```
|
|
612
|
-
|
|
613
|
-
@method reload
|
|
614
|
-
@public
|
|
615
|
-
@param {Object} options the options to pass in.
|
|
616
|
-
@return {Promise} a promise that resolves with the record in this belongs-to relationship after the reload has completed.
|
|
617
|
-
*/
|
|
618
|
-
reload(options?: Dict<unknown>) {
|
|
619
|
-
const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
|
|
620
|
-
this.___identifier
|
|
621
|
-
)!;
|
|
622
|
-
return support.reloadBelongsTo(this.key, options).then(() => this.value());
|
|
623
|
-
}
|
|
624
|
-
}
|