javascript development
play

JAVASCRIPT DEVELOPMENT Sasha Vodnik, Instructor AJAX & APIS 2 - PowerPoint PPT Presentation

JAVASCRIPT DEVELOPMENT Sasha Vodnik, Instructor AJAX & APIS 2 HELLO! 1. Pull changes from the svodnik/JS-SF-9-resources repo to your computer 2. Open the 10-ajax-apis/starter-code folder in your code editor JAVASCRIPT DEVELOPMENT AJAX


  1. JAVASCRIPT DEVELOPMENT Sasha Vodnik, Instructor

  2. AJAX & APIS 2 HELLO! 1. Pull changes from the svodnik/JS-SF-9-resources repo to your computer 2. Open the 10-ajax-apis/starter-code folder in your code editor

  3. JAVASCRIPT DEVELOPMENT AJAX & APIS

  4. AJAX & APIS 4 LEARNING OBJECTIVES At the end of this class, you will be able to ‣ Use event delegation to manage dynamic content in jQuery. ‣ Identify all the HTTP verbs & their uses. ‣ Describe APIs and how to make calls and consume API data. ‣ Access public APIs and get information back. ‣ Implement an Ajax request with vanilla JS. ‣ Implement a jQuery Ajax client for a simple REST service. ‣ Reiterate the benefits of separation of concerns – API vs. Client.

  5. AJAX & APIS 5 AGENDA ‣ Event delegation ‣ APIs ‣ HTTP ‣ Ajax using Fetch ‣ Separation of concerns ‣ Ajax & jQuery

  6. AJAX & APIS WEEKLY OVERVIEW WEEK 6 Advanced jQuery / Ajax & APIs WEEK 7 Asynchronous JavaScript & Callbacks / Advanced APIs HOLIDAY WEEK — NO CLASS! WEEK 8 Project 2 Lab / Closures & the module pattern

  7. AJAX & APIS 7 EXIT TICKET QUESTIONS 1. Should I focus on vanilla JS or on jQuery? Is it important to master vanilla JS before digging in on jQuery?

  8. JQUERY EVENT DELEGATION

  9. ADVANCED JQUERY & TEMPLATING 9 WITHOUT EVENT DELEGATION add an event listener to each li in the DOM 3. add a new list item 1. load page 2. set event listener 
 on list items $(‘li’).on(‘click’,function(){ addClass(‘selected’) }); click event •item1 click event •item1 •item1 click event •item2 •item2 •item2 click event click event •item3 click event •item3 •item3 •item4 click event is not automatically applied to the new li element

  10. ADVANCED JQUERY & TEMPLATING 10 WITH EVENT DELEGATION 3. add a new list item 1. load page 2. set event listener 
 on parent of list items new argument selector ‘li’ added to changed from ‘li’ to ‘ul’ on() method click event •item1 $(‘ul’).on(‘click’, ‘li’, function(){ •item1 addClass(‘selected’) click event •item2 •item2 }); add an event click event •item3 •item3 listener to the ul click event •item4 element that click event •item1 applies to all of its •item2 click event li descendants click event IS automatically click event •item3 applied to the new li element!

  11. INTRO TO JQUERY LET'S TAKE A CLOSER LOOK

  12. EXERCISE - EVENT DELEGATION OBJECTIVE ‣ Use event delegation to manage dynamic content. LOCATION ‣ starter-code > 1-best-practices-exercise TIMING EXERCISE 10 min 1. Return to main.js in your editor and complete items 4a and 4b. 2. In your browser, reload index.html and verify that when you add a new item to the list, its “cross off” link works. 3. BONUS: When the user mouses over each item, the item should turn grey. Don't use CSS hovering for this. 4. BONUS: Add another link, after each item, that allows you to delete the item.

  13. JQUERY ATTACHING MULTIPLE EVENTS WITH A SINGLE ON() STATEMENT

  14. ADVANCED JQUERY & TEMPLATING 14 ATTACHING MULTIPLE EVENTS WITH A SINGLE .ON() STATEMENT ‣ We could write a separate .on() statement for each event on an element: var $listElement = $('#contents-list'); $listElement.on('mouseenter', 'li', function(event) { $(this).siblings().removeClass('active'); $(this).addClass('active'); }); $listElement.on('mouseleave', 'li', function(event) { $(this).removeClass('active'); });

  15. ADVANCED JQUERY & TEMPLATING 15 ATTACHING MULTIPLE EVENTS WITH A SINGLE .ON() STATEMENT ‣ Grouping all the events for an element in a single .on() statement makes our code more organized and is faster var $listElement = $('#contents-list'); $listElement.on('mouseenter mouseleave', 'li', function(event) { if (event.type === 'mouseenter') { $(this).siblings().removeClass('active'); $(this).addClass('active'); } else if (event.type === 'mouseleave') { $(this).removeClass('active'); } });

  16. INTRO TO JQUERY LET'S TAKE A CLOSER LOOK

  17. EXERCISE - ATTACHING MULTIPLE EVENTS LOCATION ‣ starter-code > 2-multiple-events-exercise TIMING EXERCISE 5 min 1. In your browser, open index.html. Move the mouse over each list item and verify that the sibling items turn gray. 2. In your editor, open main.js and refactor the two event listeners near the bottom of the file into a single event listener for multiple events. 3. In your browser, reload index.html and verify that the functionality is unchanged.

  18. AJAX & APIS

  19. ACTIVITY TYPE OF EXERCISE ‣ Individual/Partner TIMING 3 min 1. Think about how you could use one or more sources of EXERCISE web data in an app. 2. Write a description or sketch a schematic of your app on your desk.

  20. 20 AJAX & APIS APIs

  21. AJAX & APIS 21 WEB SERVICES Your app Web service request for data response containing data your app can now incorporate data from the web service

  22. AJAX & APIS 22 WEB SERVICES

  23. AJAX & APIS 23 API = application programming interface

  24. AJAX & APIS 24 APIS IN THE REAL WORLD ‣ Most APIs are unique, like separate languages ‣ APIs for ‣ devices (iPhone) ‣ operating systems (macOS) ‣ JavaScript libraries (jQuery API) ‣ services (Slack)

  25. AJAX & APIS 25 WEB SERVICES

  26. AJAX & APIS 26 ENDPOINTS ‣ Addresses (URLs) that return data (JSON) instead of markup (HTML)

  27. AJAX & APIS 27 WHAT WE NEED TO KNOW TO USE AN API HOW TO HOW TO TERMS OF MAKE A UNDERSTAND SERVICE REQUEST RESPONSE

  28. AJAX & APIS 28 AN API MIGHT REQUIRE AUTHENTICATION Your app Web service request for data “wait, who is this?

  29. AJAX & APIS 29 YOUR APP MIGHT EXPERIENCE A DELAYED RESPONSE

  30. AJAX & APIS 30 YOUR REQUEST MAY RESULT IN AN ERROR Your app Web service request for data “sorry, something is wrong”

  31. AJAX & APIS 31 REST (representational state transfer) ‣ architectural style of web applications ‣ transfers a request representation of the state of a server resource to the client response

  32. AJAX & APIS 32 RESTful API ‣ adheres to REST architecture ‣ uses ‣ a base URL ‣ an Internet media type (such as JSON) ‣ standard HTTP methods

  33. 33 AJAX & APIS HTTP

  34. AJAX & APIS 34 HTTP (hypertext transfer protocol) ‣ System of rules for how web pages are transmitted between computers ‣ Defines the format of messages passed between HTTP clients and HTTP servers

  35. AJAX & APIS 35 HTTP (hypertext transfer protocol) ‣ A client sends a request to a server. request

  36. AJAX & APIS 36 HTTP (hypertext transfer protocol) ‣ A server sends a response back to a client. request response

  37. AJAX & APIS 37 HTTP REQUEST AND RESPONSE 1. Browser Request 2. Web Server Finds File 1 KB GET/index.html HTTP/1.1 /var/www/…/index.html read file 3. Server Response 4. Browser Displays Page HTTP/1.x 200 OK 100 KB <html>…<html>

  38. AJAX & APIS 38 HTTP (hypertext transfer protocol) HTTP client HTTP server Web service app that responds web server web browser to HTTP requests software

  39. AJAX & APIS 39 HTTP REQUESTS IN EVERYDAY LIFE resource path query protocol host http://www.domain.com/path/to/resource?a=b&x=y

  40. AJAX & APIS 40 HTTP REQUEST STRUCTURE [http request method] [URL] [http version] [list of headers] blank line [request body] optional

  41. AJAX & APIS 41 HTTP REQUEST METHODS (“HTTP VERBS”) GET Retrieve a resource Most widely used POST Create a resource Update an existing resource (use PATCH instead of PUT , which replaces) DELETE Delete a resource HEAD Retrieve the headers for a resource

  42. LET'S TAKE A CLOSER LOOK

  43. AJAX & APIS 43 HTTP REQUEST AND RESPONSE 1. Browser Request 2. Web Server Finds File 1 KB GET/index.html HTTP/1.1 /var/www/…/index.html read file 3. Server Response 4. Browser Displays Page HTTP/1.x 200 OK 100 KB <html>…<html>

  44. AJAX & APIS 44 HTTP RESPONSE STRUCTURE [http version] [status] [reason] [list of headers] blank line [response body] usually HTML, JSON, etc

  45. LET'S TAKE A CLOSER LOOK

  46. AJAX & APIS 46 HTTP STATUS CODES

  47. AJAX & APIS 47 HTTP STATUS CODES 200 Okay 301 Moved permanently 302 Moved temporarily 400 Bad request 403 Forbidden 404 Not found 500 Internal server error

  48. LET'S TAKE A CLOSER LOOK

  49. 49 AJAX & APIS Ajax

  50. AJAX & APIS 50 Ajax A synchronous J avaScript A nd X or JSON! ML

  51. AJAX & APIS 51 Fetch = Ajax requests in vanilla JavaScript fetch( url ).then(function( response ) { // check if request was successful }).then(function( response ) { // do something with the response });

  52. LET'S TAKE A CLOSER LOOK

Recommend


More recommend