@warp-drive/legacy 5.8.0-alpha.9 → 5.8.0-beta.1
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/README.md +14 -27
- package/declarations/adapter/error.d.ts +7 -7
- package/declarations/adapter/json-api.d.ts +2 -2
- package/declarations/adapter/rest.d.ts +21 -104
- package/declarations/adapter.d.ts +2 -2
- package/declarations/compat/builders/find-all.d.ts +6 -6
- package/declarations/compat/builders/find-record.d.ts +8 -8
- package/declarations/compat/builders/query.d.ts +12 -12
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +16 -24
- package/declarations/compat/utils.d.ts +13 -13
- package/declarations/compat.d.ts +31 -5
- package/declarations/index.d.ts +25 -8
- package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
- package/declarations/model/-private/model.d.ts +10 -51
- package/declarations/model/-private/promise-many-array.d.ts +0 -18
- package/declarations/model/-private/references/belongs-to.d.ts +18 -28
- package/declarations/model/-private/references/has-many.d.ts +13 -15
- package/declarations/model/migration-support.d.ts +42 -19
- package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
- package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
- package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
- package/declarations/model-fragments/index.d.ts +5 -0
- package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
- package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
- package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
- package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
- package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
- package/declarations/model-fragments.d.ts +9 -0
- package/declarations/model.d.ts +2 -2
- package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -1
- package/declarations/serializer/json-api.d.ts +7 -6
- package/declarations/serializer/json.d.ts +3 -3
- package/declarations/serializer.d.ts +5 -6
- package/dist/{-private-B1pSSN52.js → -private-BG3bMiKp.js} +2 -1
- package/dist/adapter/-private.js +1 -1
- package/dist/adapter/error.js +9 -9
- package/dist/adapter/json-api.js +1 -1
- package/dist/adapter/rest.js +33 -117
- package/dist/adapter.js +2 -2
- package/dist/compat/-private.js +1 -1
- package/dist/compat/builders.js +26 -26
- package/dist/compat/utils.js +13 -14
- package/dist/compat.js +28 -8
- package/dist/{errors-COviC59J.js → errors-Cz5KrzBk.js} +113 -120
- package/dist/{hooks-Bp8SIQBU.js → hooks-D6diaM34.js} +1 -1
- package/dist/index.js +25 -8
- package/dist/{json-ksWOHRfq.js → json-ChdEfB0X.js} +12 -27
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +55 -25
- package/dist/model-for-CqXsIKws.js +221 -0
- package/dist/model-fragments.js +76 -0
- package/dist/model.js +3 -3
- package/dist/{schema-provider-JlCneqZH.js → schema-provider-DJCV_6AF.js} +44 -87
- package/dist/{serialize-into-hash-BnYvPex3.js → serialize-into-hash-DPZYoF-i.js} +1 -1
- package/dist/serializer/json-api.js +18 -45
- package/dist/serializer/json.js +1 -1
- package/dist/serializer/rest.js +10 -10
- package/dist/serializer.js +5 -6
- package/dist/store.js +2 -1
- package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev/adapter/-private.js +1 -0
- package/dist/unpkg/dev/adapter/error.js +335 -0
- package/dist/unpkg/dev/adapter/json-api.js +271 -0
- package/dist/unpkg/dev/adapter/rest.js +1171 -0
- package/dist/unpkg/dev/adapter.js +1252 -0
- package/dist/unpkg/dev/compat/-private.js +1 -0
- package/dist/unpkg/dev/compat/builders.js +275 -0
- package/dist/unpkg/dev/compat/extensions.js +242 -0
- package/dist/unpkg/dev/compat/utils.js +223 -0
- package/dist/unpkg/dev/compat.js +1147 -0
- package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
- package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
- package/dist/unpkg/dev/index.js +197 -0
- package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev/model/-private.js +1 -0
- package/dist/unpkg/dev/model/migration-support.js +553 -0
- package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev/model-fragments.js +76 -0
- package/dist/unpkg/dev/model.js +678 -0
- package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev/schema-provider-DDVYxmUV.js +2186 -0
- package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev/serializer/json-api.js +649 -0
- package/dist/unpkg/dev/serializer/json.js +4 -0
- package/dist/unpkg/dev/serializer/rest.js +1242 -0
- package/dist/unpkg/dev/serializer/transform.js +278 -0
- package/dist/unpkg/dev/serializer.js +248 -0
- package/dist/unpkg/dev/store.js +637 -0
- package/dist/unpkg/dev/util-DvanW33H.js +20 -0
- package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
- package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
- package/dist/unpkg/dev-deprecated/adapter/rest.js +1171 -0
- package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
- package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
- package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
- package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
- package/dist/unpkg/dev-deprecated/compat.js +1147 -0
- package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
- package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
- package/dist/unpkg/dev-deprecated/index.js +196 -0
- package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -0
- package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/dev-deprecated/model.js +682 -0
- package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev-deprecated/schema-provider-BP6_8N-V.js +2211 -0
- package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -0
- package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
- package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/dev-deprecated/serializer.js +248 -0
- package/dist/unpkg/dev-deprecated/store.js +637 -0
- package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
- package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
- package/dist/unpkg/prod/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod/adapter/-private.js +1 -0
- package/dist/unpkg/prod/adapter/error.js +330 -0
- package/dist/unpkg/prod/adapter/json-api.js +266 -0
- package/dist/unpkg/prod/adapter/rest.js +1134 -0
- package/dist/unpkg/prod/adapter.js +1219 -0
- package/dist/unpkg/prod/compat/-private.js +1 -0
- package/dist/unpkg/prod/compat/builders.js +210 -0
- package/dist/unpkg/prod/compat/extensions.js +232 -0
- package/dist/unpkg/prod/compat/utils.js +218 -0
- package/dist/unpkg/prod/compat.js +727 -0
- package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
- package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
- package/dist/unpkg/prod/index.js +151 -0
- package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod/model/-private.js +1 -0
- package/dist/unpkg/prod/model/migration-support.js +546 -0
- package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod/model-fragments.js +76 -0
- package/dist/unpkg/prod/model.js +593 -0
- package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod/schema-provider-DJtD_8jZ.js +1861 -0
- package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod/serializer/json-api.js +592 -0
- package/dist/unpkg/prod/serializer/json.js +4 -0
- package/dist/unpkg/prod/serializer/rest.js +1210 -0
- package/dist/unpkg/prod/serializer/transform.js +278 -0
- package/dist/unpkg/prod/serializer.js +248 -0
- package/dist/unpkg/prod/store.js +505 -0
- package/dist/unpkg/prod/util-DvanW33H.js +20 -0
- package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
- package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
- package/dist/unpkg/prod-deprecated/adapter/rest.js +1134 -0
- package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
- package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
- package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
- package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
- package/dist/unpkg/prod-deprecated/compat.js +727 -0
- package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
- package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
- package/dist/unpkg/prod-deprecated/index.js +150 -0
- package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -0
- package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/prod-deprecated/model.js +596 -0
- package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod-deprecated/schema-provider-CjX55uSY.js +1904 -0
- package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -0
- package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
- package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/prod-deprecated/serializer.js +248 -0
- package/dist/unpkg/prod-deprecated/store.js +505 -0
- package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
- package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +14 -6
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -0,0 +1,2317 @@
|
|
|
1
|
+
import { Context } from '@warp-drive/core/reactive/-private';
|
|
2
|
+
import { memoized, defineSignal, defineNonEnumerableSignal, notifyInternalSignal } from '@warp-drive/core/signals/-leaked';
|
|
3
|
+
import { assertPrivateStore, recordIdentifierFor, isPrivateStore, storeFor, fastPush, createLegacyManyArray } from '@warp-drive/core/store/-private';
|
|
4
|
+
import { getOrSetGlobal } from '@warp-drive/core/types/-private';
|
|
5
|
+
import { EnableHydration } from '@warp-drive/core/types/request';
|
|
6
|
+
import { u as upgradeStore } from "./-private-BdyZaGEh.js";
|
|
7
|
+
import { computed, get } from '@ember/object';
|
|
8
|
+
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
|
|
9
|
+
import ObjectProxy from '@ember/object/proxy';
|
|
10
|
+
import { d as decorateMethodV2, a as decorateFieldV2, i as initializeDeferredDecorator } from "./runtime-BPCpkOf1-BKOwiRJp.js";
|
|
11
|
+
import { A } from '@ember/array';
|
|
12
|
+
import ArrayProxy from '@ember/array/proxy';
|
|
13
|
+
import { mapBy, not } from '@ember/object/computed';
|
|
14
|
+
const PromiseObject = ObjectProxy.extend(PromiseProxyMixin);
|
|
15
|
+
const LegacyPromiseProxy = Symbol.for('LegacyPromiseProxy');
|
|
16
|
+
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-extraneous-class
|
|
18
|
+
|
|
19
|
+
const Extended = PromiseObject;
|
|
20
|
+
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
A PromiseBelongsTo is a PromiseObject that also proxies certain method calls
|
|
25
|
+
to the underlying belongsTo model.
|
|
26
|
+
Right now we proxy:
|
|
27
|
+
* `reload()`
|
|
28
|
+
@class PromiseBelongsTo
|
|
29
|
+
@private
|
|
30
|
+
*/
|
|
31
|
+
class PromiseBelongsTo extends Extended {
|
|
32
|
+
get id() {
|
|
33
|
+
const {
|
|
34
|
+
key,
|
|
35
|
+
legacySupport
|
|
36
|
+
} = this._belongsToState;
|
|
37
|
+
const ref = legacySupport.referenceFor('belongsTo', key);
|
|
38
|
+
return ref.id();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// we don't proxy meta because we would need to proxy it to the relationship state container
|
|
42
|
+
// however, meta on relationships does not trigger change notifications.
|
|
43
|
+
// if you need relationship meta, you should do `record.belongsTo(relationshipName).meta()`
|
|
44
|
+
static {
|
|
45
|
+
decorateMethodV2(this.prototype, "id", [memoized]);
|
|
46
|
+
}
|
|
47
|
+
get meta() {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
static {
|
|
51
|
+
decorateMethodV2(this.prototype, "meta", [computed()]);
|
|
52
|
+
}
|
|
53
|
+
async reload(options) {
|
|
54
|
+
const {
|
|
55
|
+
key,
|
|
56
|
+
legacySupport
|
|
57
|
+
} = this._belongsToState;
|
|
58
|
+
await legacySupport.reloadBelongsTo(key, options);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
PromiseBelongsTo.prototype[LegacyPromiseProxy] = true;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
This class is returned as the result of accessing an async hasMany relationship
|
|
66
|
+
on an instance of a Model extending from `@ember-data/model`.
|
|
67
|
+
|
|
68
|
+
A PromiseManyArray is an iterable proxy that allows templates to consume related
|
|
69
|
+
ManyArrays and update once their contents are no longer pending.
|
|
70
|
+
|
|
71
|
+
In your JS code you should resolve the promise first.
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
const comments = await post.comments;
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
@public
|
|
78
|
+
*/
|
|
79
|
+
class PromiseManyArray {
|
|
80
|
+
constructor(promise, content) {
|
|
81
|
+
this._update(promise, content);
|
|
82
|
+
this.isDestroyed = false;
|
|
83
|
+
this[LegacyPromiseProxy] = true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Retrieve the length of the content
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
get length() {
|
|
91
|
+
// shouldn't be needed, but ends up being needed
|
|
92
|
+
// for computed chains even in 4.x
|
|
93
|
+
{
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
95
|
+
this['[]'];
|
|
96
|
+
}
|
|
97
|
+
return this.content ? this.content.length : 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// this will error if someone tries to call
|
|
101
|
+
// A(identifierArray) since it is not configurable
|
|
102
|
+
// which is preferrable to the `meta` override we used
|
|
103
|
+
// before which required importing all of Ember
|
|
104
|
+
static {
|
|
105
|
+
decorateMethodV2(this.prototype, "length", [memoized]);
|
|
106
|
+
}
|
|
107
|
+
get '[]'() {
|
|
108
|
+
// ember-source < 3.23 (e.g. 3.20 lts)
|
|
109
|
+
// requires that the tag `'[]'` be notified
|
|
110
|
+
// on the ArrayProxy in order for `{{#each}}`
|
|
111
|
+
// to recompute. We entangle content.
|
|
112
|
+
return this.content?.length && this.content;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Iterate the proxied content. Called by the glimmer iterator in #each
|
|
117
|
+
* We do not guarantee that forEach will always be available. This
|
|
118
|
+
* may eventually be made to use Symbol.Iterator once glimmer supports it.
|
|
119
|
+
*
|
|
120
|
+
* @param cb
|
|
121
|
+
* @return
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
static {
|
|
125
|
+
decorateMethodV2(this.prototype, '[]', [memoized]);
|
|
126
|
+
}
|
|
127
|
+
forEach(cb) {
|
|
128
|
+
if (this.content && this.length) {
|
|
129
|
+
this.content.forEach(cb);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Reload the relationship
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
reload(options) {
|
|
138
|
+
void this.content.reload(options);
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//---- Properties/Methods from the PromiseProxyMixin that we will keep as our API
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Whether the loading promise is still pending
|
|
146
|
+
*
|
|
147
|
+
* @public
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Whether the loading promise rejected
|
|
152
|
+
*
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Whether the loading promise succeeded
|
|
158
|
+
*
|
|
159
|
+
* @public
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Whether the loading promise completed (resolved or rejected)
|
|
164
|
+
*
|
|
165
|
+
* @public
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* chain this promise
|
|
170
|
+
*
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
then(success, rejected) {
|
|
174
|
+
return this.promise.then(success, rejected);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* catch errors thrown by this promise
|
|
179
|
+
* @public
|
|
180
|
+
*/
|
|
181
|
+
catch(cb) {
|
|
182
|
+
return this.promise.catch(cb);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* run cleanup after this promise completes
|
|
187
|
+
*
|
|
188
|
+
* @public
|
|
189
|
+
*/
|
|
190
|
+
finally(cb) {
|
|
191
|
+
return this.promise.finally(cb);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//---- Methods on EmberObject that we should keep
|
|
195
|
+
|
|
196
|
+
destroy() {
|
|
197
|
+
this.isDestroyed = true;
|
|
198
|
+
this.content = null;
|
|
199
|
+
this.promise = null;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
//---- Methods/Properties on ManyArray that we own and proxy to
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Retrieve the links for this relationship
|
|
206
|
+
* @public
|
|
207
|
+
*/
|
|
208
|
+
get links() {
|
|
209
|
+
return this.content ? this.content.links : undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Retrieve the meta for this relationship
|
|
214
|
+
* @public
|
|
215
|
+
*/
|
|
216
|
+
static {
|
|
217
|
+
decorateMethodV2(this.prototype, "links", [memoized]);
|
|
218
|
+
}
|
|
219
|
+
get meta() {
|
|
220
|
+
return this.content ? this.content.meta : undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
//---- Our own stuff
|
|
224
|
+
|
|
225
|
+
/** @internal */
|
|
226
|
+
static {
|
|
227
|
+
decorateMethodV2(this.prototype, "meta", [memoized]);
|
|
228
|
+
}
|
|
229
|
+
_update(promise, content) {
|
|
230
|
+
if (content !== undefined) {
|
|
231
|
+
this.content = content;
|
|
232
|
+
}
|
|
233
|
+
this.promise = tapPromise(this, promise);
|
|
234
|
+
}
|
|
235
|
+
static create({
|
|
236
|
+
promise,
|
|
237
|
+
content
|
|
238
|
+
}) {
|
|
239
|
+
return new this(promise, content);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
defineSignal(PromiseManyArray.prototype, 'content', null);
|
|
243
|
+
defineSignal(PromiseManyArray.prototype, 'isPending', false);
|
|
244
|
+
defineSignal(PromiseManyArray.prototype, 'isRejected', false);
|
|
245
|
+
defineSignal(PromiseManyArray.prototype, 'isFulfilled', false);
|
|
246
|
+
defineSignal(PromiseManyArray.prototype, 'isSettled', false);
|
|
247
|
+
function tapPromise(proxy, promise) {
|
|
248
|
+
proxy.isPending = true;
|
|
249
|
+
proxy.isSettled = false;
|
|
250
|
+
proxy.isFulfilled = false;
|
|
251
|
+
proxy.isRejected = false;
|
|
252
|
+
return Promise.resolve(promise).then(content => {
|
|
253
|
+
proxy.isPending = false;
|
|
254
|
+
proxy.isFulfilled = true;
|
|
255
|
+
proxy.isSettled = true;
|
|
256
|
+
proxy.content = content;
|
|
257
|
+
return content;
|
|
258
|
+
}, error => {
|
|
259
|
+
proxy.isPending = false;
|
|
260
|
+
proxy.isFulfilled = false;
|
|
261
|
+
proxy.isRejected = true;
|
|
262
|
+
proxy.isSettled = true;
|
|
263
|
+
throw error;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
function isResourceIdentiferWithRelatedLinks$1(value) {
|
|
267
|
+
return Boolean(value && value.links && value.links.related);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
A `HasManyReference` is a low-level API that allows access
|
|
271
|
+
and manipulation of a hasMany relationship.
|
|
272
|
+
|
|
273
|
+
It is especially useful when you're dealing with `async` relationships
|
|
274
|
+
from `@warp-drive/legacy/model` as it allows synchronous access to
|
|
275
|
+
the relationship data if loaded, as well as APIs for loading, reloading
|
|
276
|
+
the data or accessing available information without triggering a load.
|
|
277
|
+
|
|
278
|
+
It may also be useful when using `sync` relationships with `@warp-drive/legacy/model`
|
|
279
|
+
that need to be loaded/reloaded with more precise timing than marking the
|
|
280
|
+
relationship as `async` and relying on autofetch would have allowed.
|
|
281
|
+
|
|
282
|
+
However,keep in mind that marking a relationship as `async: false` will introduce
|
|
283
|
+
bugs into your application if the data is not always guaranteed to be available
|
|
284
|
+
by the time the relationship is accessed. Ergo, it is recommended when using this
|
|
285
|
+
approach to utilize `links` for unloaded relationship state instead of identifiers.
|
|
286
|
+
|
|
287
|
+
Reference APIs are entangled with the relationship's underlying state,
|
|
288
|
+
thus any getters or cached properties that utilize these will properly
|
|
289
|
+
invalidate if the relationship state changes.
|
|
290
|
+
|
|
291
|
+
References are "stable", meaning that multiple calls to retrieve the reference
|
|
292
|
+
for a given relationship will always return the same HasManyReference.
|
|
293
|
+
|
|
294
|
+
@class HasManyReference
|
|
295
|
+
@public
|
|
296
|
+
*/
|
|
297
|
+
class HasManyReference {
|
|
298
|
+
/**
|
|
299
|
+
* The field name on the parent record for this has-many relationship.
|
|
300
|
+
*
|
|
301
|
+
* @property key
|
|
302
|
+
* @type {String}
|
|
303
|
+
* @public
|
|
304
|
+
*/
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The type of resource this relationship will contain.
|
|
308
|
+
*
|
|
309
|
+
* @property type
|
|
310
|
+
* @type {String}
|
|
311
|
+
* @public
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
// unsubscribe tokens given to us by the notification manager
|
|
315
|
+
___token;
|
|
316
|
+
___identifier;
|
|
317
|
+
___relatedTokenMap;
|
|
318
|
+
constructor(store, graph, parentIdentifier, hasManyRelationship, key) {
|
|
319
|
+
this.graph = graph;
|
|
320
|
+
this.key = key;
|
|
321
|
+
this.hasManyRelationship = hasManyRelationship;
|
|
322
|
+
this.type = hasManyRelationship.definition.type;
|
|
323
|
+
this.store = store;
|
|
324
|
+
this.___identifier = parentIdentifier;
|
|
325
|
+
this.___token = store.notifications.subscribe(parentIdentifier, (_, bucket, notifiedKey) => {
|
|
326
|
+
if (bucket === 'relationships' && notifiedKey === key) {
|
|
327
|
+
this._ref++;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
this.___relatedTokenMap = new Map();
|
|
331
|
+
// TODO inverse
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* This method should never be called by user code.
|
|
336
|
+
*
|
|
337
|
+
* @internal
|
|
338
|
+
*/
|
|
339
|
+
destroy() {
|
|
340
|
+
this.store.notifications.unsubscribe(this.___token);
|
|
341
|
+
this.___relatedTokenMap.forEach(token => {
|
|
342
|
+
this.store.notifications.unsubscribe(token);
|
|
343
|
+
});
|
|
344
|
+
this.___relatedTokenMap.clear();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* An array of identifiers for the records that this reference refers to.
|
|
349
|
+
*
|
|
350
|
+
* @property identifiers
|
|
351
|
+
* @type {ResourceKey[]}
|
|
352
|
+
* @public
|
|
353
|
+
*/
|
|
354
|
+
get identifiers() {
|
|
355
|
+
ensureRefCanSubscribe(this);
|
|
356
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
357
|
+
this._ref;
|
|
358
|
+
const resource = this._resource();
|
|
359
|
+
const map = this.___relatedTokenMap;
|
|
360
|
+
this.___relatedTokenMap = new Map();
|
|
361
|
+
if (resource && resource.data) {
|
|
362
|
+
return resource.data.map(resourceIdentifier => {
|
|
363
|
+
const identifier = this.store.cacheKeyManager.getOrCreateRecordIdentifier(resourceIdentifier);
|
|
364
|
+
let token = map.get(identifier);
|
|
365
|
+
if (token) {
|
|
366
|
+
map.delete(identifier);
|
|
367
|
+
} else {
|
|
368
|
+
token = this.store.notifications.subscribe(identifier, (_, bucket, notifiedKey) => {
|
|
369
|
+
if (bucket === 'identity' || bucket === 'attributes' && notifiedKey === 'id') {
|
|
370
|
+
this._ref++;
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
this.___relatedTokenMap.set(identifier, token);
|
|
375
|
+
return identifier;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
map.forEach(token => {
|
|
379
|
+
this.store.notifications.unsubscribe(token);
|
|
380
|
+
});
|
|
381
|
+
map.clear();
|
|
382
|
+
return [];
|
|
383
|
+
}
|
|
384
|
+
static {
|
|
385
|
+
decorateMethodV2(this.prototype, "identifiers", [memoized]);
|
|
386
|
+
}
|
|
387
|
+
_resource() {
|
|
388
|
+
const cache = this.store.cache;
|
|
389
|
+
return cache.getRelationship(this.___identifier, this.key);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
This returns a string that represents how the reference will be
|
|
394
|
+
looked up when it is loaded. If the relationship has a link it will
|
|
395
|
+
use the "link" otherwise it defaults to "id".
|
|
396
|
+
Example
|
|
397
|
+
```js [app/models/post.js]
|
|
398
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
399
|
+
export default class PostModel extends Model {
|
|
400
|
+
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
```javascript
|
|
404
|
+
let post = store.push({
|
|
405
|
+
data: {
|
|
406
|
+
type: 'post',
|
|
407
|
+
id: 1,
|
|
408
|
+
relationships: {
|
|
409
|
+
comments: {
|
|
410
|
+
data: [{ type: 'comment', id: 1 }]
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
let commentsRef = post.hasMany('comments');
|
|
416
|
+
// get the identifier of the reference
|
|
417
|
+
if (commentsRef.remoteType() === "ids") {
|
|
418
|
+
let ids = commentsRef.ids();
|
|
419
|
+
} else if (commentsRef.remoteType() === "link") {
|
|
420
|
+
let link = commentsRef.link();
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
@public
|
|
424
|
+
@return The name of the remote type. This should either be `link` or `ids`
|
|
425
|
+
*/
|
|
426
|
+
remoteType() {
|
|
427
|
+
const value = this._resource();
|
|
428
|
+
if (value && value.links && value.links.related) {
|
|
429
|
+
return 'link';
|
|
430
|
+
}
|
|
431
|
+
return 'ids';
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
`ids()` returns an array of the record IDs in this relationship.
|
|
436
|
+
Example
|
|
437
|
+
```js [app/models/post.js]
|
|
438
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
439
|
+
export default class PostModel extends Model {
|
|
440
|
+
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
441
|
+
}
|
|
442
|
+
```
|
|
443
|
+
```javascript
|
|
444
|
+
let post = store.push({
|
|
445
|
+
data: {
|
|
446
|
+
type: 'post',
|
|
447
|
+
id: 1,
|
|
448
|
+
relationships: {
|
|
449
|
+
comments: {
|
|
450
|
+
data: [{ type: 'comment', id: 1 }]
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
let commentsRef = post.hasMany('comments');
|
|
456
|
+
commentsRef.ids(); // ['1']
|
|
457
|
+
```
|
|
458
|
+
@public
|
|
459
|
+
@return The ids in this has-many relationship
|
|
460
|
+
*/
|
|
461
|
+
ids() {
|
|
462
|
+
return this.identifiers.map(identifier => identifier.id);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
The link WarpDrive will use to fetch or reload this belongs-to
|
|
467
|
+
relationship. By default it uses only the "related" resource linkage.
|
|
468
|
+
Example
|
|
469
|
+
```javascript
|
|
470
|
+
// models/blog.js
|
|
471
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
472
|
+
export default Model.extend({
|
|
473
|
+
user: belongsTo('user', { async: true, inverse: null })
|
|
474
|
+
});
|
|
475
|
+
let blog = store.push({
|
|
476
|
+
data: {
|
|
477
|
+
type: 'blog',
|
|
478
|
+
id: 1,
|
|
479
|
+
relationships: {
|
|
480
|
+
user: {
|
|
481
|
+
links: {
|
|
482
|
+
related: '/articles/1/author'
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
let userRef = blog.belongsTo('user');
|
|
489
|
+
// get the identifier of the reference
|
|
490
|
+
if (userRef.remoteType() === "link") {
|
|
491
|
+
let link = userRef.link();
|
|
492
|
+
}
|
|
493
|
+
```
|
|
494
|
+
@public
|
|
495
|
+
@return The link WarpDrive will use to fetch or reload this belongs-to relationship.
|
|
496
|
+
*/
|
|
497
|
+
link() {
|
|
498
|
+
const resource = this._resource();
|
|
499
|
+
if (isResourceIdentiferWithRelatedLinks$1(resource)) {
|
|
500
|
+
if (resource.links) {
|
|
501
|
+
const related = resource.links.related;
|
|
502
|
+
return !related || typeof related === 'string' ? related : related.href;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* any links that have been received for this relationship
|
|
510
|
+
*
|
|
511
|
+
* @public
|
|
512
|
+
* @return
|
|
513
|
+
*/
|
|
514
|
+
links() {
|
|
515
|
+
const resource = this._resource();
|
|
516
|
+
return resource && resource.links ? resource.links : null;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
The meta data for the has-many relationship.
|
|
521
|
+
Example
|
|
522
|
+
```javascript
|
|
523
|
+
// models/blog.js
|
|
524
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
525
|
+
export default Model.extend({
|
|
526
|
+
users: hasMany('user', { async: true, inverse: null })
|
|
527
|
+
});
|
|
528
|
+
let blog = store.push({
|
|
529
|
+
data: {
|
|
530
|
+
type: 'blog',
|
|
531
|
+
id: 1,
|
|
532
|
+
relationships: {
|
|
533
|
+
users: {
|
|
534
|
+
links: {
|
|
535
|
+
related: {
|
|
536
|
+
href: '/articles/1/authors'
|
|
537
|
+
},
|
|
538
|
+
},
|
|
539
|
+
meta: {
|
|
540
|
+
lastUpdated: 1458014400000
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
let usersRef = blog.hasMany('user');
|
|
547
|
+
usersRef.meta() // { lastUpdated: 1458014400000 }
|
|
548
|
+
```
|
|
549
|
+
@public
|
|
550
|
+
@return The meta information for the belongs-to relationship.
|
|
551
|
+
*/
|
|
552
|
+
meta() {
|
|
553
|
+
let meta = null;
|
|
554
|
+
const resource = this._resource();
|
|
555
|
+
if (resource && resource.meta && typeof resource.meta === 'object') {
|
|
556
|
+
meta = resource.meta;
|
|
557
|
+
}
|
|
558
|
+
return meta;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
`push` can be used to update the data in the relationship and WarpDrive
|
|
563
|
+
will treat the new data as the canonical value of this relationship on
|
|
564
|
+
the backend. An empty array will signify the canonical value should be
|
|
565
|
+
empty.
|
|
566
|
+
Example model
|
|
567
|
+
```js [app/models/post.js]
|
|
568
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
569
|
+
export default class PostModel extends Model {
|
|
570
|
+
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
571
|
+
}
|
|
572
|
+
```
|
|
573
|
+
Setup some initial state, note we haven't loaded the comments yet:
|
|
574
|
+
```js
|
|
575
|
+
const post = store.push({
|
|
576
|
+
data: {
|
|
577
|
+
type: 'post',
|
|
578
|
+
id: '1',
|
|
579
|
+
relationships: {
|
|
580
|
+
comments: {
|
|
581
|
+
data: [{ type: 'comment', id: '1' }]
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
const commentsRef = post.hasMany('comments');
|
|
587
|
+
commentsRef.ids(); // ['1']
|
|
588
|
+
```
|
|
589
|
+
Update the state using `push`, note we can do this even without
|
|
590
|
+
having loaded these comments yet by providing resource identifiers.
|
|
591
|
+
Both full resources and resource identifiers are supported.
|
|
592
|
+
```js
|
|
593
|
+
await commentsRef.push({
|
|
594
|
+
data: [
|
|
595
|
+
{ type: 'comment', id: '2' },
|
|
596
|
+
{ type: 'comment', id: '3' },
|
|
597
|
+
]
|
|
598
|
+
});
|
|
599
|
+
commentsRef.ids(); // ['2', '3']
|
|
600
|
+
```
|
|
601
|
+
For convenience, you can also pass in an array of resources or resource identifiers
|
|
602
|
+
without wrapping them in the `data` property:
|
|
603
|
+
```js
|
|
604
|
+
await commentsRef.push([
|
|
605
|
+
{ type: 'comment', id: '4' },
|
|
606
|
+
{ type: 'comment', id: '5' },
|
|
607
|
+
]);
|
|
608
|
+
commentsRef.ids(); // ['4', '5']
|
|
609
|
+
```
|
|
610
|
+
When using the `data` property, you may also include other resource data via included,
|
|
611
|
+
as well as provide new links and meta to the relationship.
|
|
612
|
+
```js
|
|
613
|
+
await commentsRef.push({
|
|
614
|
+
links: {
|
|
615
|
+
related: '/posts/1/comments'
|
|
616
|
+
},
|
|
617
|
+
meta: {
|
|
618
|
+
total: 2
|
|
619
|
+
},
|
|
620
|
+
data: [
|
|
621
|
+
{ type: 'comment', id: '4' },
|
|
622
|
+
{ type: 'comment', id: '5' },
|
|
623
|
+
],
|
|
624
|
+
included: [
|
|
625
|
+
{ type: 'other-thing', id: '1', attributes: { foo: 'bar' },
|
|
626
|
+
]
|
|
627
|
+
});
|
|
628
|
+
```
|
|
629
|
+
By default, the store will attempt to fetch any unloaded records before resolving
|
|
630
|
+
the returned promise with the ManyArray.
|
|
631
|
+
Alternatively, pass `true` as the second argument to avoid fetching unloaded records
|
|
632
|
+
and instead the promise will resolve with void without attempting to fetch. This is
|
|
633
|
+
particularly useful if you want to update the state of the relationship without
|
|
634
|
+
forcing the load of all of the associated records.
|
|
635
|
+
@public
|
|
636
|
+
@param doc a JSONAPI document object describing the new value of this relationship.
|
|
637
|
+
@param skipFetch [optional] if `true`, do not attempt to fetch unloaded records
|
|
638
|
+
*/
|
|
639
|
+
async push(doc, skipFetch) {
|
|
640
|
+
const {
|
|
641
|
+
store
|
|
642
|
+
} = this;
|
|
643
|
+
const dataDoc = Array.isArray(doc) ? {
|
|
644
|
+
data: doc
|
|
645
|
+
} : doc;
|
|
646
|
+
const isResourceData = Array.isArray(dataDoc.data) && dataDoc.data.length > 0 && isMaybeResource(dataDoc.data[0]);
|
|
647
|
+
const identifiers = !Array.isArray(dataDoc.data) ? [] : isResourceData ? store._push(dataDoc, true) : dataDoc.data.map(i => store.cacheKeyManager.getOrCreateRecordIdentifier(i));
|
|
648
|
+
const {
|
|
649
|
+
identifier
|
|
650
|
+
} = this.hasManyRelationship;
|
|
651
|
+
const newData = {};
|
|
652
|
+
// only set data if it was passed in
|
|
653
|
+
if (Array.isArray(dataDoc.data)) {
|
|
654
|
+
newData.data = identifiers;
|
|
655
|
+
}
|
|
656
|
+
if ('links' in dataDoc) {
|
|
657
|
+
newData.links = dataDoc.links;
|
|
658
|
+
}
|
|
659
|
+
if ('meta' in dataDoc) {
|
|
660
|
+
newData.meta = dataDoc.meta;
|
|
661
|
+
}
|
|
662
|
+
assertPrivateStore(store);
|
|
663
|
+
store._join(() => {
|
|
664
|
+
this.graph.push({
|
|
665
|
+
op: 'updateRelationship',
|
|
666
|
+
record: identifier,
|
|
667
|
+
field: this.key,
|
|
668
|
+
value: newData
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
if (!skipFetch) return this.load();
|
|
672
|
+
}
|
|
673
|
+
_isLoaded() {
|
|
674
|
+
const hasRelationshipDataProperty = this.hasManyRelationship.state.hasReceivedData;
|
|
675
|
+
if (!hasRelationshipDataProperty) {
|
|
676
|
+
return false;
|
|
677
|
+
}
|
|
678
|
+
const relationship = this.graph.getData(this.hasManyRelationship.identifier, this.key);
|
|
679
|
+
return relationship.data ? relationship.data.every(identifier => {
|
|
680
|
+
assertPrivateStore(this.store);
|
|
681
|
+
return this.store._instanceCache.recordIsLoaded(identifier, true) === true;
|
|
682
|
+
}) : false;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
`value()` synchronously returns the current value of the has-many
|
|
687
|
+
relationship. Unlike `record.relationshipName`, calling
|
|
688
|
+
`value()` on a reference does not trigger a fetch if the async
|
|
689
|
+
relationship is not yet loaded. If the relationship is not loaded
|
|
690
|
+
it will always return `null`.
|
|
691
|
+
Example
|
|
692
|
+
```js [app/models/post.js]
|
|
693
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
694
|
+
export default class PostModel extends Model {
|
|
695
|
+
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
696
|
+
}
|
|
697
|
+
```
|
|
698
|
+
```javascript
|
|
699
|
+
let post = store.push({
|
|
700
|
+
data: {
|
|
701
|
+
type: 'post',
|
|
702
|
+
id: 1,
|
|
703
|
+
relationships: {
|
|
704
|
+
comments: {
|
|
705
|
+
data: [{ type: 'comment', id: 1 }]
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
let commentsRef = post.hasMany('comments');
|
|
711
|
+
post.comments.then(function(comments) {
|
|
712
|
+
commentsRef.value() === comments
|
|
713
|
+
})
|
|
714
|
+
```
|
|
715
|
+
@public
|
|
716
|
+
*/
|
|
717
|
+
value() {
|
|
718
|
+
const support = LEGACY_SUPPORT.get(this.___identifier);
|
|
719
|
+
if (!ensureRefCanSubscribe(this)) {
|
|
720
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
721
|
+
this._ref;
|
|
722
|
+
return null;
|
|
723
|
+
}
|
|
724
|
+
return support.getManyArray(this.key);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
Loads the relationship if it is not already loaded. If the
|
|
729
|
+
relationship is already loaded this method does not trigger a new
|
|
730
|
+
load. This causes a request to the specified
|
|
731
|
+
relationship link or reloads all items currently in the relationship.
|
|
732
|
+
Example
|
|
733
|
+
```js [app/models/post.js]
|
|
734
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
735
|
+
export default class PostModel extends Model {
|
|
736
|
+
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
737
|
+
}
|
|
738
|
+
```
|
|
739
|
+
```javascript
|
|
740
|
+
let post = store.push({
|
|
741
|
+
data: {
|
|
742
|
+
type: 'post',
|
|
743
|
+
id: 1,
|
|
744
|
+
relationships: {
|
|
745
|
+
comments: {
|
|
746
|
+
data: [{ type: 'comment', id: 1 }]
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
});
|
|
751
|
+
let commentsRef = post.hasMany('comments');
|
|
752
|
+
commentsRef.load().then(function(comments) {
|
|
753
|
+
//...
|
|
754
|
+
});
|
|
755
|
+
```
|
|
756
|
+
You may also pass in an options object whose properties will be
|
|
757
|
+
fed forward. This enables you to pass `adapterOptions` into the
|
|
758
|
+
request given to the adapter via the reference.
|
|
759
|
+
Example
|
|
760
|
+
```javascript
|
|
761
|
+
commentsRef.load({ adapterOptions: { isPrivate: true } })
|
|
762
|
+
.then(function(comments) {
|
|
763
|
+
//...
|
|
764
|
+
});
|
|
765
|
+
```
|
|
766
|
+
```js [app/adapters/comment.js]
|
|
767
|
+
export default ApplicationAdapter.extend({
|
|
768
|
+
findMany(store, type, id, snapshots) {
|
|
769
|
+
// In the adapter you will have access to adapterOptions.
|
|
770
|
+
let adapterOptions = snapshots[0].adapterOptions;
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
```
|
|
774
|
+
@public
|
|
775
|
+
@param options the options to pass in.
|
|
776
|
+
@return a promise that resolves with the ManyArray in
|
|
777
|
+
this has-many relationship.
|
|
778
|
+
*/
|
|
779
|
+
async load(options) {
|
|
780
|
+
const support = LEGACY_SUPPORT.get(this.___identifier);
|
|
781
|
+
const fetchSyncRel = !this.hasManyRelationship.definition.isAsync && !areAllInverseRecordsLoaded(this.store, this._resource());
|
|
782
|
+
return fetchSyncRel ? support.reloadHasMany(this.key, options) :
|
|
783
|
+
// we cast to fix the return type since typescript and eslint don't understand async functions
|
|
784
|
+
// properly
|
|
785
|
+
support.getHasMany(this.key, options);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
Reloads this has-many relationship. This causes a request to the specified
|
|
790
|
+
relationship link or reloads all items currently in the relationship.
|
|
791
|
+
Example
|
|
792
|
+
```js [app/models/post.js]
|
|
793
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
794
|
+
export default class PostModel extends Model {
|
|
795
|
+
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
796
|
+
}
|
|
797
|
+
```
|
|
798
|
+
```javascript
|
|
799
|
+
let post = store.push({
|
|
800
|
+
data: {
|
|
801
|
+
type: 'post',
|
|
802
|
+
id: 1,
|
|
803
|
+
relationships: {
|
|
804
|
+
comments: {
|
|
805
|
+
data: [{ type: 'comment', id: 1 }]
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
});
|
|
810
|
+
let commentsRef = post.hasMany('comments');
|
|
811
|
+
commentsRef.reload().then(function(comments) {
|
|
812
|
+
//...
|
|
813
|
+
});
|
|
814
|
+
```
|
|
815
|
+
You may also pass in an options object whose properties will be
|
|
816
|
+
fed forward. This enables you to pass `adapterOptions` into the
|
|
817
|
+
request given to the adapter via the reference. A full example
|
|
818
|
+
can be found in the `load` method.
|
|
819
|
+
Example
|
|
820
|
+
```javascript
|
|
821
|
+
commentsRef.reload({ adapterOptions: { isPrivate: true } })
|
|
822
|
+
```
|
|
823
|
+
@public
|
|
824
|
+
@param options the options to pass in.
|
|
825
|
+
@return a promise that resolves with the ManyArray in this has-many relationship.
|
|
826
|
+
*/
|
|
827
|
+
reload(options) {
|
|
828
|
+
const support = LEGACY_SUPPORT.get(this.___identifier);
|
|
829
|
+
return support.reloadHasMany(this.key, options);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
defineNonEnumerableSignal(HasManyReference.prototype, '_ref', 0);
|
|
833
|
+
function isMaybeResource(object) {
|
|
834
|
+
const keys = Object.keys(object).filter(k => k !== 'id' && k !== 'type' && k !== 'lid');
|
|
835
|
+
return keys.length > 0;
|
|
836
|
+
}
|
|
837
|
+
function ensureRefCanSubscribe(rel) {
|
|
838
|
+
const loaded = rel._isLoaded();
|
|
839
|
+
if (!loaded) {
|
|
840
|
+
// subscribe to changes
|
|
841
|
+
// for when we are not loaded yet
|
|
842
|
+
//
|
|
843
|
+
// because the graph optimizes the case where a relationship has never been subscribed,
|
|
844
|
+
// we force accessed to be true here. When we make the graph public we should create a
|
|
845
|
+
// subscribe/unsubscribe API
|
|
846
|
+
const edge = rel.graph.get(rel.___identifier, rel.key);
|
|
847
|
+
edge.accessed = true;
|
|
848
|
+
return false;
|
|
849
|
+
}
|
|
850
|
+
return true;
|
|
851
|
+
}
|
|
852
|
+
function isResourceIdentiferWithRelatedLinks(value) {
|
|
853
|
+
return Boolean(value && value.links && value.links.related);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
A `BelongsToReference` is a low-level API that allows access
|
|
858
|
+
and manipulation of a belongsTo relationship.
|
|
859
|
+
|
|
860
|
+
It is especially useful when you're dealing with `async` relationships
|
|
861
|
+
from `@warp-drive/legacy/model` as it allows synchronous access to
|
|
862
|
+
the relationship data if loaded, as well as APIs for loading, reloading
|
|
863
|
+
the data or accessing available information without triggering a load.
|
|
864
|
+
|
|
865
|
+
It may also be useful when using `sync` relationships with `@warp-drive/legacy/model`
|
|
866
|
+
that need to be loaded/reloaded with more precise timing than marking the
|
|
867
|
+
relationship as `async` and relying on autofetch would have allowed.
|
|
868
|
+
|
|
869
|
+
However,keep in mind that marking a relationship as `async: false` will introduce
|
|
870
|
+
bugs into your application if the data is not always guaranteed to be available
|
|
871
|
+
by the time the relationship is accessed. Ergo, it is recommended when using this
|
|
872
|
+
approach to utilize `links` for unloaded relationship state instead of identifiers.
|
|
873
|
+
|
|
874
|
+
Reference APIs are entangled with the relationship's underlying state,
|
|
875
|
+
thus any getters or cached properties that utilize these will properly
|
|
876
|
+
invalidate if the relationship state changes.
|
|
877
|
+
|
|
878
|
+
References are "stable", meaning that multiple calls to retrieve the reference
|
|
879
|
+
for a given relationship will always return the same HasManyReference.
|
|
880
|
+
|
|
881
|
+
@public
|
|
882
|
+
*/
|
|
883
|
+
class BelongsToReference {
|
|
884
|
+
/**
|
|
885
|
+
* The field name on the parent record for this has-many relationship.
|
|
886
|
+
*
|
|
887
|
+
*/
|
|
888
|
+
|
|
889
|
+
/**
|
|
890
|
+
* The type of resource this relationship will contain.
|
|
891
|
+
*
|
|
892
|
+
*/
|
|
893
|
+
|
|
894
|
+
// unsubscribe tokens given to us by the notification manager
|
|
895
|
+
|
|
896
|
+
constructor(store, graph, parentIdentifier, belongsToRelationship, key) {
|
|
897
|
+
this.graph = graph;
|
|
898
|
+
this.key = key;
|
|
899
|
+
this.belongsToRelationship = belongsToRelationship;
|
|
900
|
+
this.type = belongsToRelationship.definition.type;
|
|
901
|
+
this.store = store;
|
|
902
|
+
this.___identifier = parentIdentifier;
|
|
903
|
+
this.___relatedToken = null;
|
|
904
|
+
this.___token = store.notifications.subscribe(parentIdentifier, (_, bucket, notifiedKey) => {
|
|
905
|
+
if (bucket === 'relationships' && notifiedKey === key) {
|
|
906
|
+
this._ref++;
|
|
907
|
+
}
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
// TODO inverse
|
|
911
|
+
}
|
|
912
|
+
destroy() {
|
|
913
|
+
// TODO @feature we need the notification manager often enough
|
|
914
|
+
// we should potentially just expose it fully public
|
|
915
|
+
this.store.notifications.unsubscribe(this.___token);
|
|
916
|
+
this.___token = null;
|
|
917
|
+
if (this.___relatedToken) {
|
|
918
|
+
this.store.notifications.unsubscribe(this.___relatedToken);
|
|
919
|
+
this.___relatedToken = null;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* The identifier of the record that this reference refers to.
|
|
925
|
+
* `null` if no related record is known.
|
|
926
|
+
*
|
|
927
|
+
*/
|
|
928
|
+
get identifier() {
|
|
929
|
+
if (this.___relatedToken) {
|
|
930
|
+
this.store.notifications.unsubscribe(this.___relatedToken);
|
|
931
|
+
this.___relatedToken = null;
|
|
932
|
+
}
|
|
933
|
+
const resource = this._resource();
|
|
934
|
+
if (resource && resource.data) {
|
|
935
|
+
const identifier = this.store.cacheKeyManager.getOrCreateRecordIdentifier(resource.data);
|
|
936
|
+
this.___relatedToken = this.store.notifications.subscribe(identifier, (_, bucket, notifiedKey) => {
|
|
937
|
+
if (bucket === 'identity' || bucket === 'attributes' && notifiedKey === 'id') {
|
|
938
|
+
this._ref++;
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
return identifier;
|
|
942
|
+
}
|
|
943
|
+
return null;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
The `id` of the record that this reference refers to. Together, the
|
|
948
|
+
`type()` and `id()` methods form a composite key for the identity
|
|
949
|
+
map. This can be used to access the id of an async relationship
|
|
950
|
+
without triggering a fetch that would normally happen if you
|
|
951
|
+
attempted to use `record.relationship.id`.
|
|
952
|
+
Example
|
|
953
|
+
```javascript
|
|
954
|
+
// models/blog.js
|
|
955
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
956
|
+
export default class BlogModel extends Model {
|
|
957
|
+
@belongsTo('user', { async: true, inverse: null }) user;
|
|
958
|
+
}
|
|
959
|
+
let blog = store.push({
|
|
960
|
+
data: {
|
|
961
|
+
type: 'blog',
|
|
962
|
+
id: 1,
|
|
963
|
+
relationships: {
|
|
964
|
+
user: {
|
|
965
|
+
data: { type: 'user', id: 1 }
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
let userRef = blog.belongsTo('user');
|
|
971
|
+
// get the identifier of the reference
|
|
972
|
+
if (userRef.remoteType() === "id") {
|
|
973
|
+
let id = userRef.id();
|
|
974
|
+
}
|
|
975
|
+
```
|
|
976
|
+
@public
|
|
977
|
+
@return The id of the record in this belongsTo relationship.
|
|
978
|
+
*/
|
|
979
|
+
static {
|
|
980
|
+
decorateMethodV2(this.prototype, "identifier", [memoized]);
|
|
981
|
+
}
|
|
982
|
+
id() {
|
|
983
|
+
return this.identifier?.id || null;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/**
|
|
987
|
+
The link WarpDrive will use to fetch or reload this belongs-to
|
|
988
|
+
relationship. By default it uses only the "related" resource linkage.
|
|
989
|
+
Example
|
|
990
|
+
```javascript
|
|
991
|
+
// models/blog.js
|
|
992
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
993
|
+
export default Model.extend({
|
|
994
|
+
user: belongsTo('user', { async: true, inverse: null })
|
|
995
|
+
});
|
|
996
|
+
let blog = store.push({
|
|
997
|
+
data: {
|
|
998
|
+
type: 'blog',
|
|
999
|
+
id: 1,
|
|
1000
|
+
relationships: {
|
|
1001
|
+
user: {
|
|
1002
|
+
links: {
|
|
1003
|
+
related: '/articles/1/author'
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
});
|
|
1009
|
+
let userRef = blog.belongsTo('user');
|
|
1010
|
+
// get the identifier of the reference
|
|
1011
|
+
if (userRef.remoteType() === "link") {
|
|
1012
|
+
let link = userRef.link();
|
|
1013
|
+
}
|
|
1014
|
+
```
|
|
1015
|
+
@public
|
|
1016
|
+
@return The link WarpDrive will use to fetch or reload this belongs-to relationship.
|
|
1017
|
+
*/
|
|
1018
|
+
link() {
|
|
1019
|
+
const resource = this._resource();
|
|
1020
|
+
if (isResourceIdentiferWithRelatedLinks(resource)) {
|
|
1021
|
+
if (resource.links) {
|
|
1022
|
+
const related = resource.links.related;
|
|
1023
|
+
return !related || typeof related === 'string' ? related : related.href;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
return null;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* any links that have been received for this relationship
|
|
1031
|
+
*
|
|
1032
|
+
* @public
|
|
1033
|
+
* @return
|
|
1034
|
+
*/
|
|
1035
|
+
links() {
|
|
1036
|
+
const resource = this._resource();
|
|
1037
|
+
return resource && resource.links ? resource.links : null;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/**
|
|
1041
|
+
The meta data for the belongs-to relationship.
|
|
1042
|
+
Example
|
|
1043
|
+
```javascript
|
|
1044
|
+
// models/blog.js
|
|
1045
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
1046
|
+
export default Model.extend({
|
|
1047
|
+
user: belongsTo('user', { async: true, inverse: null })
|
|
1048
|
+
});
|
|
1049
|
+
let blog = store.push({
|
|
1050
|
+
data: {
|
|
1051
|
+
type: 'blog',
|
|
1052
|
+
id: 1,
|
|
1053
|
+
relationships: {
|
|
1054
|
+
user: {
|
|
1055
|
+
links: {
|
|
1056
|
+
related: {
|
|
1057
|
+
href: '/articles/1/author'
|
|
1058
|
+
},
|
|
1059
|
+
},
|
|
1060
|
+
meta: {
|
|
1061
|
+
lastUpdated: 1458014400000
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
});
|
|
1067
|
+
let userRef = blog.belongsTo('user');
|
|
1068
|
+
userRef.meta() // { lastUpdated: 1458014400000 }
|
|
1069
|
+
```
|
|
1070
|
+
@public
|
|
1071
|
+
@return The meta information for the belongs-to relationship.
|
|
1072
|
+
*/
|
|
1073
|
+
meta() {
|
|
1074
|
+
let meta = null;
|
|
1075
|
+
const resource = this._resource();
|
|
1076
|
+
if (resource && resource.meta && typeof resource.meta === 'object') {
|
|
1077
|
+
meta = resource.meta;
|
|
1078
|
+
}
|
|
1079
|
+
return meta;
|
|
1080
|
+
}
|
|
1081
|
+
_resource() {
|
|
1082
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
1083
|
+
this._ref; // subscribe
|
|
1084
|
+
const cache = this.store.cache;
|
|
1085
|
+
return cache.getRelationship(this.___identifier, this.key);
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
/**
|
|
1089
|
+
This returns a string that represents how the reference will be
|
|
1090
|
+
looked up when it is loaded. If the relationship has a link it will
|
|
1091
|
+
use the "link" otherwise it defaults to "id".
|
|
1092
|
+
Example
|
|
1093
|
+
```js [app/models/post.js]
|
|
1094
|
+
import Model, { hasMany } from '@warp-drive/legacy/model';
|
|
1095
|
+
export default class PostModel extends Model {
|
|
1096
|
+
@hasMany('comment', { async: true, inverse: null }) comments;
|
|
1097
|
+
}
|
|
1098
|
+
```
|
|
1099
|
+
```javascript
|
|
1100
|
+
let post = store.push({
|
|
1101
|
+
data: {
|
|
1102
|
+
type: 'post',
|
|
1103
|
+
id: 1,
|
|
1104
|
+
relationships: {
|
|
1105
|
+
comments: {
|
|
1106
|
+
data: [{ type: 'comment', id: 1 }]
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
let commentsRef = post.hasMany('comments');
|
|
1112
|
+
// get the identifier of the reference
|
|
1113
|
+
if (commentsRef.remoteType() === "ids") {
|
|
1114
|
+
let ids = commentsRef.ids();
|
|
1115
|
+
} else if (commentsRef.remoteType() === "link") {
|
|
1116
|
+
let link = commentsRef.link();
|
|
1117
|
+
}
|
|
1118
|
+
```
|
|
1119
|
+
@public
|
|
1120
|
+
@return The name of the remote type. This should either be `link` or `id`
|
|
1121
|
+
*/
|
|
1122
|
+
remoteType() {
|
|
1123
|
+
const value = this._resource();
|
|
1124
|
+
if (isResourceIdentiferWithRelatedLinks(value)) {
|
|
1125
|
+
return 'link';
|
|
1126
|
+
}
|
|
1127
|
+
return 'id';
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
`push` can be used to update the data in the relationship and WarpDrive
|
|
1132
|
+
will treat the new data as the canonical value of this relationship on
|
|
1133
|
+
the backend. A value of `null` (e.g. `{ data: null }`) can be passed to
|
|
1134
|
+
clear the relationship.
|
|
1135
|
+
Example model
|
|
1136
|
+
```js [app/models/blog.js]
|
|
1137
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
1138
|
+
export default class BlogModel extends Model {
|
|
1139
|
+
@belongsTo('user', { async: true, inverse: null }) user;
|
|
1140
|
+
}
|
|
1141
|
+
```
|
|
1142
|
+
Setup some initial state, note we haven't loaded the user yet:
|
|
1143
|
+
```js
|
|
1144
|
+
const blog = store.push({
|
|
1145
|
+
data: {
|
|
1146
|
+
type: 'blog',
|
|
1147
|
+
id: '1',
|
|
1148
|
+
relationships: {
|
|
1149
|
+
user: {
|
|
1150
|
+
data: { type: 'user', id: '1' }
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
});
|
|
1155
|
+
const userRef = blog.belongsTo('user');
|
|
1156
|
+
userRef.id(); // '1'
|
|
1157
|
+
```
|
|
1158
|
+
Update the state using `push`, note we can do this even without
|
|
1159
|
+
having loaded the user yet by providing a resource-identifier.
|
|
1160
|
+
Both full a resource and a resource-identifier are supported.
|
|
1161
|
+
```js
|
|
1162
|
+
await userRef.push({
|
|
1163
|
+
data: {
|
|
1164
|
+
type: 'user',
|
|
1165
|
+
id: '2',
|
|
1166
|
+
}
|
|
1167
|
+
});
|
|
1168
|
+
userRef.id(); // '2'
|
|
1169
|
+
```
|
|
1170
|
+
You may also pass in links and meta fore the relationship, and sideload
|
|
1171
|
+
additional resources that might be required.
|
|
1172
|
+
```js
|
|
1173
|
+
await userRef.push({
|
|
1174
|
+
data: {
|
|
1175
|
+
type: 'user',
|
|
1176
|
+
id: '2',
|
|
1177
|
+
},
|
|
1178
|
+
links: {
|
|
1179
|
+
related: '/articles/1/author'
|
|
1180
|
+
},
|
|
1181
|
+
meta: {
|
|
1182
|
+
lastUpdated: Date.now()
|
|
1183
|
+
},
|
|
1184
|
+
included: [
|
|
1185
|
+
{
|
|
1186
|
+
type: 'user-preview',
|
|
1187
|
+
id: '2',
|
|
1188
|
+
attributes: {
|
|
1189
|
+
username: '@runspired'
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
]
|
|
1193
|
+
});
|
|
1194
|
+
```
|
|
1195
|
+
By default, the store will attempt to fetch the record if it is not loaded or its
|
|
1196
|
+
resource data is not included in the call to `push` before resolving the returned
|
|
1197
|
+
promise with the new state..
|
|
1198
|
+
Alternatively, pass `true` as the second argument to avoid fetching unloaded records
|
|
1199
|
+
and instead the promise will resolve with void without attempting to fetch. This is
|
|
1200
|
+
particularly useful if you want to update the state of the relationship without
|
|
1201
|
+
forcing the load of all of the associated record.
|
|
1202
|
+
@public
|
|
1203
|
+
@param doc a JSONAPI document object describing the new value of this relationship.
|
|
1204
|
+
@param skipFetch [optional] if `true`, do not attempt to fetch unloaded records
|
|
1205
|
+
@return a promise that resolves with the record in this belongs-to relationship after the push has completed.
|
|
1206
|
+
*/
|
|
1207
|
+
async push(doc, skipFetch) {
|
|
1208
|
+
const {
|
|
1209
|
+
store
|
|
1210
|
+
} = this;
|
|
1211
|
+
const isResourceData = doc.data && isMaybeResource(doc.data);
|
|
1212
|
+
const added = isResourceData ? store._push(doc, true) : doc.data ? store.cacheKeyManager.getOrCreateRecordIdentifier(doc.data) : null;
|
|
1213
|
+
const {
|
|
1214
|
+
identifier
|
|
1215
|
+
} = this.belongsToRelationship;
|
|
1216
|
+
const newData = {};
|
|
1217
|
+
|
|
1218
|
+
// only set data if it was passed in
|
|
1219
|
+
if (doc.data || doc.data === null) {
|
|
1220
|
+
newData.data = added;
|
|
1221
|
+
}
|
|
1222
|
+
if ('links' in doc) {
|
|
1223
|
+
newData.links = doc.links;
|
|
1224
|
+
}
|
|
1225
|
+
if ('meta' in doc) {
|
|
1226
|
+
newData.meta = doc.meta;
|
|
1227
|
+
}
|
|
1228
|
+
assertPrivateStore(store);
|
|
1229
|
+
store._join(() => {
|
|
1230
|
+
this.graph.push({
|
|
1231
|
+
op: 'updateRelationship',
|
|
1232
|
+
record: identifier,
|
|
1233
|
+
field: this.key,
|
|
1234
|
+
value: newData
|
|
1235
|
+
});
|
|
1236
|
+
});
|
|
1237
|
+
if (!skipFetch) return this.load();
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
`value()` synchronously returns the current value of the belongs-to
|
|
1242
|
+
relationship. Unlike `record.relationshipName`, calling
|
|
1243
|
+
`value()` on a reference does not trigger a fetch if the async
|
|
1244
|
+
relationship is not yet loaded. If the relationship is not loaded
|
|
1245
|
+
it will always return `null`.
|
|
1246
|
+
Example
|
|
1247
|
+
```javascript
|
|
1248
|
+
// models/blog.js
|
|
1249
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
1250
|
+
export default class BlogModel extends Model {
|
|
1251
|
+
@belongsTo('user', { async: true, inverse: null }) user;
|
|
1252
|
+
}
|
|
1253
|
+
let blog = store.push({
|
|
1254
|
+
data: {
|
|
1255
|
+
type: 'blog',
|
|
1256
|
+
id: 1,
|
|
1257
|
+
relationships: {
|
|
1258
|
+
user: {
|
|
1259
|
+
data: { type: 'user', id: 1 }
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
});
|
|
1264
|
+
let userRef = blog.belongsTo('user');
|
|
1265
|
+
userRef.value(); // null
|
|
1266
|
+
// provide data for reference
|
|
1267
|
+
userRef.push({
|
|
1268
|
+
data: {
|
|
1269
|
+
type: 'user',
|
|
1270
|
+
id: 1,
|
|
1271
|
+
attributes: {
|
|
1272
|
+
username: "@user"
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
}).then(function(user) {
|
|
1276
|
+
userRef.value(); // user
|
|
1277
|
+
});
|
|
1278
|
+
```
|
|
1279
|
+
@public
|
|
1280
|
+
@return the record in this relationship
|
|
1281
|
+
*/
|
|
1282
|
+
value() {
|
|
1283
|
+
const resource = this._resource();
|
|
1284
|
+
return resource && resource.data ? this.store.peekRecord(resource.data) : null;
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
/**
|
|
1288
|
+
Loads a record in a belongs-to relationship if it is not already
|
|
1289
|
+
loaded. If the relationship is already loaded this method does not
|
|
1290
|
+
trigger a new load.
|
|
1291
|
+
Example
|
|
1292
|
+
```javascript
|
|
1293
|
+
// models/blog.js
|
|
1294
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
1295
|
+
export default class BlogModel extends Model {
|
|
1296
|
+
@belongsTo('user', { async: true, inverse: null }) user;
|
|
1297
|
+
}
|
|
1298
|
+
let blog = store.push({
|
|
1299
|
+
data: {
|
|
1300
|
+
type: 'blog',
|
|
1301
|
+
id: 1,
|
|
1302
|
+
relationships: {
|
|
1303
|
+
user: {
|
|
1304
|
+
data: { type: 'user', id: 1 }
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
let userRef = blog.belongsTo('user');
|
|
1310
|
+
userRef.value(); // null
|
|
1311
|
+
userRef.load().then(function(user) {
|
|
1312
|
+
userRef.value() === user
|
|
1313
|
+
});
|
|
1314
|
+
```
|
|
1315
|
+
You may also pass in an options object whose properties will be
|
|
1316
|
+
fed forward. This enables you to pass `adapterOptions` into the
|
|
1317
|
+
request given to the adapter via the reference.
|
|
1318
|
+
Example
|
|
1319
|
+
```javascript
|
|
1320
|
+
userRef.load({ adapterOptions: { isPrivate: true } }).then(function(user) {
|
|
1321
|
+
userRef.value() === user;
|
|
1322
|
+
});
|
|
1323
|
+
```
|
|
1324
|
+
```js [app/adapters/user.js]
|
|
1325
|
+
import Adapter from '@warp-drive/legacy/adapter';
|
|
1326
|
+
export default class UserAdapter extends Adapter {
|
|
1327
|
+
findRecord(store, type, id, snapshot) {
|
|
1328
|
+
// In the adapter you will have access to adapterOptions.
|
|
1329
|
+
let adapterOptions = snapshot.adapterOptions;
|
|
1330
|
+
}
|
|
1331
|
+
});
|
|
1332
|
+
```
|
|
1333
|
+
@public
|
|
1334
|
+
@param options the options to pass in.
|
|
1335
|
+
@return a promise that resolves with the record in this belongs-to relationship.
|
|
1336
|
+
*/
|
|
1337
|
+
async load(options) {
|
|
1338
|
+
const support = LEGACY_SUPPORT.get(this.___identifier);
|
|
1339
|
+
const fetchSyncRel = !this.belongsToRelationship.definition.isAsync && !areAllInverseRecordsLoaded(this.store, this._resource());
|
|
1340
|
+
return fetchSyncRel ? support.reloadBelongsTo(this.key, options).then(() => this.value()) :
|
|
1341
|
+
// we cast to fix the return type since typescript and eslint don't understand async functions
|
|
1342
|
+
// properly
|
|
1343
|
+
support.getBelongsTo(this.key, options);
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
/**
|
|
1347
|
+
Triggers a reload of the value in this relationship. If the
|
|
1348
|
+
remoteType is `"link"` WarpDrive will use the relationship link to
|
|
1349
|
+
reload the relationship. Otherwise it will reload the record by its
|
|
1350
|
+
id.
|
|
1351
|
+
Example
|
|
1352
|
+
```javascript
|
|
1353
|
+
// models/blog.js
|
|
1354
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
1355
|
+
export default class BlogModel extends Model {
|
|
1356
|
+
@belongsTo('user', { async: true, inverse: null }) user;
|
|
1357
|
+
}
|
|
1358
|
+
let blog = store.push({
|
|
1359
|
+
data: {
|
|
1360
|
+
type: 'blog',
|
|
1361
|
+
id: 1,
|
|
1362
|
+
relationships: {
|
|
1363
|
+
user: {
|
|
1364
|
+
data: { type: 'user', id: 1 }
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
});
|
|
1369
|
+
let userRef = blog.belongsTo('user');
|
|
1370
|
+
userRef.reload().then(function(user) {
|
|
1371
|
+
userRef.value() === user
|
|
1372
|
+
});
|
|
1373
|
+
```
|
|
1374
|
+
You may also pass in an options object whose properties will be
|
|
1375
|
+
fed forward. This enables you to pass `adapterOptions` into the
|
|
1376
|
+
request given to the adapter via the reference. A full example
|
|
1377
|
+
can be found in the `load` method.
|
|
1378
|
+
Example
|
|
1379
|
+
```javascript
|
|
1380
|
+
userRef.reload({ adapterOptions: { isPrivate: true } })
|
|
1381
|
+
```
|
|
1382
|
+
@public
|
|
1383
|
+
@param options the options to pass in.
|
|
1384
|
+
@return a promise that resolves with the record in this belongs-to relationship after the reload has completed.
|
|
1385
|
+
*/
|
|
1386
|
+
reload(options) {
|
|
1387
|
+
const support = LEGACY_SUPPORT.get(this.___identifier);
|
|
1388
|
+
return support.reloadBelongsTo(this.key, options).then(() => this.value());
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
defineNonEnumerableSignal(BelongsToReference.prototype, '_ref', 0);
|
|
1392
|
+
const LEGACY_SUPPORT = getOrSetGlobal('LEGACY_SUPPORT', new Map());
|
|
1393
|
+
function lookupLegacySupport(record) {
|
|
1394
|
+
const identifier = recordIdentifierFor(record);
|
|
1395
|
+
let support = LEGACY_SUPPORT.get(identifier);
|
|
1396
|
+
if (!support) {
|
|
1397
|
+
support = new LegacySupport(record, identifier);
|
|
1398
|
+
LEGACY_SUPPORT.set(identifier, support);
|
|
1399
|
+
}
|
|
1400
|
+
return support;
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
/**
|
|
1404
|
+
* @hideconstructor
|
|
1405
|
+
*/
|
|
1406
|
+
class LegacySupport {
|
|
1407
|
+
/** @internal */
|
|
1408
|
+
|
|
1409
|
+
/** @internal */
|
|
1410
|
+
|
|
1411
|
+
/** @internal */
|
|
1412
|
+
|
|
1413
|
+
/** @internal */
|
|
1414
|
+
|
|
1415
|
+
/** @internal */
|
|
1416
|
+
|
|
1417
|
+
/** @internal */
|
|
1418
|
+
|
|
1419
|
+
/** @internal */
|
|
1420
|
+
|
|
1421
|
+
/** @internal */
|
|
1422
|
+
|
|
1423
|
+
/** @internal */
|
|
1424
|
+
|
|
1425
|
+
/** @internal */
|
|
1426
|
+
|
|
1427
|
+
/** @internal */
|
|
1428
|
+
|
|
1429
|
+
constructor(record, identifier) {
|
|
1430
|
+
this.record = record;
|
|
1431
|
+
this.store = isPrivateStore(storeFor(record, false));
|
|
1432
|
+
this.identifier = identifier;
|
|
1433
|
+
this.cache = this.store.cache;
|
|
1434
|
+
if (this.store._graph) {
|
|
1435
|
+
this.graph = this.store._graph;
|
|
1436
|
+
}
|
|
1437
|
+
this._manyArrayCache = Object.create(null);
|
|
1438
|
+
this._relationshipPromisesCache = Object.create(null);
|
|
1439
|
+
this._relationshipProxyCache = Object.create(null);
|
|
1440
|
+
this._pending = Object.create(null);
|
|
1441
|
+
this.references = Object.create(null);
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
/** @private */
|
|
1445
|
+
_syncArray(array) {
|
|
1446
|
+
// It’s possible the parent side of the relationship may have been destroyed by this point
|
|
1447
|
+
if (this.isDestroyed || this.isDestroying) {
|
|
1448
|
+
return;
|
|
1449
|
+
}
|
|
1450
|
+
const currentState = array[Context].source;
|
|
1451
|
+
const identifier = this.identifier;
|
|
1452
|
+
const [identifiers, jsonApi] = this._getCurrentState(identifier, array.key);
|
|
1453
|
+
if (jsonApi.meta) {
|
|
1454
|
+
array.meta = jsonApi.meta;
|
|
1455
|
+
}
|
|
1456
|
+
if (jsonApi.links) {
|
|
1457
|
+
array.links = jsonApi.links;
|
|
1458
|
+
}
|
|
1459
|
+
currentState.length = 0;
|
|
1460
|
+
fastPush(currentState, identifiers);
|
|
1461
|
+
}
|
|
1462
|
+
mutate(mutation) {
|
|
1463
|
+
this.cache.mutate(mutation);
|
|
1464
|
+
}
|
|
1465
|
+
_findBelongsTo(key, resource, relationship, options) {
|
|
1466
|
+
const name = getRealFieldName(this, key);
|
|
1467
|
+
|
|
1468
|
+
// TODO @runspired follow up if parent isNew then we should not be attempting load here
|
|
1469
|
+
// TODO @runspired follow up on whether this should be in the relationship requests cache
|
|
1470
|
+
return this._findBelongsToByJsonApiResource(resource, this.identifier, relationship, options).then(identifier => handleCompletedRelationshipRequest(this, name, relationship, identifier), e => handleCompletedRelationshipRequest(this, name, relationship, null, e));
|
|
1471
|
+
}
|
|
1472
|
+
reloadBelongsTo(key, options) {
|
|
1473
|
+
const loadingPromise = this._relationshipPromisesCache[key];
|
|
1474
|
+
if (loadingPromise) {
|
|
1475
|
+
return loadingPromise;
|
|
1476
|
+
}
|
|
1477
|
+
const name = getRealFieldName(this, key);
|
|
1478
|
+
const resource = this.store.cache.getRelationship(this.identifier, name);
|
|
1479
|
+
const relationship = this.graph.get(this.identifier, name);
|
|
1480
|
+
relationship.state.hasFailedLoadAttempt = false;
|
|
1481
|
+
relationship.state.shouldForceReload = true;
|
|
1482
|
+
const promise = this._findBelongsTo(name, resource, relationship, options);
|
|
1483
|
+
if (this._relationshipProxyCache[name]) {
|
|
1484
|
+
// @ts-expect-error
|
|
1485
|
+
return this._updatePromiseProxyFor('belongsTo', name, {
|
|
1486
|
+
promise
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1489
|
+
return promise;
|
|
1490
|
+
}
|
|
1491
|
+
getBelongsTo(key, options) {
|
|
1492
|
+
const {
|
|
1493
|
+
identifier
|
|
1494
|
+
} = this;
|
|
1495
|
+
const name = getRealFieldName(this, key);
|
|
1496
|
+
const resource = this.store.cache.getRelationship(this.identifier, name);
|
|
1497
|
+
const relatedIdentifier = resource && resource.data ? resource.data : null;
|
|
1498
|
+
const store = this.store;
|
|
1499
|
+
const relationship = this.graph.get(this.identifier, name);
|
|
1500
|
+
const isAsync = relationship.definition.isAsync;
|
|
1501
|
+
const _belongsToState = {
|
|
1502
|
+
key,
|
|
1503
|
+
store,
|
|
1504
|
+
legacySupport: this,
|
|
1505
|
+
modelName: relationship.definition.type
|
|
1506
|
+
};
|
|
1507
|
+
if (isAsync) {
|
|
1508
|
+
if (relationship.state.hasFailedLoadAttempt) {
|
|
1509
|
+
return this._relationshipProxyCache[name];
|
|
1510
|
+
}
|
|
1511
|
+
const promise = this._findBelongsTo(name, resource, relationship, options);
|
|
1512
|
+
const isLoaded = relatedIdentifier && store._instanceCache.recordIsLoaded(relatedIdentifier);
|
|
1513
|
+
return this._updatePromiseProxyFor('belongsTo', name, {
|
|
1514
|
+
promise,
|
|
1515
|
+
content: isLoaded ? store._instanceCache.getRecord(relatedIdentifier) : null,
|
|
1516
|
+
_belongsToState
|
|
1517
|
+
});
|
|
1518
|
+
} else {
|
|
1519
|
+
if (relatedIdentifier === null) {
|
|
1520
|
+
return null;
|
|
1521
|
+
} else {
|
|
1522
|
+
return store._instanceCache.getRecord(relatedIdentifier);
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
setDirtyBelongsTo(key, value) {
|
|
1527
|
+
const name = getRealFieldName(this, key);
|
|
1528
|
+
return this.cache.mutate({
|
|
1529
|
+
op: 'replaceRelatedRecord',
|
|
1530
|
+
record: this.identifier,
|
|
1531
|
+
field: name,
|
|
1532
|
+
value: extractIdentifierFromRecord(value)
|
|
1533
|
+
},
|
|
1534
|
+
// @ts-expect-error
|
|
1535
|
+
true);
|
|
1536
|
+
}
|
|
1537
|
+
_getCurrentState(identifier, field) {
|
|
1538
|
+
const jsonApi = this.cache.getRelationship(identifier, field);
|
|
1539
|
+
const cache = this.store._instanceCache;
|
|
1540
|
+
const identifiers = [];
|
|
1541
|
+
if (jsonApi.data) {
|
|
1542
|
+
for (let i = 0; i < jsonApi.data.length; i++) {
|
|
1543
|
+
const relatedIdentifier = jsonApi.data[i];
|
|
1544
|
+
if (cache.recordIsLoaded(relatedIdentifier, true)) {
|
|
1545
|
+
identifiers.push(relatedIdentifier);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
return [identifiers, jsonApi];
|
|
1550
|
+
}
|
|
1551
|
+
getManyArray(key, definition) {
|
|
1552
|
+
const name = getRealFieldName(this, key);
|
|
1553
|
+
if (this.graph) {
|
|
1554
|
+
let manyArray = this._manyArrayCache[name];
|
|
1555
|
+
if (!definition) {
|
|
1556
|
+
definition = this.graph.get(this.identifier, name).definition;
|
|
1557
|
+
}
|
|
1558
|
+
if (!manyArray) {
|
|
1559
|
+
const [identifiers, doc] = this._getCurrentState(this.identifier, name);
|
|
1560
|
+
const field = getField(this, name);
|
|
1561
|
+
manyArray = createLegacyManyArray({
|
|
1562
|
+
store: this.store,
|
|
1563
|
+
// @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
|
|
1564
|
+
manager: this,
|
|
1565
|
+
source: identifiers,
|
|
1566
|
+
type: definition.type,
|
|
1567
|
+
isLoaded: !definition.isAsync,
|
|
1568
|
+
editable: true,
|
|
1569
|
+
isAsync: definition.isAsync,
|
|
1570
|
+
isPolymorphic: definition.isPolymorphic,
|
|
1571
|
+
field: field,
|
|
1572
|
+
identifier: this.identifier,
|
|
1573
|
+
links: doc.links || null,
|
|
1574
|
+
meta: doc.meta || null
|
|
1575
|
+
});
|
|
1576
|
+
this._manyArrayCache[name] = manyArray;
|
|
1577
|
+
}
|
|
1578
|
+
return manyArray;
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
fetchAsyncHasMany(key, relationship, manyArray, options) {
|
|
1582
|
+
const name = getRealFieldName(this, key);
|
|
1583
|
+
if (this.graph) {
|
|
1584
|
+
let loadingPromise = this._relationshipPromisesCache[name];
|
|
1585
|
+
if (loadingPromise) {
|
|
1586
|
+
return loadingPromise;
|
|
1587
|
+
}
|
|
1588
|
+
const jsonApi = this.cache.getRelationship(this.identifier, name);
|
|
1589
|
+
const promise = this._findHasManyByJsonApiResource(jsonApi, this.identifier, relationship, options);
|
|
1590
|
+
if (!promise) {
|
|
1591
|
+
manyArray.isLoaded = true;
|
|
1592
|
+
return Promise.resolve(manyArray);
|
|
1593
|
+
}
|
|
1594
|
+
loadingPromise = promise.then(() => handleCompletedRelationshipRequest(this, name, relationship, manyArray), e => handleCompletedRelationshipRequest(this, name, relationship, manyArray, e));
|
|
1595
|
+
this._relationshipPromisesCache[name] = loadingPromise;
|
|
1596
|
+
return loadingPromise;
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
reloadHasMany(key, options) {
|
|
1600
|
+
const name = getRealFieldName(this, key);
|
|
1601
|
+
if (this.graph) {
|
|
1602
|
+
const loadingPromise = this._relationshipPromisesCache[name];
|
|
1603
|
+
if (loadingPromise) {
|
|
1604
|
+
return loadingPromise;
|
|
1605
|
+
}
|
|
1606
|
+
const relationship = this.graph.get(this.identifier, name);
|
|
1607
|
+
const {
|
|
1608
|
+
definition,
|
|
1609
|
+
state
|
|
1610
|
+
} = relationship;
|
|
1611
|
+
state.hasFailedLoadAttempt = false;
|
|
1612
|
+
state.shouldForceReload = true;
|
|
1613
|
+
const manyArray = this.getManyArray(name, definition);
|
|
1614
|
+
const promise = this.fetchAsyncHasMany(name, relationship, manyArray, options);
|
|
1615
|
+
if (this._relationshipProxyCache[name]) {
|
|
1616
|
+
return this._updatePromiseProxyFor('hasMany', name, {
|
|
1617
|
+
promise
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
return promise;
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
getHasMany(key, options) {
|
|
1624
|
+
const name = getRealFieldName(this, key);
|
|
1625
|
+
if (this.graph) {
|
|
1626
|
+
const relationship = this.graph.get(this.identifier, name);
|
|
1627
|
+
const {
|
|
1628
|
+
definition,
|
|
1629
|
+
state
|
|
1630
|
+
} = relationship;
|
|
1631
|
+
const manyArray = this.getManyArray(name, definition);
|
|
1632
|
+
if (definition.isAsync) {
|
|
1633
|
+
if (state.hasFailedLoadAttempt) {
|
|
1634
|
+
return this._relationshipProxyCache[name];
|
|
1635
|
+
}
|
|
1636
|
+
const promise = this.fetchAsyncHasMany(name, relationship, manyArray, options);
|
|
1637
|
+
return this._updatePromiseProxyFor('hasMany', name, {
|
|
1638
|
+
promise,
|
|
1639
|
+
content: manyArray
|
|
1640
|
+
});
|
|
1641
|
+
} else {
|
|
1642
|
+
return manyArray;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
_updatePromiseProxyFor(kind, key, args) {
|
|
1647
|
+
let promiseProxy = this._relationshipProxyCache[key];
|
|
1648
|
+
if (kind === 'hasMany') {
|
|
1649
|
+
const {
|
|
1650
|
+
promise,
|
|
1651
|
+
content
|
|
1652
|
+
} = args;
|
|
1653
|
+
if (promiseProxy) {
|
|
1654
|
+
promiseProxy._update(promise, content);
|
|
1655
|
+
} else {
|
|
1656
|
+
promiseProxy = this._relationshipProxyCache[key] = new PromiseManyArray(promise, content);
|
|
1657
|
+
}
|
|
1658
|
+
return promiseProxy;
|
|
1659
|
+
}
|
|
1660
|
+
if (promiseProxy) {
|
|
1661
|
+
const {
|
|
1662
|
+
promise,
|
|
1663
|
+
content
|
|
1664
|
+
} = args;
|
|
1665
|
+
if (content !== undefined) {
|
|
1666
|
+
promiseProxy.set('content', content);
|
|
1667
|
+
}
|
|
1668
|
+
void promiseProxy.set('promise', promise);
|
|
1669
|
+
} else {
|
|
1670
|
+
promiseProxy = PromiseBelongsTo.create(args);
|
|
1671
|
+
this._relationshipProxyCache[key] = promiseProxy;
|
|
1672
|
+
}
|
|
1673
|
+
return promiseProxy;
|
|
1674
|
+
}
|
|
1675
|
+
referenceFor(kind, key) {
|
|
1676
|
+
const name = getRealFieldName(this, key);
|
|
1677
|
+
let reference = this.references[name];
|
|
1678
|
+
if (!reference) {
|
|
1679
|
+
if (!this.graph) ;
|
|
1680
|
+
const {
|
|
1681
|
+
graph,
|
|
1682
|
+
identifier
|
|
1683
|
+
} = this;
|
|
1684
|
+
const relationship = graph.get(identifier, name);
|
|
1685
|
+
const relationshipKind = relationship.definition.kind;
|
|
1686
|
+
if (relationshipKind === 'belongsTo') {
|
|
1687
|
+
reference = new BelongsToReference(this.store, graph, identifier, relationship, key);
|
|
1688
|
+
} else if (relationshipKind === 'hasMany') {
|
|
1689
|
+
reference = new HasManyReference(this.store, graph, identifier, relationship, key);
|
|
1690
|
+
}
|
|
1691
|
+
this.references[name] = reference;
|
|
1692
|
+
}
|
|
1693
|
+
return reference;
|
|
1694
|
+
}
|
|
1695
|
+
_findHasManyByJsonApiResource(resource, parentIdentifier, relationship, options = {}) {
|
|
1696
|
+
if (this.graph) {
|
|
1697
|
+
if (!resource) {
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
const {
|
|
1701
|
+
definition,
|
|
1702
|
+
state
|
|
1703
|
+
} = relationship;
|
|
1704
|
+
upgradeStore(this.store);
|
|
1705
|
+
const adapter = this.store.adapterFor?.(definition.type);
|
|
1706
|
+
const {
|
|
1707
|
+
isStale,
|
|
1708
|
+
hasDematerializedInverse,
|
|
1709
|
+
hasReceivedData,
|
|
1710
|
+
isEmpty,
|
|
1711
|
+
shouldForceReload
|
|
1712
|
+
} = state;
|
|
1713
|
+
const allInverseRecordsAreLoaded = areAllInverseRecordsLoaded(this.store, resource);
|
|
1714
|
+
const identifiers = resource.data;
|
|
1715
|
+
const shouldFindViaLink = resource.links && resource.links.related && (typeof adapter?.findHasMany === 'function' || typeof identifiers === 'undefined') && (shouldForceReload || hasDematerializedInverse || isStale || !allInverseRecordsAreLoaded && !isEmpty);
|
|
1716
|
+
const field = this.store.schema.fields({
|
|
1717
|
+
type: definition.inverseType
|
|
1718
|
+
}).get(definition.key);
|
|
1719
|
+
const request = {
|
|
1720
|
+
useLink: shouldFindViaLink,
|
|
1721
|
+
field,
|
|
1722
|
+
links: resource.links,
|
|
1723
|
+
meta: resource.meta,
|
|
1724
|
+
options,
|
|
1725
|
+
record: parentIdentifier
|
|
1726
|
+
};
|
|
1727
|
+
|
|
1728
|
+
// fetch via link
|
|
1729
|
+
if (shouldFindViaLink) {
|
|
1730
|
+
const req = field.options.linksMode ? {
|
|
1731
|
+
url: getRelatedLink(resource),
|
|
1732
|
+
op: 'findHasMany',
|
|
1733
|
+
method: 'GET',
|
|
1734
|
+
records: identifiers || [],
|
|
1735
|
+
data: request,
|
|
1736
|
+
[EnableHydration]: false
|
|
1737
|
+
} : {
|
|
1738
|
+
op: 'findHasMany',
|
|
1739
|
+
records: identifiers || [],
|
|
1740
|
+
data: request,
|
|
1741
|
+
cacheOptions: {
|
|
1742
|
+
[Symbol.for('wd:skip-cache')]: true
|
|
1743
|
+
}
|
|
1744
|
+
};
|
|
1745
|
+
return this.store.request(req);
|
|
1746
|
+
}
|
|
1747
|
+
const preferLocalCache = hasReceivedData && !isEmpty;
|
|
1748
|
+
const hasLocalPartialData = hasDematerializedInverse || isEmpty && Array.isArray(identifiers) && identifiers.length > 0;
|
|
1749
|
+
const attemptLocalCache = !shouldForceReload && !isStale && (preferLocalCache || hasLocalPartialData);
|
|
1750
|
+
if (attemptLocalCache && allInverseRecordsAreLoaded) {
|
|
1751
|
+
return;
|
|
1752
|
+
}
|
|
1753
|
+
const hasData = hasReceivedData && !isEmpty;
|
|
1754
|
+
if (attemptLocalCache || hasData || hasLocalPartialData) {
|
|
1755
|
+
options.reload = options.reload || !attemptLocalCache || undefined;
|
|
1756
|
+
return this.store.request({
|
|
1757
|
+
op: 'findHasMany',
|
|
1758
|
+
records: identifiers,
|
|
1759
|
+
data: request,
|
|
1760
|
+
cacheOptions: {
|
|
1761
|
+
[Symbol.for('wd:skip-cache')]: true
|
|
1762
|
+
}
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
// we were explicitly told we have no data and no links.
|
|
1767
|
+
// TODO if the relationshipIsStale, should we hit the adapter anyway?
|
|
1768
|
+
return;
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
_findBelongsToByJsonApiResource(resource, parentIdentifier, relationship, options = {}) {
|
|
1772
|
+
if (!resource) {
|
|
1773
|
+
return Promise.resolve(null);
|
|
1774
|
+
}
|
|
1775
|
+
const key = relationship.definition.key;
|
|
1776
|
+
const name = getRealFieldName(this, key);
|
|
1777
|
+
|
|
1778
|
+
// interleaved promises mean that we MUST cache this here
|
|
1779
|
+
// in order to prevent infinite re-render if the request
|
|
1780
|
+
// fails.
|
|
1781
|
+
if (this._pending[name]) {
|
|
1782
|
+
return this._pending[name];
|
|
1783
|
+
}
|
|
1784
|
+
const identifier = resource.data ? resource.data : null;
|
|
1785
|
+
const {
|
|
1786
|
+
isStale,
|
|
1787
|
+
hasDematerializedInverse,
|
|
1788
|
+
hasReceivedData,
|
|
1789
|
+
isEmpty,
|
|
1790
|
+
shouldForceReload
|
|
1791
|
+
} = relationship.state;
|
|
1792
|
+
const allInverseRecordsAreLoaded = areAllInverseRecordsLoaded(this.store, resource);
|
|
1793
|
+
const shouldFindViaLink = resource.links?.related && (shouldForceReload || hasDematerializedInverse || isStale || !allInverseRecordsAreLoaded && !isEmpty);
|
|
1794
|
+
const field = this.store.schema.fields(this.identifier).get(relationship.definition.key);
|
|
1795
|
+
const request = {
|
|
1796
|
+
useLink: shouldFindViaLink,
|
|
1797
|
+
field,
|
|
1798
|
+
links: resource.links,
|
|
1799
|
+
meta: resource.meta,
|
|
1800
|
+
options,
|
|
1801
|
+
record: parentIdentifier
|
|
1802
|
+
};
|
|
1803
|
+
|
|
1804
|
+
// fetch via link
|
|
1805
|
+
if (shouldFindViaLink) {
|
|
1806
|
+
const req = field.options.linksMode ? {
|
|
1807
|
+
url: getRelatedLink(resource),
|
|
1808
|
+
op: 'findBelongsTo',
|
|
1809
|
+
method: 'GET',
|
|
1810
|
+
records: identifier ? [identifier] : [],
|
|
1811
|
+
data: request,
|
|
1812
|
+
[EnableHydration]: false
|
|
1813
|
+
} : {
|
|
1814
|
+
op: 'findBelongsTo',
|
|
1815
|
+
records: identifier ? [identifier] : [],
|
|
1816
|
+
data: request,
|
|
1817
|
+
cacheOptions: {
|
|
1818
|
+
[Symbol.for('wd:skip-cache')]: true
|
|
1819
|
+
}
|
|
1820
|
+
};
|
|
1821
|
+
const future = this.store.request(req);
|
|
1822
|
+
this._pending[name] = future.then(doc => field.options.linksMode ? doc.content.data : doc.content).finally(() => {
|
|
1823
|
+
this._pending[name] = undefined;
|
|
1824
|
+
});
|
|
1825
|
+
return this._pending[name];
|
|
1826
|
+
}
|
|
1827
|
+
const preferLocalCache = hasReceivedData && allInverseRecordsAreLoaded && !isEmpty;
|
|
1828
|
+
const hasLocalPartialData = hasDematerializedInverse || isEmpty && resource.data;
|
|
1829
|
+
// null is explicit empty, undefined is "we don't know anything"
|
|
1830
|
+
const localDataIsEmpty = !identifier;
|
|
1831
|
+
const attemptLocalCache = !shouldForceReload && !isStale && (preferLocalCache || hasLocalPartialData);
|
|
1832
|
+
|
|
1833
|
+
// we dont need to fetch and are empty
|
|
1834
|
+
if (attemptLocalCache && localDataIsEmpty) {
|
|
1835
|
+
return Promise.resolve(null);
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
// we dont need to fetch because we are local state
|
|
1839
|
+
const resourceIsLocal = identifier?.id === null;
|
|
1840
|
+
if (attemptLocalCache && allInverseRecordsAreLoaded || resourceIsLocal) {
|
|
1841
|
+
return Promise.resolve(identifier);
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
// we may need to fetch
|
|
1845
|
+
if (identifier) {
|
|
1846
|
+
options.reload = options.reload || !attemptLocalCache || undefined;
|
|
1847
|
+
this._pending[name] = this.store.request({
|
|
1848
|
+
op: 'findBelongsTo',
|
|
1849
|
+
records: [identifier],
|
|
1850
|
+
data: request,
|
|
1851
|
+
cacheOptions: {
|
|
1852
|
+
[Symbol.for('wd:skip-cache')]: true
|
|
1853
|
+
}
|
|
1854
|
+
}).then(doc => doc.content).finally(() => {
|
|
1855
|
+
this._pending[name] = undefined;
|
|
1856
|
+
});
|
|
1857
|
+
return this._pending[name];
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
// we were explicitly told we have no data and no links.
|
|
1861
|
+
// TODO if the relationshipIsStale, should we hit the adapter anyway?
|
|
1862
|
+
return Promise.resolve(null);
|
|
1863
|
+
}
|
|
1864
|
+
destroy() {
|
|
1865
|
+
this.isDestroying = true;
|
|
1866
|
+
let cache = this._manyArrayCache;
|
|
1867
|
+
this._manyArrayCache = Object.create(null);
|
|
1868
|
+
Object.keys(cache).forEach(key => {
|
|
1869
|
+
cache[key].destroy();
|
|
1870
|
+
});
|
|
1871
|
+
cache = this._relationshipProxyCache;
|
|
1872
|
+
this._relationshipProxyCache = Object.create(null);
|
|
1873
|
+
Object.keys(cache).forEach(key => {
|
|
1874
|
+
const proxy = cache[key];
|
|
1875
|
+
if (proxy.destroy) {
|
|
1876
|
+
proxy.destroy();
|
|
1877
|
+
}
|
|
1878
|
+
});
|
|
1879
|
+
cache = this.references;
|
|
1880
|
+
this.references = Object.create(null);
|
|
1881
|
+
Object.keys(cache).forEach(key => {
|
|
1882
|
+
cache[key].destroy();
|
|
1883
|
+
});
|
|
1884
|
+
this.isDestroyed = true;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
function getRelatedLink(resource) {
|
|
1888
|
+
const related = resource.links?.related;
|
|
1889
|
+
return typeof related === 'object' ? related.href : related;
|
|
1890
|
+
}
|
|
1891
|
+
function handleCompletedRelationshipRequest(recordExt, key, relationship, value, error) {
|
|
1892
|
+
delete recordExt._relationshipPromisesCache[key];
|
|
1893
|
+
relationship.state.shouldForceReload = false;
|
|
1894
|
+
const isHasMany = relationship.definition.kind === 'hasMany';
|
|
1895
|
+
if (isHasMany) {
|
|
1896
|
+
// we don't notify the record property here to avoid refetch
|
|
1897
|
+
// only the many array
|
|
1898
|
+
notifyInternalSignal(value[Context].signal);
|
|
1899
|
+
}
|
|
1900
|
+
if (error) {
|
|
1901
|
+
relationship.state.hasFailedLoadAttempt = true;
|
|
1902
|
+
const proxy = recordExt._relationshipProxyCache[key];
|
|
1903
|
+
// belongsTo relationships are sometimes unloaded
|
|
1904
|
+
// when a load fails, in this case we need
|
|
1905
|
+
// to make sure that we aren't proxying
|
|
1906
|
+
// to destroyed content
|
|
1907
|
+
// for the sync belongsTo reload case there will be no proxy
|
|
1908
|
+
// for the async reload case there will be no proxy if the ui
|
|
1909
|
+
// has never been accessed
|
|
1910
|
+
if (proxy && !isHasMany) {
|
|
1911
|
+
// @ts-expect-error unsure why this is not resolving the boolean but async belongsTo is weird
|
|
1912
|
+
if (proxy.content && proxy.content.isDestroying) {
|
|
1913
|
+
proxy.set('content', null);
|
|
1914
|
+
}
|
|
1915
|
+
recordExt.store.notifications._flush();
|
|
1916
|
+
}
|
|
1917
|
+
throw error;
|
|
1918
|
+
}
|
|
1919
|
+
if (isHasMany) {
|
|
1920
|
+
value.isLoaded = true;
|
|
1921
|
+
} else {
|
|
1922
|
+
recordExt.store.notifications._flush();
|
|
1923
|
+
}
|
|
1924
|
+
relationship.state.hasFailedLoadAttempt = false;
|
|
1925
|
+
// only set to not stale if no error is thrown
|
|
1926
|
+
relationship.state.isStale = false;
|
|
1927
|
+
return isHasMany || !value ? value : recordExt.store.peekRecord(value);
|
|
1928
|
+
}
|
|
1929
|
+
function extractIdentifierFromRecord(record) {
|
|
1930
|
+
if (!record) {
|
|
1931
|
+
return null;
|
|
1932
|
+
}
|
|
1933
|
+
return recordIdentifierFor(record);
|
|
1934
|
+
}
|
|
1935
|
+
function areAllInverseRecordsLoaded(store, resource) {
|
|
1936
|
+
assertPrivateStore(store);
|
|
1937
|
+
const instanceCache = store._instanceCache;
|
|
1938
|
+
const identifiers = resource.data;
|
|
1939
|
+
if (Array.isArray(identifiers)) {
|
|
1940
|
+
// treat as collection
|
|
1941
|
+
// check for unloaded records
|
|
1942
|
+
return identifiers.every(identifier => instanceCache.recordIsLoaded(identifier));
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
// treat as single resource
|
|
1946
|
+
if (!identifiers) return true;
|
|
1947
|
+
return instanceCache.recordIsLoaded(identifiers);
|
|
1948
|
+
}
|
|
1949
|
+
function getField(context, key) {
|
|
1950
|
+
const {
|
|
1951
|
+
identifier,
|
|
1952
|
+
store
|
|
1953
|
+
} = context;
|
|
1954
|
+
return store.schema.fields(identifier).get(key) ?? store.schema.cacheFields?.(identifier).get(key);
|
|
1955
|
+
}
|
|
1956
|
+
function getRealFieldName(context, key) {
|
|
1957
|
+
const field = getField(context, key);
|
|
1958
|
+
return field ? field.sourceKey ?? field.name : key;
|
|
1959
|
+
}
|
|
1960
|
+
|
|
1961
|
+
// we force the type here to our own construct because mixin and extend patterns
|
|
1962
|
+
// lose generic signatures. We also do this because we need to Omit `clear` from
|
|
1963
|
+
// the type of ArrayProxy as we override it's signature.
|
|
1964
|
+
const ArrayProxyWithCustomOverrides = ArrayProxy;
|
|
1965
|
+
|
|
1966
|
+
/**
|
|
1967
|
+
Holds validation errors for a given record, organized by attribute names.
|
|
1968
|
+
|
|
1969
|
+
This class is not directly instantiable.
|
|
1970
|
+
|
|
1971
|
+
Every `Model` has an `errors` property that is an instance of
|
|
1972
|
+
`Errors`. This can be used to display validation error
|
|
1973
|
+
messages returned from the server when a `record.save()` rejects.
|
|
1974
|
+
|
|
1975
|
+
For Example, if you had a `User` model that looked like this:
|
|
1976
|
+
|
|
1977
|
+
```js [app/models/user.js]
|
|
1978
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
1979
|
+
|
|
1980
|
+
export default class UserModel extends Model {
|
|
1981
|
+
@attr('string') username;
|
|
1982
|
+
@attr('string') email;
|
|
1983
|
+
}
|
|
1984
|
+
```
|
|
1985
|
+
And you attempted to save a record that did not validate on the backend:
|
|
1986
|
+
|
|
1987
|
+
```javascript
|
|
1988
|
+
let user = store.createRecord('user', {
|
|
1989
|
+
username: 'tomster',
|
|
1990
|
+
email: 'invalidEmail'
|
|
1991
|
+
});
|
|
1992
|
+
user.save();
|
|
1993
|
+
```
|
|
1994
|
+
|
|
1995
|
+
Your backend would be expected to return an error response that described
|
|
1996
|
+
the problem, so that error messages can be generated on the app.
|
|
1997
|
+
|
|
1998
|
+
API responses will be translated into instances of `Errors` differently,
|
|
1999
|
+
depending on the specific combination of adapter and serializer used. You
|
|
2000
|
+
may want to check the documentation or the source code of the libraries
|
|
2001
|
+
that you are using, to know how they expect errors to be communicated.
|
|
2002
|
+
|
|
2003
|
+
Errors can be displayed to the user by accessing their property name
|
|
2004
|
+
to get an array of all the error objects for that property. Each
|
|
2005
|
+
error object is a JavaScript object with two keys:
|
|
2006
|
+
|
|
2007
|
+
- `message` A string containing the error message from the backend
|
|
2008
|
+
- `attribute` The name of the property associated with this error message
|
|
2009
|
+
|
|
2010
|
+
```handlebars
|
|
2011
|
+
<label>Username: <Input @value={{@model.username}} /> </label>
|
|
2012
|
+
{{#each @model.errors.username as |error|}}
|
|
2013
|
+
<div class="error">
|
|
2014
|
+
{{error.message}}
|
|
2015
|
+
</div>
|
|
2016
|
+
{{/each}}
|
|
2017
|
+
|
|
2018
|
+
<label>Email: <Input @value={{@model.email}} /> </label>
|
|
2019
|
+
{{#each @model.errors.email as |error|}}
|
|
2020
|
+
<div class="error">
|
|
2021
|
+
{{error.message}}
|
|
2022
|
+
</div>
|
|
2023
|
+
{{/each}}
|
|
2024
|
+
```
|
|
2025
|
+
|
|
2026
|
+
You can also access the special `messages` property on the error
|
|
2027
|
+
object to get an array of all the error strings.
|
|
2028
|
+
|
|
2029
|
+
```handlebars
|
|
2030
|
+
{{#each @model.errors.messages as |message|}}
|
|
2031
|
+
<div class="error">
|
|
2032
|
+
{{message}}
|
|
2033
|
+
</div>
|
|
2034
|
+
{{/each}}
|
|
2035
|
+
```
|
|
2036
|
+
|
|
2037
|
+
@class Errors
|
|
2038
|
+
@public
|
|
2039
|
+
*/
|
|
2040
|
+
class Errors extends ArrayProxyWithCustomOverrides {
|
|
2041
|
+
/**
|
|
2042
|
+
@property errorsByAttributeName
|
|
2043
|
+
@type {MapWithDefault}
|
|
2044
|
+
@private
|
|
2045
|
+
*/
|
|
2046
|
+
get errorsByAttributeName() {
|
|
2047
|
+
return new Map();
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
/**
|
|
2051
|
+
Returns errors for a given attribute
|
|
2052
|
+
```javascript
|
|
2053
|
+
let user = store.createRecord('user', {
|
|
2054
|
+
username: 'tomster',
|
|
2055
|
+
email: 'invalidEmail'
|
|
2056
|
+
});
|
|
2057
|
+
user.save().catch(function(){
|
|
2058
|
+
user.errors.errorsFor('email'); // returns:
|
|
2059
|
+
// [{attribute: "email", message: "Doesn't look like a valid email."}]
|
|
2060
|
+
});
|
|
2061
|
+
```
|
|
2062
|
+
@public
|
|
2063
|
+
@param {String} attribute
|
|
2064
|
+
@return {Array}
|
|
2065
|
+
*/
|
|
2066
|
+
static {
|
|
2067
|
+
decorateMethodV2(this.prototype, "errorsByAttributeName", [computed()]);
|
|
2068
|
+
}
|
|
2069
|
+
errorsFor(attribute) {
|
|
2070
|
+
const map = this.errorsByAttributeName;
|
|
2071
|
+
let errors = map.get(attribute);
|
|
2072
|
+
if (errors === undefined) {
|
|
2073
|
+
errors = A();
|
|
2074
|
+
map.set(attribute, errors);
|
|
2075
|
+
}
|
|
2076
|
+
|
|
2077
|
+
// Errors may be a native array with extensions turned on. Since we access
|
|
2078
|
+
// the array via a method, and not a computed or using `Ember.get`, it does
|
|
2079
|
+
// not entangle properly with autotracking, so we entangle manually by
|
|
2080
|
+
// getting the `[]` property.
|
|
2081
|
+
get(errors, '[]');
|
|
2082
|
+
return errors;
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2085
|
+
/**
|
|
2086
|
+
An array containing all of the error messages for this
|
|
2087
|
+
record. This is useful for displaying all errors to the user.
|
|
2088
|
+
```handlebars
|
|
2089
|
+
{{#each @model.errors.messages as |message|}}
|
|
2090
|
+
<div class="error">
|
|
2091
|
+
{{message}}
|
|
2092
|
+
</div>
|
|
2093
|
+
{{/each}}
|
|
2094
|
+
```
|
|
2095
|
+
@property messages
|
|
2096
|
+
@public
|
|
2097
|
+
@type {Array}
|
|
2098
|
+
*/
|
|
2099
|
+
static {
|
|
2100
|
+
decorateFieldV2(this.prototype, "messages", [mapBy('content', 'message')]);
|
|
2101
|
+
}
|
|
2102
|
+
#messages = (initializeDeferredDecorator(this, "messages"), void 0);
|
|
2103
|
+
/**
|
|
2104
|
+
@property content
|
|
2105
|
+
@type {Array}
|
|
2106
|
+
@private
|
|
2107
|
+
*/
|
|
2108
|
+
get content() {
|
|
2109
|
+
return A();
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
/**
|
|
2113
|
+
@private
|
|
2114
|
+
*/
|
|
2115
|
+
static {
|
|
2116
|
+
decorateMethodV2(this.prototype, "content", [computed()]);
|
|
2117
|
+
}
|
|
2118
|
+
unknownProperty(attribute) {
|
|
2119
|
+
const errors = this.errorsFor(attribute);
|
|
2120
|
+
if (errors.length === 0) {
|
|
2121
|
+
return undefined;
|
|
2122
|
+
}
|
|
2123
|
+
return errors;
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
/**
|
|
2127
|
+
Total number of errors.
|
|
2128
|
+
@property length
|
|
2129
|
+
@type {Number}
|
|
2130
|
+
@public
|
|
2131
|
+
@readonly
|
|
2132
|
+
*/
|
|
2133
|
+
|
|
2134
|
+
/**
|
|
2135
|
+
`true` if we have no errors.
|
|
2136
|
+
@property isEmpty
|
|
2137
|
+
@type {Boolean}
|
|
2138
|
+
@public
|
|
2139
|
+
@readonly
|
|
2140
|
+
*/
|
|
2141
|
+
static {
|
|
2142
|
+
decorateFieldV2(this.prototype, "isEmpty", [not('length')]);
|
|
2143
|
+
}
|
|
2144
|
+
#isEmpty = (initializeDeferredDecorator(this, "isEmpty"), void 0);
|
|
2145
|
+
/**
|
|
2146
|
+
Manually adds errors to the record. This will trigger the `becameInvalid` event/ lifecycle method on
|
|
2147
|
+
the record and transition the record into an `invalid` state.
|
|
2148
|
+
Example
|
|
2149
|
+
```javascript
|
|
2150
|
+
let errors = user.errors;
|
|
2151
|
+
// add multiple errors
|
|
2152
|
+
errors.add('password', [
|
|
2153
|
+
'Must be at least 12 characters',
|
|
2154
|
+
'Must contain at least one symbol',
|
|
2155
|
+
'Cannot contain your name'
|
|
2156
|
+
]);
|
|
2157
|
+
errors.errorsFor('password');
|
|
2158
|
+
// =>
|
|
2159
|
+
// [
|
|
2160
|
+
// { attribute: 'password', message: 'Must be at least 12 characters' },
|
|
2161
|
+
// { attribute: 'password', message: 'Must contain at least one symbol' },
|
|
2162
|
+
// { attribute: 'password', message: 'Cannot contain your name' },
|
|
2163
|
+
// ]
|
|
2164
|
+
// add a single error
|
|
2165
|
+
errors.add('username', 'This field is required');
|
|
2166
|
+
errors.errorsFor('username');
|
|
2167
|
+
// =>
|
|
2168
|
+
// [
|
|
2169
|
+
// { attribute: 'username', message: 'This field is required' },
|
|
2170
|
+
// ]
|
|
2171
|
+
```
|
|
2172
|
+
@public
|
|
2173
|
+
@param {String} attribute - the property name of an attribute or relationship
|
|
2174
|
+
@param {string[]|string} messages - an error message or array of error messages for the attribute
|
|
2175
|
+
*/
|
|
2176
|
+
add(attribute, messages) {
|
|
2177
|
+
const errors = this._findOrCreateMessages(attribute, messages);
|
|
2178
|
+
this.addObjects(errors);
|
|
2179
|
+
this.errorsFor(attribute).addObjects(errors);
|
|
2180
|
+
this.__record.currentState.notify('isValid');
|
|
2181
|
+
this.notifyPropertyChange(attribute);
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
/**
|
|
2185
|
+
@private
|
|
2186
|
+
*/
|
|
2187
|
+
_findOrCreateMessages(attribute, messages) {
|
|
2188
|
+
const errors = this.errorsFor(attribute);
|
|
2189
|
+
const messagesArray = Array.isArray(messages) ? messages : [messages];
|
|
2190
|
+
const _messages = new Array(messagesArray.length);
|
|
2191
|
+
for (let i = 0; i < messagesArray.length; i++) {
|
|
2192
|
+
const message = messagesArray[i];
|
|
2193
|
+
const err = errors.findBy('message', message);
|
|
2194
|
+
if (err) {
|
|
2195
|
+
_messages[i] = err;
|
|
2196
|
+
} else {
|
|
2197
|
+
_messages[i] = {
|
|
2198
|
+
attribute: attribute,
|
|
2199
|
+
message
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
return _messages;
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
/**
|
|
2207
|
+
Manually removes all errors for a given member from the record.
|
|
2208
|
+
This will transition the record into a `valid` state, and
|
|
2209
|
+
triggers the `becameValid` event and lifecycle method.
|
|
2210
|
+
Example:
|
|
2211
|
+
```javascript
|
|
2212
|
+
let errors = user.errors;
|
|
2213
|
+
errors.add('phone', ['error-1', 'error-2']);
|
|
2214
|
+
errors.errorsFor('phone');
|
|
2215
|
+
// =>
|
|
2216
|
+
// [
|
|
2217
|
+
// { attribute: 'phone', message: 'error-1' },
|
|
2218
|
+
// { attribute: 'phone', message: 'error-2' },
|
|
2219
|
+
// ]
|
|
2220
|
+
errors.remove('phone');
|
|
2221
|
+
errors.errorsFor('phone');
|
|
2222
|
+
// => undefined
|
|
2223
|
+
```
|
|
2224
|
+
@public
|
|
2225
|
+
@param {String} member - the property name of an attribute or relationship
|
|
2226
|
+
*/
|
|
2227
|
+
remove(attribute) {
|
|
2228
|
+
if (this.isEmpty) {
|
|
2229
|
+
return;
|
|
2230
|
+
}
|
|
2231
|
+
const content = this.rejectBy('attribute', attribute);
|
|
2232
|
+
this.content.setObjects(content);
|
|
2233
|
+
|
|
2234
|
+
// Although errorsByAttributeName.delete is technically enough to sync errors state, we also
|
|
2235
|
+
// must mutate the array as well for autotracking
|
|
2236
|
+
const errors = this.errorsFor(attribute);
|
|
2237
|
+
for (let i = 0; i < errors.length; i++) {
|
|
2238
|
+
if (errors[i].attribute === attribute) {
|
|
2239
|
+
// .replace from Ember.NativeArray is necessary. JS splice will not work.
|
|
2240
|
+
errors.replace(i, 1);
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
this.errorsByAttributeName.delete(attribute);
|
|
2244
|
+
this.__record.currentState.notify('isValid');
|
|
2245
|
+
this.notifyPropertyChange(attribute);
|
|
2246
|
+
this.notifyPropertyChange('length');
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
/**
|
|
2250
|
+
Manually clears all errors for the record.
|
|
2251
|
+
This will transition the record into a `valid` state, and
|
|
2252
|
+
will trigger the `becameValid` event and lifecycle method.
|
|
2253
|
+
Example:
|
|
2254
|
+
```javascript
|
|
2255
|
+
let errors = user.errors;
|
|
2256
|
+
errors.add('username', ['error-a']);
|
|
2257
|
+
errors.add('phone', ['error-1', 'error-2']);
|
|
2258
|
+
errors.errorsFor('username');
|
|
2259
|
+
// =>
|
|
2260
|
+
// [
|
|
2261
|
+
// { attribute: 'username', message: 'error-a' },
|
|
2262
|
+
// ]
|
|
2263
|
+
errors.errorsFor('phone');
|
|
2264
|
+
// =>
|
|
2265
|
+
// [
|
|
2266
|
+
// { attribute: 'phone', message: 'error-1' },
|
|
2267
|
+
// { attribute: 'phone', message: 'error-2' },
|
|
2268
|
+
// ]
|
|
2269
|
+
errors.clear();
|
|
2270
|
+
errors.errorsFor('username');
|
|
2271
|
+
// => undefined
|
|
2272
|
+
errors.errorsFor('phone');
|
|
2273
|
+
// => undefined
|
|
2274
|
+
errors.messages
|
|
2275
|
+
// => []
|
|
2276
|
+
```
|
|
2277
|
+
@public
|
|
2278
|
+
*/
|
|
2279
|
+
clear() {
|
|
2280
|
+
if (this.isEmpty) {
|
|
2281
|
+
return;
|
|
2282
|
+
}
|
|
2283
|
+
const errorsByAttributeName = this.errorsByAttributeName;
|
|
2284
|
+
const attributes = [];
|
|
2285
|
+
errorsByAttributeName.forEach(function (_, attribute) {
|
|
2286
|
+
attributes.push(attribute);
|
|
2287
|
+
});
|
|
2288
|
+
errorsByAttributeName.clear();
|
|
2289
|
+
attributes.forEach(attribute => {
|
|
2290
|
+
this.notifyPropertyChange(attribute);
|
|
2291
|
+
});
|
|
2292
|
+
this.__record.currentState.notify('isValid');
|
|
2293
|
+
super.clear();
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
/**
|
|
2297
|
+
Checks if there are error messages for the given attribute.
|
|
2298
|
+
```js [app/controllers/user/edit.js]
|
|
2299
|
+
export default class UserEditController extends Controller {
|
|
2300
|
+
@action
|
|
2301
|
+
save(user) {
|
|
2302
|
+
if (user.errors.has('email')) {
|
|
2303
|
+
return alert('Please update your email before attempting to save.');
|
|
2304
|
+
}
|
|
2305
|
+
user.save();
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
```
|
|
2309
|
+
@public
|
|
2310
|
+
@param {String} attribute
|
|
2311
|
+
@return {Boolean} true if there some errors on given attribute
|
|
2312
|
+
*/
|
|
2313
|
+
has(attribute) {
|
|
2314
|
+
return this.errorsFor(attribute).length > 0;
|
|
2315
|
+
}
|
|
2316
|
+
}
|
|
2317
|
+
export { Errors as E, LEGACY_SUPPORT as L, PromiseBelongsTo as P, PromiseManyArray as a, lookupLegacySupport as l };
|