SheTech
  • SheTech JS bootcamp
  • Level 1 - awesome tasks
    • Introduction to Awesome Tasks
    • Step 1: HTML
    • Step 2: playing with the DOM
    • Step 3: add a new task
    • Step 4: dynamic list of tasks
    • Step 5: adding new items
    • Step 6: removing items
    • Step 7: add some style
    • Step 8: marking items as done
    • Step 9: saving items in the localstorage
    • Step 10: adding filters and bulk actions
    • Step 11: add counters
    • From Awesome Task to the next big thing!
  • Level 2 - timeline
    • Live Timeline
    • Step 1: basic HTML page
    • Step 2: add util library
    • Step 3: show all posts
    • Step 4: add form
    • Step 5: collect form data
    • Step 6: create a new post
    • Step 7: validating posts
    • Step 8: refactoring
    • Step 9: refresh button
    • Step 10: auto refresh
  • Code Editors
  • Useful resources
  • API Reference
  • Glossary
Powered by GitBook
On this page

Was this helpful?

  1. Level 2 - timeline

Step 2: add util library

PreviousStep 1: basic HTML pageNextStep 3: show all posts

Last updated 4 years ago

Was this helpful?

Add the utility library

<script type="text/javascript" src="utils/utils.js"></script>

Remove the static object postToAdd with an actual call.

note that we will print only the first item

fetch(CHAT_URI)
  .then(function(response) {
    return response.json();
  })
  .then(function(posts) {
    var postToAdd = toArray(posts)[0];
    drawElement(postToAdd);
  });

What does this code do?

This code fragment looks pretty dense; we can phrase it as follows:

Retrieve the information at the address, prepare them for reading, take the first information piece and draw it

Now let's analyze it bit by bit (pun intended :) )

1.1 — thefetch() function

fetch(CHAT_URI)

fetch() is a function that accepts some parameters, the first one is always an address, in this particular case CHAT_URI.

CHAT_URI is a string inside the file utils/utils.js, it's the specific address of our json on the database

1.2 — The first .then()

  .then(function(response) {
    return response.json();
  })

This is a dirty implementation detail.

This step is executed as soon as fetch() completes, hence it's named then().

We can manipulate the response with a function. A function always need a return statement, here we instrument the function to pass the response to the next step in some handy (json) format.

1.2 — The second .then()

  .then(function(posts) {
    var postToAdd = toArray(posts)[0];
    drawElement(postToAdd);
  });

The first line inside the function is a variable assignment, here we transform the posts from an object to an array, for convenience, and we take the first element (please note that the first element has index 0)

In the second line we can find the same function that we used in the previous step.

What we just did:

  • We used fetch() to retrieve the json at this address: FIREBASE_JSON

  • We manipulated the response of fetch(), using then(), in something that we can use

  • We isolated the first post and displayed it in the page using drawElement()

ajax