Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active May 6, 2021 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runspired/19433433f4a3b5f108266ef53e5af32e to your computer and use it in GitHub Desktop.
Save runspired/19433433f4a3b5f108266ef53e5af32e to your computer and use it in GitHub Desktop.
Paginated Collections
import Controller from '@ember/controller';
import Paginated from '../paginated';
import { tracked } from '@glimmer/tracking';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
@tracked pageIndex = 0;
data = Paginated.create();
}
import ArrayProxy from '@ember/array/proxy';
import { tracked } from '@glimmer/tracking';
import { A } from '@ember/array';
const Page1 = {
links: {},
meta: {
pageName: 'page 1!',
},
data: [
{
type: 'post',
id: '1',
attributes: {
title: 'A spiffy title 1',
}
},
{
type: 'post',
id: '2',
attributes: {
title: 'A spiffy title 2',
}
},
]
}
const Page2 = {
links: {},
meta: {
pageName: 'page 2!',
},
data: [
{
type: 'post',
id: '3',
attributes: {
title: 'A spiffy title 3',
}
},
{
type: 'post',
id: '4',
attributes: {
title: 'A spiffy title 4',
}
}
]
}
const Page3 = {
links: {},
meta: {
pageName: 'page 3!',
},
data: [
{
type: 'post',
id: '5',
attributes: {
title: 'A spiffy title 5',
}
},
{
type: 'post',
id: '6',
attributes: {
title: 'A spiffy title 6',
}
}
]
}
class Resource {
constructor(data) {
this.id = data.id;
this.type = data.type;
Object.assign(this, data.attributes);
}
}
class Collection {
@tracked links;
@tracked meta;
@tracked data;
constructor({ links, meta, data }) {
this.links = links;
this.meta = meta;
this.data = A(data.map(d => new Resource(d)));
}
}
const pagesForCollection = A([
new Collection(Page1),
new Collection(Page2),
new Collection(Page3),
]);
// Extending ArrayProxy is only necessary in older Ember versions
export default class PaginatedCollection extends ArrayProxy {
@tracked pages = pagesForCollection;
get length() {
return this.pages.reduce((count, page) => { return count + page.data.length; }, 0);
}
objectAt(index) {
const { pages } = this;
let counted = 0;
for (let i = 0; i < pages.length; i++) {
let startCount = counted;
counted += pages[i].data.length;
if (index < counted) {
return pages[i].data[index - startCount];
}
}
return undefined;
}
forEach(cb) {
for (let index = 0; index < this.length; index++) {
cb(this.objectAt(index));
}
}
}
<h1>Welcome to {{this.appName}}</h1>
<br>
{{#let (get this.data.pages this.pageIndex) as |page|}}
<h3>Posts {{page.meta.pageName}}</h3>
<ul>
{{#each page.data as |post|}}
<li>{{post.title}}</li>
{{/each}}
</ul>
{{/let}}
{{#each this.data.pages as |page index|}}
<button {{on 'click' (fn (mut this.pageIndex) index)}}>{{index}}</button>
{{/each}}
<h3>All Posts</h3>
<ul>
{{#each this.data as |item index|}}
<li>{{item.title}}</li>
{{/each}}
</ul>
<br>
{{outlet}}
<br>
<br>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment