@ember-data/model 4.12.0-beta.7 → 4.12.0-beta.9
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/LICENSE.md +1 -1
- package/README.md +42 -18
- package/addon/-private.js +2 -2
- package/addon/{has-many-f4cb2303.js → has-many-465843f4.js} +88 -22
- package/addon/has-many-465843f4.js.map +1 -0
- package/addon/index.js +1 -1
- package/blueprints/model/HELP.md +26 -0
- package/blueprints/model/files/__root__/__path__/__name__.js +5 -0
- package/blueprints/model/index.js +150 -0
- package/blueprints/model/native-files/__root__/__path__/__name__.js +5 -0
- package/blueprints/model-test/index.js +33 -0
- package/blueprints/model-test/mocha-files/__root__/__path__/__test__.js +18 -0
- package/blueprints/model-test/mocha-rfc-232-files/__root__/__path__/__test__.js +15 -0
- package/blueprints/model-test/qunit-files/__root__/__path__/__test__.js +14 -0
- package/package.json +23 -18
- package/addon/has-many-f4cb2303.js.map +0 -1
package/LICENSE.md
CHANGED
package/README.md
CHANGED
|
@@ -1,31 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img
|
|
3
|
+
class="project-logo"
|
|
4
|
+
src="./ember-data-logo-dark.svg#gh-dark-mode-only"
|
|
5
|
+
alt="EmberData Model"
|
|
6
|
+
width="240px"
|
|
7
|
+
title="EmberData Model"
|
|
8
|
+
/>
|
|
9
|
+
<img
|
|
10
|
+
class="project-logo"
|
|
11
|
+
src="./ember-data-logo-light.svg#gh-light-mode-only"
|
|
12
|
+
alt="EmberData Model"
|
|
13
|
+
width="240px"
|
|
14
|
+
title="EmberData Model"
|
|
15
|
+
/>
|
|
16
|
+
</p>
|
|
3
17
|
|
|
4
|
-
|
|
18
|
+
<p align="center">Provides a Presentation Model for resource data in an EmberData Cache</p>
|
|
5
19
|
|
|
20
|
+
This package implements the EmberData Store's `instantiateRecord` and `teardownRecord` hooks
|
|
21
|
+
as well as configures an associated `SchemaService` implementation.
|
|
6
22
|
|
|
7
|
-
|
|
8
|
-
|
|
23
|
+
Models are defined as classes extending from `import Model from '@ember-data/model';` and the
|
|
24
|
+
attributes and relationships on these classes are parsed at runtime to supply static "schema"
|
|
25
|
+
to EmberData's SchemaService.
|
|
9
26
|
|
|
10
|
-
|
|
11
|
-
|
|
27
|
+
Resource data for individual resources fetched from your API is presented to the UI via instances
|
|
28
|
+
of the `Model`s you define. An instantiated `Model` is referred to as a `record`.
|
|
12
29
|
|
|
30
|
+
When we refer to the `ModelClass` as opposed to a `Model` or `Record` we are referring
|
|
31
|
+
specifically to the class definition and the static schema methods present on it.
|
|
13
32
|
|
|
14
|
-
|
|
15
|
-
|
|
33
|
+
When we refer to a `record` we refer to a specific class instance presenting
|
|
34
|
+
the resource data for a given `type` and `id`.
|
|
16
35
|
|
|
17
|
-
|
|
18
|
-
ember install @ember-data/model
|
|
19
|
-
```
|
|
36
|
+
### Defining a Model
|
|
20
37
|
|
|
38
|
+
*app/models/person.js*
|
|
39
|
+
```ts
|
|
40
|
+
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
|
|
21
41
|
|
|
22
|
-
|
|
23
|
-
|
|
42
|
+
export default class PersonModel extends Model {
|
|
43
|
+
@attr name;
|
|
24
44
|
|
|
25
|
-
|
|
45
|
+
@belongsTo('pet', { inverse: 'owners', async: false }) dog;
|
|
26
46
|
|
|
47
|
+
@hasMany('person', { inverse: 'friends', async: true }) friends;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
27
50
|
|
|
28
|
-
|
|
29
|
-
------------------------------------------------------------------------------
|
|
51
|
+
### modelName convention
|
|
30
52
|
|
|
31
|
-
|
|
53
|
+
By convention, the name of a given model (its `type`) matches the name
|
|
54
|
+
of the file in the `app/models` folder and should be lowercase, singular
|
|
55
|
+
and dasherized.
|
package/addon/-private.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { M as Model } from "./has-many-
|
|
2
|
-
export { E as Errors, L as LEGACY_SUPPORT, R as ManyArray, P as PromiseBelongsTo, c as PromiseManyArray, a as attr, b as belongsTo, h as hasMany } from "./has-many-
|
|
1
|
+
import { M as Model } from "./has-many-465843f4";
|
|
2
|
+
export { E as Errors, L as LEGACY_SUPPORT, R as ManyArray, P as PromiseBelongsTo, c as PromiseManyArray, a as attr, b as belongsTo, h as hasMany } from "./has-many-465843f4";
|
|
3
3
|
import { getOwner } from '@ember/application';
|
|
4
4
|
|
|
5
5
|
/*
|
|
@@ -269,6 +269,11 @@ function deprecatedPromiseObject(promise) {
|
|
|
269
269
|
return new Proxy(promiseObjectProxy, handler);
|
|
270
270
|
}
|
|
271
271
|
var _dec$1, _dec2, _dec3, _dec4, _class$6, _descriptor$5, _descriptor2$2;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
@module @ember-data/model
|
|
275
|
+
*/
|
|
276
|
+
|
|
272
277
|
// we force the type here to our own construct because mixin and extend patterns
|
|
273
278
|
// lose generic signatures. We also do this because we need to Omit `clear` from
|
|
274
279
|
// the type of ArrayProxy as we override it's signature.
|
|
@@ -952,6 +957,9 @@ function isPromiseRecord$1(record) {
|
|
|
952
957
|
return !!record.then;
|
|
953
958
|
}
|
|
954
959
|
var _dec, _class$5;
|
|
960
|
+
|
|
961
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
962
|
+
|
|
955
963
|
const Extended = PromiseObject;
|
|
956
964
|
|
|
957
965
|
/**
|
|
@@ -998,6 +1006,26 @@ let PromiseBelongsTo = (_dec = computed(), (_class$5 = class PromiseBelongsTo ex
|
|
|
998
1006
|
}
|
|
999
1007
|
}, (_applyDecoratedDescriptor(_class$5.prototype, "id", [cached], Object.getOwnPropertyDescriptor(_class$5.prototype, "id"), _class$5.prototype), _applyDecoratedDescriptor(_class$5.prototype, "meta", [_dec], Object.getOwnPropertyDescriptor(_class$5.prototype, "meta"), _class$5.prototype)), _class$5));
|
|
1000
1008
|
var _class$4, _descriptor$4, _descriptor2$1, _descriptor3, _descriptor4, _descriptor5;
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
@module @ember-data/model
|
|
1012
|
+
*/
|
|
1013
|
+
/**
|
|
1014
|
+
This class is returned as the result of accessing an async hasMany relationship
|
|
1015
|
+
on an instance of a Model extending from `@ember-data/model`.
|
|
1016
|
+
|
|
1017
|
+
A PromiseManyArray is an iterable proxy that allows templates to consume related
|
|
1018
|
+
ManyArrays and update once their contents are no longer pending.
|
|
1019
|
+
|
|
1020
|
+
In your JS code you should resolve the promise first.
|
|
1021
|
+
|
|
1022
|
+
```js
|
|
1023
|
+
const comments = await post.comments;
|
|
1024
|
+
```
|
|
1025
|
+
|
|
1026
|
+
@class PromiseManyArray
|
|
1027
|
+
@public
|
|
1028
|
+
*/
|
|
1001
1029
|
let PromiseManyArray = (_class$4 = class PromiseManyArray {
|
|
1002
1030
|
// @deprecated (isDestroyed is not deprecated)
|
|
1003
1031
|
|
|
@@ -2716,7 +2744,10 @@ class LegacySupport {
|
|
|
2716
2744
|
return this.store.request({
|
|
2717
2745
|
op: 'findHasMany',
|
|
2718
2746
|
records: identifiers || [],
|
|
2719
|
-
data: request
|
|
2747
|
+
data: request,
|
|
2748
|
+
cacheOptions: {
|
|
2749
|
+
[Symbol.for('ember-data:skip-cache')]: true
|
|
2750
|
+
}
|
|
2720
2751
|
});
|
|
2721
2752
|
}
|
|
2722
2753
|
const preferLocalCache = hasReceivedData && !isEmpty;
|
|
@@ -2733,7 +2764,10 @@ class LegacySupport {
|
|
|
2733
2764
|
return this.store.request({
|
|
2734
2765
|
op: 'findHasMany',
|
|
2735
2766
|
records: identifiers,
|
|
2736
|
-
data: request
|
|
2767
|
+
data: request,
|
|
2768
|
+
cacheOptions: {
|
|
2769
|
+
[Symbol.for('ember-data:skip-cache')]: true
|
|
2770
|
+
}
|
|
2737
2771
|
});
|
|
2738
2772
|
}
|
|
2739
2773
|
|
|
@@ -2781,7 +2815,10 @@ class LegacySupport {
|
|
|
2781
2815
|
const future = this.store.request({
|
|
2782
2816
|
op: 'findBelongsTo',
|
|
2783
2817
|
records: identifier ? [identifier] : [],
|
|
2784
|
-
data: request
|
|
2818
|
+
data: request,
|
|
2819
|
+
cacheOptions: {
|
|
2820
|
+
[Symbol.for('ember-data:skip-cache')]: true
|
|
2821
|
+
}
|
|
2785
2822
|
});
|
|
2786
2823
|
this._pending = future.then(doc => doc.content).finally(() => {
|
|
2787
2824
|
this._pending = null;
|
|
@@ -2812,7 +2849,10 @@ class LegacySupport {
|
|
|
2812
2849
|
this._pending = this.store.request({
|
|
2813
2850
|
op: 'findBelongsTo',
|
|
2814
2851
|
records: [identifier],
|
|
2815
|
-
data: request
|
|
2852
|
+
data: request,
|
|
2853
|
+
cacheOptions: {
|
|
2854
|
+
[Symbol.for('ember-data:skip-cache')]: true
|
|
2855
|
+
}
|
|
2816
2856
|
}).then(doc => doc.content).finally(() => {
|
|
2817
2857
|
this._pending = null;
|
|
2818
2858
|
});
|
|
@@ -3527,7 +3567,7 @@ function computeOnce(target, key, desc) {
|
|
|
3527
3567
|
}
|
|
3528
3568
|
|
|
3529
3569
|
/**
|
|
3530
|
-
Base class from which Models can be
|
|
3570
|
+
Base class from which Models can be defined.
|
|
3531
3571
|
|
|
3532
3572
|
```js
|
|
3533
3573
|
import Model, { attr } from '@ember-data/model';
|
|
@@ -4277,6 +4317,9 @@ let Model = (_class = (_class2 = class Model extends EmberObject {
|
|
|
4277
4317
|
data: {
|
|
4278
4318
|
options,
|
|
4279
4319
|
record: identifier
|
|
4320
|
+
},
|
|
4321
|
+
cacheOptions: {
|
|
4322
|
+
[Symbol.for('ember-data:skip-cache')]: true
|
|
4280
4323
|
}
|
|
4281
4324
|
}).then(() => this).finally(() => {
|
|
4282
4325
|
this.isReloading = false;
|
|
@@ -4451,6 +4494,41 @@ let Model = (_class = (_class2 = class Model extends EmberObject {
|
|
|
4451
4494
|
eachAttribute(callback, binding) {
|
|
4452
4495
|
this.constructor.eachAttribute(callback, binding);
|
|
4453
4496
|
}
|
|
4497
|
+
|
|
4498
|
+
/**
|
|
4499
|
+
Create should only ever be called by the store. To create an instance of a
|
|
4500
|
+
`Model` in a dirty state use `store.createRecord`.
|
|
4501
|
+
To create instances of `Model` in a clean state, use `store.push`
|
|
4502
|
+
@method create
|
|
4503
|
+
@private
|
|
4504
|
+
@static
|
|
4505
|
+
*/
|
|
4506
|
+
/**
|
|
4507
|
+
Represents the model's class name as a string. This can be used to look up the model's class name through
|
|
4508
|
+
`Store`'s modelFor method.
|
|
4509
|
+
`modelName` is generated for you by Ember Data. It will be a lowercased, dasherized string.
|
|
4510
|
+
For example:
|
|
4511
|
+
```javascript
|
|
4512
|
+
store.modelFor('post').modelName; // 'post'
|
|
4513
|
+
store.modelFor('blog-post').modelName; // 'blog-post'
|
|
4514
|
+
```
|
|
4515
|
+
The most common place you'll want to access `modelName` is in your serializer's `payloadKeyFromModelName` method. For example, to change payload
|
|
4516
|
+
keys to underscore (instead of dasherized), you might use the following code:
|
|
4517
|
+
```javascript
|
|
4518
|
+
import RESTSerializer from '@ember-data/serializer/rest';
|
|
4519
|
+
import { underscore } from '<app-name>/utils/string-utils';
|
|
4520
|
+
export default const PostSerializer = RESTSerializer.extend({
|
|
4521
|
+
payloadKeyFromModelName(modelName) {
|
|
4522
|
+
return underscore(modelName);
|
|
4523
|
+
}
|
|
4524
|
+
});
|
|
4525
|
+
```
|
|
4526
|
+
@property modelName
|
|
4527
|
+
@public
|
|
4528
|
+
@type String
|
|
4529
|
+
@readonly
|
|
4530
|
+
@static
|
|
4531
|
+
*/
|
|
4454
4532
|
/*
|
|
4455
4533
|
These class methods below provide relationship
|
|
4456
4534
|
introspection abilities about relationships.
|
|
@@ -4464,7 +4542,6 @@ let Model = (_class = (_class2 = class Model extends EmberObject {
|
|
|
4464
4542
|
do it before using your model with the store, which uses these properties
|
|
4465
4543
|
extensively.
|
|
4466
4544
|
*/
|
|
4467
|
-
|
|
4468
4545
|
/**
|
|
4469
4546
|
For a given relationship name, returns the model type of the relationship.
|
|
4470
4547
|
For example, if you define a model like this:
|
|
@@ -5504,6 +5581,7 @@ function normalizeType$1(type) {
|
|
|
5504
5581
|
```
|
|
5505
5582
|
|
|
5506
5583
|
#### One-To-Many
|
|
5584
|
+
|
|
5507
5585
|
To declare a one-to-many relationship between two models, use
|
|
5508
5586
|
`belongsTo` in combination with `hasMany`, like this:
|
|
5509
5587
|
|
|
@@ -5511,7 +5589,7 @@ function normalizeType$1(type) {
|
|
|
5511
5589
|
import Model, { hasMany } from '@ember-data/model';
|
|
5512
5590
|
|
|
5513
5591
|
export default class PostModel extends Model {
|
|
5514
|
-
@hasMany('comment') comments;
|
|
5592
|
+
@hasMany('comment', { async: false, inverse: 'post' }) comments;
|
|
5515
5593
|
}
|
|
5516
5594
|
```
|
|
5517
5595
|
|
|
@@ -5519,23 +5597,10 @@ function normalizeType$1(type) {
|
|
|
5519
5597
|
import Model, { belongsTo } from '@ember-data/model';
|
|
5520
5598
|
|
|
5521
5599
|
export default class CommentModel extends Model {
|
|
5522
|
-
@belongsTo('post') post;
|
|
5600
|
+
@belongsTo('post', { async: false, inverse: 'comments' }) post;
|
|
5523
5601
|
}
|
|
5524
5602
|
```
|
|
5525
5603
|
|
|
5526
|
-
You can avoid passing a string as the first parameter. In that case Ember Data
|
|
5527
|
-
will infer the type from the key name.
|
|
5528
|
-
|
|
5529
|
-
```app/models/comment.js
|
|
5530
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
5531
|
-
|
|
5532
|
-
export default class CommentModel extends Model {
|
|
5533
|
-
@belongsTo post;
|
|
5534
|
-
}
|
|
5535
|
-
```
|
|
5536
|
-
|
|
5537
|
-
will lookup for a Post type.
|
|
5538
|
-
|
|
5539
5604
|
#### Sync relationships
|
|
5540
5605
|
|
|
5541
5606
|
Ember Data resolves sync relationships with the related resources
|
|
@@ -5547,7 +5612,8 @@ function normalizeType$1(type) {
|
|
|
5547
5612
|
|
|
5548
5613
|
export default class CommentModel extends Model {
|
|
5549
5614
|
@belongsTo('post', {
|
|
5550
|
-
async: false
|
|
5615
|
+
async: false,
|
|
5616
|
+
inverse: null
|
|
5551
5617
|
})
|
|
5552
5618
|
post;
|
|
5553
5619
|
}
|