Skip to content

Commit af7db15

Browse files
authored
Fix: emit is loading when resource come from memory (#217)
Fixes: * Emit from memory before is_loading=true. * loaded attribute added to resource and collections. is_loading deprecated since 2.2.0. * Set loaded and builded, set any child hasMany relationship
1 parent 8c0edc4 commit af7db15

16 files changed

+138
-54
lines changed

demo/app/authors/components/author.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ <h3>Author #{{ author.id }}, with books and photos</h3>
1313
</p>
1414

1515
<h4>Photos</h4>
16+
<demo-collection-info [collection]="author.relationships.photos"></demo-collection-info>
1617
<span *ngIf="author.relationships.photos.data.length === 0">
1718
This author don't have any photo :(
1819
</span>
@@ -24,6 +25,7 @@ <h4>Photos</h4>
2425
</span>
2526

2627
<h4>Books</h4>
28+
<demo-collection-info [collection]="author.relationships.books"></demo-collection-info>
2729
<table class="table table-striped" *ngIf="author.relationships.books.builded">
2830
<thead>
2931
<tr>

demo/app/shared/collection-info.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<p>
22
<code>
33
length={{ collection.data.length }}.
4-
is_loading={{ collection.is_loading }}.
4+
loaded={{ collection.loaded }}.
55
$source={{ collection.source }}.
66
builded={{ collection.builded }}.
77
</code>

demo/app/shared/resource-info.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p>
22
<code>
3-
is_loading={{ resource.is_loading }}.
3+
loaded={{ resource.loaded }}.
44
source={{ resource.source }}.
55
<!-- builded={{ resource.builded }}. -->
66
</code>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,6 @@
156156
"dependencies": {
157157
"dexie": "^2.0.4",
158158
"rxjs": "^6.2"
159-
}
159+
},
160+
"version": "0.0.0"
160161
}

src/common.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('common funcions', () => {
88
it('should be isLive return a correct boolean value', () => {
99
let cacheable: ICacheable = {
1010
is_loading: false,
11+
loaded: true,
1112
source: 'server',
1213
cache_last_update: Date.now() - 1000 * 1000
1314
};

src/document-collection.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,38 @@ export class DocumentCollection<R extends Resource = Resource> extends Document
7979

8080
return total_resources < this.page.total_resources;
8181
}
82+
83+
public setLoaded(value: boolean): void {
84+
// tslint:disable-next-line:deprecation
85+
this.is_loading = !value;
86+
this.loaded = value;
87+
}
88+
89+
public setLoadedAndPropagate(value: boolean): void {
90+
this.setLoaded(value);
91+
this.data.forEach(resource => {
92+
for (let relationship_alias in resource.relationships) {
93+
let relationship = resource.relationships[relationship_alias];
94+
if (relationship instanceof DocumentCollection) {
95+
relationship.setLoaded(value);
96+
}
97+
}
98+
});
99+
}
100+
101+
public setBuilded(value: boolean): void {
102+
this.builded = value;
103+
}
104+
105+
public setBuildedAndPropagate(value: boolean): void {
106+
this.setBuilded(value);
107+
this.data.forEach(resource => {
108+
resource.setLoaded(value);
109+
});
110+
}
111+
112+
/** @todo generate interface */
113+
public setSource(value: 'new' | 'memory' | 'store' | 'server'): void {
114+
this.source = value;
115+
}
82116
}

src/document.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ describe('document', () => {
44
it('should be created', () => {
55
let document = new Document();
66
expect(document.builded).toBe(false);
7-
expect(document.is_loading).toBeTruthy();
7+
expect(document.loaded).toBeFalsy();
88
});
99
});

src/document.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export class Document implements IDocumentData {
66
public builded = false;
77
public content: 'ids' | 'collection' | 'id' | 'resource';
88

9+
// deprecated since 2.2.0. Use loaded.
910
public is_loading = true;
11+
public loaded = false;
1012
public source: 'new' | 'memory' | 'store' | 'server' = 'new';
1113
public cache_last_update = 0;
1214
public meta: {

src/interfaces/cacheable.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export interface ICacheable {
2+
// deprecated since 2.2.0. Use loaded.
23
is_loading: boolean;
4+
loaded: boolean;
35
source: 'new' | 'memory' | 'store' | 'server';
46
cache_last_update: number;
57
ttl?: number;

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-jsonapi",
3-
"version": "2.1.2",
3+
"version": "2.1.6",
44
"description": "JSON API library for Angular",
55
"module": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js",
66
"es2015": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.js",

0 commit comments

Comments
 (0)