concept dog in category svelte

appears as: dogs, dog, dog, dogs
Svelte and Sapper in Action

This is an excerpt from Manning's book Svelte and Sapper in Action.

For example, suppose we have a getDogs function that calls an API service, perhaps using the Fetch API, to retrieve a list of dog descriptions. Calling an API service is an asynchronous operation, so a JavaScript Promise object is returned. It will resolve to an array of objects that describe dogs. Each has a name and breed property.

{#await getDogs() then dogs}
  {#each dogs as dog}
    <div>{dog.name} is a {dog.breed}.</div>
  {/each}
{:catch error}
  <div class="error">Error: {error.message}</div>
{/await}
  • Create a dogs directory under src/routes.
  • Create the following files in the dogs directory. First, create src/routes/dogs/dogs.json: ❶ Feel free to substitute dogs you know. The next file, src/routes/dogs/index.json.js, implements an API service that retrieves a sorted array of dog names: ❶ This gets the dog names from dogs.json and sorts them ❷ This implements the GET /dogs API service. Alternatively, we could implement this to get the dog names from a database. The next file, src/routes/dogs/[name].json.js, implements an API service that retrieves an object that describes the dog with a given name: ❶ This implements the GET /dogs/{name} API service. Alternatively, we could implement this to get the dog data from a database. The next file, src/routes/dogs/index.svelte, implements the Dogs page, which renders an anchor for each dog, styled to look like a button. Clicking a button navigates to the page for that dog. ❶ This retrieves an array of dog names by invoking an API service. ❷ This supplies the value of the names prop used in the #each block. ❸ This is only here to demonstrate what happens when there is a link to a non-existent page. The next file, src/routes/dogs/[name].svelte, implements the page that renders data for a specific dog. ❶ This retrieves an object describing a dog with a given name by invoking an API. ❷ This supplies the value of the dog prop used in the HTML of this component. ❸ The goto function navigates to a page at a given path. ❹ The dog object has a message property when there is an error retrieving data for a given dog name, as is the case with “Spot”.