Skip to content

Commit 87be93f

Browse files
committed
Add more legacy examples, update layout
1 parent 1a841c5 commit 87be93f

File tree

5 files changed

+102
-15
lines changed

5 files changed

+102
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<VerticalCollection
2+
@items={{this.books}}
3+
@tagName="ul"
4+
@staticHeight={{true}}
5+
@estimateHeight={{60}}
6+
@bufferSize={{10}}
7+
@lastReached={{this.next}}
8+
@containerSelector=".scroll-container"
9+
as |book|
10+
>
11+
<li>
12+
<strong>{{book.title}}</strong> ({{book.genre}}) - {{book.publicationDate}}
13+
<br> by <em>{{book.author}}</em> ISBN: {{book.isbn}}
14+
</li>
15+
</VerticalCollection>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { service } from '@ember/service';
2+
import Component from '@glimmer/component';
3+
import { tracked } from '@glimmer/tracking';
4+
5+
import type Store from '@ember-data/store';
6+
import type { Collection } from '@ember-data/store/-private/record-arrays/identifier-array';
7+
8+
import type Book from '../models/book';
9+
10+
export interface InfiniteBookSignature {
11+
Element: HTMLUListElement;
12+
Args: {
13+
allBooks: Collection<Book>;
14+
};
15+
}
16+
17+
class Pages<T> {
18+
@tracked pages: Collection<T>[] = [];
19+
@tracked data: T[] = [];
20+
21+
constructor(page: Collection<T>) {
22+
this.pages = [page];
23+
this.data = page.slice();
24+
}
25+
26+
addPage(page: Collection<T>) {
27+
this.pages.push(page);
28+
this.data = this.data.concat(page);
29+
}
30+
}
31+
32+
export default class InfiniteBookComponent extends Component<InfiniteBookSignature> {
33+
@service declare store: Store;
34+
pageCollection = new Pages(this.args.allBooks);
35+
36+
get books(): Book[] {
37+
return this.pageCollection.data;
38+
}
39+
40+
next = async () => {
41+
const meta = this.pageCollection.pages.at(-1)?.query as { page: number; pageSize: number };
42+
if (!meta) {
43+
return;
44+
}
45+
const result = await this.store.query<Book>('book', { page: meta.page + 1, pageSize: meta.pageSize });
46+
this.pageCollection.addPage(result);
47+
};
48+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import Model, { attr } from '@ember-data/model';
2+
import { ResourceType } from '@warp-drive/core-types/symbols';
23

34
export default class Book extends Model {
45
@attr declare title: string;
56
@attr declare isbn: string;
67
@attr declare publicationDate: string;
78
@attr declare author: string;
89
@attr declare genre: string;
10+
11+
[ResourceType] = 'book' as const;
912
}

tests/incremental-json-api/app/routes/application.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@ export default class ApplicationRoute extends Route {
1515
override async model() {
1616
const genres = this.store.request<Document<Genre[]>>({ url: '/api/books/genres' });
1717
const authors = this.store.request<Document<Author[]>>({ url: '/api/books/authors' });
18-
const books = this.store.request<Document<Book[]>>(query('book'));
18+
19+
// Example of legacy usage to be refactored, unpaginated
1920
const oldBooks = this.store.findAll('book');
2021

21-
const data = await Promise.all([genres, authors, books, oldBooks]);
22+
// Example of legacy usage, paginated
23+
const oldBooksPaginated = this.store.query('book', { page: 1, pageSize: 20 });
24+
25+
// Example of new usage (refactored, paginated)
26+
const books = this.store.request<Document<Book[]>>(query('book'));
27+
28+
const data = await Promise.all([genres, authors, books, oldBooks, oldBooksPaginated]);
2229

2330
return {
2431
genres: data[0].content.data!,
2532
authors: data[1].content.data!,
2633
allBooks: data[2].content,
27-
oldBooks: Array.from(data[3]),
34+
oldBooks: data[3],
35+
oldBooksPaginated: data[4],
2836
};
2937
}
3038
}

tests/incremental-json-api/app/templates/application.hbs

+25-12
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,28 @@
1616
</tbody>
1717
</table>
1818

19-
{{outlet}}
20-
<section>
21-
<h2>Old Books({{@model.oldBooks.length}})</h2>
22-
<ul>
23-
{{#each @model.oldBooks as |book|}}
24-
<li>
25-
<strong>{{book.title}}</strong> ({{book.genre}}) - {{book.publicationDate}}
26-
<br> by <em>{{book.author}}</em> ISBN: {{book.isbn}}
27-
</li>
28-
{{/each}}
29-
</ul>
30-
</section>
19+
<table>
20+
<tbody>
21+
<tr>
22+
<td>
23+
<h2>Old Books (All) ({{@model.oldBooks.length}})</h2>
24+
<ul>
25+
{{#each @model.oldBooks as |book|}}
26+
<li>
27+
<strong>{{book.title}}</strong> ({{book.genre}}) - {{book.publicationDate}}
28+
<br> by <em>{{book.author}}</em> ISBN: {{book.isbn}}
29+
</li>
30+
{{/each}}
31+
</ul>
32+
</td>
33+
<td>
34+
<h2>Old Books (Paginated) ({{@model.oldBooksPaginated.length}})</h2>
35+
<div class="scroll-container">
36+
<LegacyInfiniteBooks @allBooks={{@model.oldBooksPaginated}} />
37+
</div>
38+
</td>
39+
</tr>
40+
</tbody>
41+
</table>
42+
43+
{{outlet}}

0 commit comments

Comments
 (0)