@ember-data/model 4.12.0-beta.1 → 4.12.0-beta.10
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 +3 -61
- package/addon/-private.js.map +1 -1
- package/addon/{has-many-0b8d932c.js → has-many-2eb0dc13.js} +339 -1338
- package/addon/has-many-2eb0dc13.js.map +1 -0
- package/addon/index.js +1 -1
- package/addon-main.js +18 -15
- 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 +32 -26
- package/addon/has-many-0b8d932c.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-2eb0dc13";
|
|
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-2eb0dc13";
|
|
3
3
|
import { getOwner } from '@ember/application';
|
|
4
4
|
|
|
5
5
|
/*
|
|
@@ -34,62 +34,4 @@ function modelForMixin(store, normalizedModelName) {
|
|
|
34
34
|
}
|
|
35
35
|
return owner.factoryFor(`model:${normalizedModelName}`);
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
@module @ember-data/model
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
@method diffArray
|
|
44
|
-
@internal
|
|
45
|
-
@param {Array} oldArray the old array
|
|
46
|
-
@param {Array} newArray the new array
|
|
47
|
-
@return {hash} {
|
|
48
|
-
firstChangeIndex: <integer>, // null if no change
|
|
49
|
-
addedCount: <integer>, // 0 if no change
|
|
50
|
-
removedCount: <integer> // 0 if no change
|
|
51
|
-
}
|
|
52
|
-
*/
|
|
53
|
-
function diffArray(oldArray, newArray) {
|
|
54
|
-
const oldLength = oldArray.length;
|
|
55
|
-
const newLength = newArray.length;
|
|
56
|
-
const shortestLength = Math.min(oldLength, newLength);
|
|
57
|
-
let firstChangeIndex = null; // null signifies no changes
|
|
58
|
-
|
|
59
|
-
// find the first change
|
|
60
|
-
for (let i = 0; i < shortestLength; i++) {
|
|
61
|
-
// compare each item in the array
|
|
62
|
-
if (oldArray[i] !== newArray[i]) {
|
|
63
|
-
firstChangeIndex = i;
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (firstChangeIndex === null && newLength !== oldLength) {
|
|
68
|
-
// no change found in the overlapping block
|
|
69
|
-
// and array lengths differ,
|
|
70
|
-
// so change starts at end of overlap
|
|
71
|
-
firstChangeIndex = shortestLength;
|
|
72
|
-
}
|
|
73
|
-
let addedCount = 0;
|
|
74
|
-
let removedCount = 0;
|
|
75
|
-
if (firstChangeIndex !== null) {
|
|
76
|
-
// we found a change, find the end of the change
|
|
77
|
-
let unchangedEndBlockLength = shortestLength - firstChangeIndex;
|
|
78
|
-
// walk back from the end of both arrays until we find a change
|
|
79
|
-
for (let i = 1; i <= shortestLength; i++) {
|
|
80
|
-
// compare each item in the array
|
|
81
|
-
if (oldArray[oldLength - i] !== newArray[newLength - i]) {
|
|
82
|
-
unchangedEndBlockLength = i - 1;
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
addedCount = newLength - unchangedEndBlockLength - firstChangeIndex;
|
|
87
|
-
removedCount = oldLength - unchangedEndBlockLength - firstChangeIndex;
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
firstChangeIndex,
|
|
91
|
-
addedCount,
|
|
92
|
-
removedCount
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
export { Model, modelForMixin as _modelForMixin, diffArray };
|
|
37
|
+
export { Model, modelForMixin as _modelForMixin };
|
package/addon/-private.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"-private.js","sources":["../src/-private/model-for-mixin.ts"
|
|
1
|
+
{"version":3,"file":"-private.js","sources":["../src/-private/model-for-mixin.ts"],"sourcesContent":["import { getOwner } from '@ember/application';\n\nimport type Store from '@ember-data/store';\n\nimport Model from './model';\n\n/*\n In case someone defined a relationship to a mixin, for example:\n ```\n import Model, { belongsTo, hasMany } from '@ember-data/model';\n import Mixin from '@ember/object/mixin';\n\n class CommentModel extends Model {\n @belongsTo('commentable', { polymorphic: true }) owner;\n }\n\n let Commentable = Mixin.create({\n @hasMany('comment') comments;\n });\n ```\n we want to look up a Commentable class which has all the necessary\n relationship meta data. Thus, we look up the mixin and create a mock\n Model, so we can access the relationship CPs of the mixin (`comments`)\n in this case\n */\nexport default function modelForMixin(store: Store, normalizedModelName: string): Model | null {\n let owner: any = getOwner(store);\n let MaybeMixin = owner.factoryFor(`mixin:${normalizedModelName}`);\n let mixin = MaybeMixin && MaybeMixin.class;\n if (mixin) {\n let ModelForMixin = Model.extend(mixin);\n ModelForMixin.__isMixin = true;\n ModelForMixin.__mixin = mixin;\n //Cache the class as a model\n owner.register('model:' + normalizedModelName, ModelForMixin);\n }\n return owner.factoryFor(`model:${normalizedModelName}`);\n}\n"],"names":["modelForMixin","store","normalizedModelName","owner","getOwner","MaybeMixin","factoryFor","mixin","class","ModelForMixin","Model","extend","__isMixin","__mixin","register"],"mappings":";;;;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,aAAaA,CAACC,KAAY,EAAEC,mBAA2B,EAAgB;AAC7F,EAAA,IAAIC,KAAU,GAAGC,QAAQ,CAACH,KAAK,CAAC,CAAA;EAChC,IAAII,UAAU,GAAGF,KAAK,CAACG,UAAU,CAAE,CAAA,MAAA,EAAQJ,mBAAoB,CAAA,CAAC,CAAC,CAAA;AACjE,EAAA,IAAIK,KAAK,GAAGF,UAAU,IAAIA,UAAU,CAACG,KAAK,CAAA;AAC1C,EAAA,IAAID,KAAK,EAAE;AACT,IAAA,IAAIE,aAAa,GAAGC,KAAK,CAACC,MAAM,CAACJ,KAAK,CAAC,CAAA;IACvCE,aAAa,CAACG,SAAS,GAAG,IAAI,CAAA;IAC9BH,aAAa,CAACI,OAAO,GAAGN,KAAK,CAAA;AAC7B;IACAJ,KAAK,CAACW,QAAQ,CAAC,QAAQ,GAAGZ,mBAAmB,EAAEO,aAAa,CAAC,CAAA;AAC/D,GAAA;AACA,EAAA,OAAON,KAAK,CAACG,UAAU,CAAE,CAAQJ,MAAAA,EAAAA,mBAAoB,EAAC,CAAC,CAAA;AACzD;;;;"}
|