@ember-data/model 4.12.0-beta.8 → 4.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -18
- package/addon/-private.js +2 -2
- package/addon/{has-many-c84372b4.js → has-many-465843f4.js} +69 -18
- 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 +18 -17
- package/addon/has-many-c84372b4.js.map +0 -1
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
|
|
|
@@ -3539,7 +3567,7 @@ function computeOnce(target, key, desc) {
|
|
|
3539
3567
|
}
|
|
3540
3568
|
|
|
3541
3569
|
/**
|
|
3542
|
-
Base class from which Models can be
|
|
3570
|
+
Base class from which Models can be defined.
|
|
3543
3571
|
|
|
3544
3572
|
```js
|
|
3545
3573
|
import Model, { attr } from '@ember-data/model';
|
|
@@ -4466,6 +4494,41 @@ let Model = (_class = (_class2 = class Model extends EmberObject {
|
|
|
4466
4494
|
eachAttribute(callback, binding) {
|
|
4467
4495
|
this.constructor.eachAttribute(callback, binding);
|
|
4468
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
|
+
*/
|
|
4469
4532
|
/*
|
|
4470
4533
|
These class methods below provide relationship
|
|
4471
4534
|
introspection abilities about relationships.
|
|
@@ -4479,7 +4542,6 @@ let Model = (_class = (_class2 = class Model extends EmberObject {
|
|
|
4479
4542
|
do it before using your model with the store, which uses these properties
|
|
4480
4543
|
extensively.
|
|
4481
4544
|
*/
|
|
4482
|
-
|
|
4483
4545
|
/**
|
|
4484
4546
|
For a given relationship name, returns the model type of the relationship.
|
|
4485
4547
|
For example, if you define a model like this:
|
|
@@ -5519,6 +5581,7 @@ function normalizeType$1(type) {
|
|
|
5519
5581
|
```
|
|
5520
5582
|
|
|
5521
5583
|
#### One-To-Many
|
|
5584
|
+
|
|
5522
5585
|
To declare a one-to-many relationship between two models, use
|
|
5523
5586
|
`belongsTo` in combination with `hasMany`, like this:
|
|
5524
5587
|
|
|
@@ -5526,7 +5589,7 @@ function normalizeType$1(type) {
|
|
|
5526
5589
|
import Model, { hasMany } from '@ember-data/model';
|
|
5527
5590
|
|
|
5528
5591
|
export default class PostModel extends Model {
|
|
5529
|
-
@hasMany('comment') comments;
|
|
5592
|
+
@hasMany('comment', { async: false, inverse: 'post' }) comments;
|
|
5530
5593
|
}
|
|
5531
5594
|
```
|
|
5532
5595
|
|
|
@@ -5534,23 +5597,10 @@ function normalizeType$1(type) {
|
|
|
5534
5597
|
import Model, { belongsTo } from '@ember-data/model';
|
|
5535
5598
|
|
|
5536
5599
|
export default class CommentModel extends Model {
|
|
5537
|
-
@belongsTo('post') post;
|
|
5538
|
-
}
|
|
5539
|
-
```
|
|
5540
|
-
|
|
5541
|
-
You can avoid passing a string as the first parameter. In that case Ember Data
|
|
5542
|
-
will infer the type from the key name.
|
|
5543
|
-
|
|
5544
|
-
```app/models/comment.js
|
|
5545
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
5546
|
-
|
|
5547
|
-
export default class CommentModel extends Model {
|
|
5548
|
-
@belongsTo post;
|
|
5600
|
+
@belongsTo('post', { async: false, inverse: 'comments' }) post;
|
|
5549
5601
|
}
|
|
5550
5602
|
```
|
|
5551
5603
|
|
|
5552
|
-
will lookup for a Post type.
|
|
5553
|
-
|
|
5554
5604
|
#### Sync relationships
|
|
5555
5605
|
|
|
5556
5606
|
Ember Data resolves sync relationships with the related resources
|
|
@@ -5562,7 +5612,8 @@ function normalizeType$1(type) {
|
|
|
5562
5612
|
|
|
5563
5613
|
export default class CommentModel extends Model {
|
|
5564
5614
|
@belongsTo('post', {
|
|
5565
|
-
async: false
|
|
5615
|
+
async: false,
|
|
5616
|
+
inverse: null
|
|
5566
5617
|
})
|
|
5567
5618
|
post;
|
|
5568
5619
|
}
|