Last active
May 6, 2021 17:58
-
-
Save runspired/19433433f4a3b5f108266ef53e5af32e to your computer and use it in GitHub Desktop.
Paginated Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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