Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active April 7, 2021 21:51
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/1ed784fd06e859a6a00df29c7490525b to your computer and use it in GitHub Desktop.
Save runspired/1ed784fd06e859a6a00df29c7490525b to your computer and use it in GitHub Desktop.
LinkedLists
import Controller from '@ember/controller';
import LinkedList from '../linked-list';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
data = LinkedList.create();
}
import ArrayProxy from '@ember/array/proxy';
import { tracked } from '@glimmer/tracking';
import { A } from '@ember/array';
const Page1 = {
links: {},
meta: {},
data: [
{
type: 'post',
id: '1',
attributes: {
title: 'A spiffy title 1',
}
}
]
}
const Page2 = {
links: {},
meta: {},
data: [
{
type: 'post',
id: '2',
attributes: {
title: 'A spiffy title 2',
}
}
]
}
const Page3 = {
links: {},
meta: {},
data: [
{
type: 'post',
id: '3',
attributes: {
title: 'A spiffy title 3',
}
}
]
}
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),
]);
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;
}
}
<h1>Welcome to {{this.appName}}</h1>
<br>
<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