{"id":3808,"date":"2023-07-18T02:12:50","date_gmt":"2023-07-18T02:12:50","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3808"},"modified":"2025-10-21T03:17:44","modified_gmt":"2025-10-21T03:17:44","slug":"exercise-dawgs","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-nine\/exercise-dawgs\/","title":{"rendered":"Exercise: Dawgs"},"content":{"rendered":"\n<p>We are going to explore the process of capturing and displaying information from The Dog Api, a site that lists and shares pictures of zillions of dog breeds. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Check out the API<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>To begin, take a look at the following URL:&nbsp;<a href=\"https:\/\/dog.ceo\/api\/breed\/spaniel\/list\">https:\/\/dog.ceo\/api\/breed\/spaniel\/list<\/a>&nbsp;. When you visit the URL you should see an object containing a list of spaniels just like this one:<\/li>\n\n\n\n<li>Now visit&nbsp;<a href=\"https:\/\/dog.ceo\/api\/breed\/spaniel\/images\">https:\/\/dog.ceo\/api\/breed\/spaniel\/images<\/a>&nbsp;where you will see an object with the same structure that contains a long list of spaniel image urls. The dog api contains MANY more dogs and images, ranging from Airedales to Whippets, but let&#8217;s focus on Spaniels to keep the pack size manageable.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Check out this example<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>To get started download&nbsp;<a href=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/10\/Dawgs_start.zip\" data-type=\"attachment\" data-id=\"4678\">dawgs.zip<\/a>&nbsp;and move both files into your work folder. Open both and take a look.<\/li>\n\n\n\n<li>In the HTML:\n<ul class=\"wp-block-list\">\n<li>You\u2019ll see that we import <strong>Vue<\/strong> and <strong>Axios<\/strong> (a library that helps Vue get data from APIs).\n<ul class=\"wp-block-list\">\n<li>The &lt;div id=&#8221;app&#8221;&gt; section uses v-on:click to call the function showme() when a button is clicked.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>In Dawgs.js, we create a Vue instance.\n<ul class=\"wp-block-list\">\n<li>The data() function must <strong>return<\/strong> an object for Axios to work properly.<\/li>\n\n\n\n<li>The mounted() section acts like document.ready() in jQuery\u2014it waits until the page is loaded before running any code.<\/li>\n\n\n\n<li>The async and await keywords tell the browser to <strong>wait<\/strong> for the data to load before continuing (otherwise, Vue would try to use data that doesn\u2019t exist yet).<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Get some data from the API<\/h3>\n\n\n\n<p>For the best troubleshooting experience, make sure you know <a href=\"https:\/\/www.youtube.com\/watch?v=P8HYosJ1PvQ\" target=\"_blank\" rel=\"noreferrer noopener\">how to view your Console<\/a>. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, let&#8217;s use the v-on:click function to talk to a method that we will build to talk to the buttons. <pre class=\"wp-block-code\"><code>&lt;button v-on:click=\"showme('cocker')\"&gt;Cocker&lt;\/button&gt;\n&lt;button v-on:click=\"showme('brittany')\"&gt;Brittany&lt;\/button&gt;<\/code><\/pre><\/li>\n\n\n\n<li>Inside the methods section, create the showme() function that logs the breed name to the console. This tests that our buttons are working. You should see &#8220;You clicked cocker&#8221; and &#8220;You clicked spaniel&#8221; in the Console when you inspect. <pre class=\"wp-block-code\"><code>methods: {<br>  showme(breed) {<br>    console.log(\"You clicked \" + breed);<br>  }<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Turn showme() into an <strong>async<\/strong> function, use <strong>axios.get()<\/strong> to fetch data, and print it to the console. In the console, you\u2019ll see a big object with lots of image URLs.<pre class=\"wp-block-code\"><code>methods: {<br>  async showme(breed) {<br>    const response = await axios.get(`https:\/\/dog.ceo\/api\/breed\/spaniel\/${breed}\/images`);<br>    console.log(response.data);<br>  }<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Now that we know the data loads, we need to store it in Vue so that we can show it on the webpage. Store the response data in this.info, and use the message array to populate moreData. <pre class=\"wp-block-code\"><code>methods: {<br>  async showme(breed) {<br>    const response = await axios.get(`https:\/\/dog.ceo\/api\/breed\/spaniel\/${breed}\/images`);<br>    <strong>this.info = response.data;<\/strong><br>    <strong>this.moreData = this.info.message;<br>    console.log(this.moreData);<\/strong><br>  }<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Now that moreData holds our image URLs, let\u2019s loop through them in the HTML. <br>Replace your placeholder &lt;div&gt; with this block that loops through moreData and displays each image: <pre class=\"wp-block-code\"><code>&lt;div v-for=\"theBreed in moreData\" :key=\"theBreed\"&gt;\n  &lt;a :href=\"theBreed\" target=\"_blank\"&gt;\n    &lt;img :src=\"theBreed\" alt=\"Spaniel image\"&gt;\n  &lt;\/a&gt;\n&lt;\/div&gt;<\/code><\/pre><\/li>\n\n\n\n<li>Clicking the <strong>Cocker<\/strong> or <strong>Brittany<\/strong> button should now display a wall of adorable spaniel photos! Now, let&#8217;s use mounted() to load one breed automatically when the page loads. Add a mounted() section that runs once when Vue is ready. <pre class=\"wp-block-code\"><code>data() {<br>      return {<br>        info: null,<br>        moreData: []<br>      }<br>    },<br><br>    mounted() {<br>      this.showme('cocker');<br>    },<br><br>    methods: {<\/code><\/pre><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">SlideShows<\/h3>\n\n\n\n<p>To begin, save your html file as slideshow.html, your js file as slideshow.js and connect the two. This way you don&#8217;t have to overwrite your previous work.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Add new data properties. We\u2019ll track which image we\u2019re showing (selector) and which dog breed we\u2019re currently viewing (whichDawg). <pre class=\"wp-block-code\"><code>data() {\n  return {\n    info: null,\n    moreData: [],\n    selector: 0,\n    whichDawg: ''\n  }\n}<\/code><\/pre><\/li>\n\n\n\n<li>Create the slider() method. The slider() method will change the currently displayed image by updating selector. Then we\u2019ll use it inside showme() to start the slideshow automatically. Add this below the existing showme() method. <pre class=\"wp-block-code\"><code>slider() {\n  this.selector++;\n  if (this.selector &gt;= this.moreData.length) {\n    this.selector = 0;\n  }\n}<\/code><\/pre><\/li>\n\n\n\n<li>Now we\u2019ll make showme() remember which breed is being shown and call the new slider() function. Update showme(): <pre class=\"wp-block-code\"><code>async showme(breed) {\n  const response = await axios.get(`https:\/\/dog.ceo\/api\/breed\/spaniel\/${breed}\/images`);\n  this.info = response.data;\n  this.moreData = this.info.message;\n  this.whichDawg = breed;\n  this.selector = 0;\n  this.slider();\n}<\/code><\/pre><\/li>\n\n\n\n<li>Update the HTML to include slideshow buttons and the current image. I threw in a fun extra styling decision, an all-caps title. <pre class=\"wp-block-code\"><code>&lt;div id=\"app\"&gt;<br>    &lt;button v-on:click=\"showme('cocker')\"&gt;Cocker&lt;\/button&gt;<br>    &lt;button v-on:click=\"showme('brittany')\"&gt;Brittany&lt;\/button&gt;<br><br>    &lt;div v-if=\"moreData.length &gt; 0\"&gt;<br>      &lt;h3&gt;{{ whichDawg.toUpperCase() }} SPANIEL&lt;\/h3&gt;<br>      &lt;a :href=\"moreData[selector]\" target=\"_blank\"&gt;<br>        &lt;img :src=\"moreData[selector]\" alt=\"Current spaniel image\"&gt;<br>      &lt;\/a&gt;<br>      &lt;br&gt;<br>      &lt;button v-on:click=\"slider()\"&gt;Next&lt;\/button&gt;<br>    &lt;\/div&gt;<br>  &lt;\/div&gt;<\/code><\/pre><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>We are going to explore the process of capturing and displaying information from The Dog Api, a site that lists and shares pictures of zillions of dog breeds. Step 1: Check out the API Step 2: Check out this example Step 3: Get some data from the API For the best troubleshooting experience, make sure &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-nine\/exercise-dawgs\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Dawgs<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3700,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3808","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/comments?post=3808"}],"version-history":[{"count":13,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3808\/revisions"}],"predecessor-version":[{"id":4696,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3808\/revisions\/4696"}],"up":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3700"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/media?parent=3808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}