Skip to content

Instantly share code, notes, and snippets.

@SyNeto
Created October 29, 2019 16:28
Show Gist options
  • Save SyNeto/ddbec28afd941d237236c67e3c110abe to your computer and use it in GitHub Desktop.
Save SyNeto/ddbec28afd941d237236c67e3c110abe to your computer and use it in GitHub Desktop.
<script>
import { onMount } from 'svelte';
import { posts } from './blogStore.js';
onMount(async () => {
const data = await fetchPosts();
posts.set(data);
})
const fetchPosts = async (url=null) => {
const res = await fetch(url || 'http://localhost:8000/api/posts/');
return await res.json();
}
const handleNextPage = async (url) => {
const nextPage = await fetchPosts(url);
posts.update(x => ({...x, ...nextPage}));
}
</script>
<h1>Post List</h1>
<p>{JSON.stringify($posts.results)}</p>
<ul>
{#each $posts.results as post}
<li>{post.title}</li>
{/each}
</ul>
<p>
<span on:click={() => handleNextPage($posts.previous)}>prev</span> |
<span on:click={() => handleNextPage($posts.next)}>next</span>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment