Giter Club home page Giter Club logo

libraryspa's Introduction

#AJAX & JSON APIs

Lot's of acronyms here, but you should be able to appreciate the importance of each by the end of the lesson

##Objectives By the end of the lesson, you should be able to...

  • Design a RESTful JSON API
  • Retrieve Data from the API
  • Use JQuery to append data to HTML elements
  • Contrast how Handlebars can refactor your code
  • Send data to the API and, on success, maintain state between the DB and the DOM

##AJAX It allows us to easily send and recieve data to and from our server without reloading the page.

  • Go to an example JSBin project
  • GOAL: understand the code

##JSON API We want to be able to respond to our AJAX requests with the exact data it needs

  • GOAL: Compare the differences in the instruments vs. the books controller

##SPA CRUD Single-page app's (SPAs) are made possible due to asynchronicity. They provide fluid user experiences akin to native applications.

Discussion Questions

  • Can you articulate the differences between a typical API and a JSON API
  • When most people refer to "API's" do you think they are refering to
  • What are the implications of have a centralized API?

##Exercise

We will work in pairs to leverage our JSON API and create a SPA.

###Setup

  • Run bundle
  • Run rake db:create db:migrate db:seed

###Fill the library

  1. Create a loadBooks function in the books.js file that fetches all the the books in the database

For help, you can always reference the JSBin example on hitting the OMDB API with AJAX.

  • Get an AJAX GET request to /books working
  • When the responce is recieved (aka done) you will need to append the data to the page:
    • Create an empty unordered list
    • Loop through the AJAX response and on each iteration create an li that contains the book's title and author. The format should be "Book by Author"
    • Append the li to the ul
    • After all the li's have been appended to the ul, you can then append the ul to
  1. Have the function execute once the document is ready

###Refactor Our Code with Handlebars

  • Add gem 'handlebars_assets'
  • Run bundle
  • In your assets/javascripts create a folder called templates
  • Inside templates create a folder called books
  • Inside books create a file called book.hbs
  • In the loadBooks function reference your template using `HandlebarsTemplates"books/book"
    • HandlebarsTemplates is a variable that now exists in the global namespace
    • It is a object that can take, as a key, the path the template we want to use
    • We then can pass in the object we want to use in our template

###Add to our Library

  • Create two input forms and a button
    • Add onclick=Library.addBook() on the button
  • Build on the function called Library.addBook
    • Send a post request, using AJAX to /books so that you can add a book
  • Build on the Library.addBook function so that you are sending the input values in your AJAX request
    • On done clear the input values and call Library.loadBooks() to reload your books
    • You may see duplicate lists. If so, edit the loadBooks function so that the first thing it does is clear out the current books on the page.

###Bonus

  • Add a delete button next to each book that sends an AJAX request to delete the book you are targeting
  • Hint: you will need to have access to the book's Id.
    • Tip: try adding the book's Id into a data-atr tag in the li element

libraryspa's People

Contributors

ilias-t avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.