fix hacker news example

pull/2421/head
Richard Harris 7 years ago
parent df75e0c6d9
commit efa4ff1a44

@ -6,10 +6,6 @@
let item; let item;
let page; let page;
onMount(() => {
hashchange();
});
async function hashchange() { async function hashchange() {
// the poor man's router! // the poor man's router!
const path = window.location.hash.slice(1); const path = window.location.hash.slice(1);
@ -26,6 +22,8 @@
window.location.hash = '/top/1'; window.location.hash = '/top/1';
} }
} }
onMount(hashchange);
</script> </script>
<style> <style>
@ -52,7 +50,7 @@
<main> <main>
{#if item} {#if item}
<Item {item}/> <Item {item} returnTo="#/top/{page}"/>
{:else if page} {:else if page}
<List {page}/> <List {page}/>
{/if} {/if}

@ -1,3 +1,7 @@
<script>
export let comment;
</script>
<style> <style>
article { article {
border-top: 1px solid #eee; border-top: 1px solid #eee;

@ -1,12 +1,8 @@
<script> <script>
import Comment from "./Comment.html"; import Comment from "./Comment.svelte";
export let item; export let item;
export let returnTo;
function back(event) {
event.preventDefault();
window.history.back();
}
</script> </script>
<style> <style>
@ -25,7 +21,7 @@
} }
</style> </style>
<a href="#/top/1" on:click={back}>&laquo; back</a> <a href={returnTo}>&laquo; back</a>
<article> <article>
<a href="{item.url}"> <a href="{item.url}">

@ -1,24 +1,21 @@
<script> <script>
import { beforeUpdate } from "svelte"; import { beforeUpdate } from "svelte";
import Summary from "./Summary.html"; import Summary from "./Summary.svelte";
const PAGE_SIZE = 20; const PAGE_SIZE = 20;
export let items;
export let offset;
export let page; export let page;
let previous_page; let items;
let offset;
beforeUpdate(async () => { $: fetch(`https://node-hnapi.herokuapp.com/news?page=${page}`)
if (page !== previous_page) { .then(r => r.json())
previous_page = page; .then(data => {
items = data;
items = await fetch(`https://node-hnapi.herokuapp.com/news?page=${page}`).then(r => r.json())
offset = PAGE_SIZE * (page - 1); offset = PAGE_SIZE * (page - 1);
window.scrollTo(0, 0); window.scrollTo(0, 0);
} });
});
</script> </script>
<style> <style>

Loading…
Cancel
Save