|
| 1 | +goals do |
| 2 | + goal "Understand how the internet is made up of clients and servers." |
| 3 | + goal "Write some basic requests using JavaScript, jQuery and your browser's console." |
| 4 | + goal "Create a list and your first item." |
| 5 | +end |
| 6 | + |
| 7 | +overview do |
| 8 | + |
| 9 | + message <<-MARKDOWN |
| 10 | + |
| 11 | +So far, we've learned about JavaScript, HTML, and CSS. These are the languages |
| 12 | +your browser understands, and it uses them to give structure, style, and interactivity |
| 13 | +to your website. |
| 14 | + |
| 15 | +The problem, though, is that browsers don't remember anything. Every time you refresh |
| 16 | +a page, your browser reruns all of your CSS, HTML, and JavaScript. A browser, by itself, |
| 17 | +doesn't remember who your users are, or any of their information. |
| 18 | + |
| 19 | +But wait, we need our website to remember things! Specifically, we need our website |
| 20 | +to remember all the items on our list, and whether we've completed them. That's where |
| 21 | +a __server__ comes in. A server is a catch-all term for a web application that can be |
| 22 | +reached over the internet. They are often attached to __databases__ that store information |
| 23 | +about their users. If you make an __HTTP request__ with the correct __URL__ (like |
| 24 | +http://www.google.com) and __HTTP method__ (like GET or POST), the server will |
| 25 | +respond with helpful data. |
| 26 | + |
| 27 | +Servers are written in dozens of languages, including Java, PHP, and Ruby. |
| 28 | +For this exercise, we've already built a server (using Ruby on Rails), and its |
| 29 | +available at https://listalous.herokuapp.com/. If you want to learn more about how |
| 30 | +to write a server, take a look at our Rails curriculum! |
| 31 | + |
| 32 | +Browsers, like Chrome and Internet Explorer, are in the business of making requests |
| 33 | +to servers, and displaying the data they receive as a web page. Browsers are a |
| 34 | +type of __client__, which consume data. There are other types of clients, too. |
| 35 | +Your computer can be a client, using the curl command in the terminal. An iPhone |
| 36 | +is another type of client, and servers can even communicate with each other. |
| 37 | + |
| 38 | +Every time you refresh a web page, your browser is making another request to |
| 39 | +the server. On modern websites, browsers often make multiple requests to a server, |
| 40 | +depending on what the user is doing. For instance, Twitter doesn't load your entire |
| 41 | +feed at once – as you scroll down the page, it's making more and more requests! |
| 42 | + |
| 43 | +To accomplish that feat, websites can use JavaScript to make requests to servers, |
| 44 | +updating the page without the user having to refresh. This type of |
| 45 | +request is called an __AJAX__ request, which stands for __A__synchronous __J__avaScript |
| 46 | +__a__nd __X__ML. It's a technique that is used by most major websites, to provide |
| 47 | +a seamless experience to their users. |
| 48 | + |
| 49 | +In this lesson, we'll be making AJAX requests using our browser's console. |
| 50 | + MARKDOWN |
| 51 | +end |
| 52 | + |
| 53 | +steps do |
| 54 | + |
| 55 | + step do |
| 56 | + message <<-MARKDOWN |
| 57 | +Copy the following code into your browser's console, replacing 'YOUR-LIST-NAME-HERE' |
| 58 | +with your name of choice. Note: it has to be unique! I'd suggest using your github username. |
| 59 | + MARKDOWN |
| 60 | + |
| 61 | + source_code :javascript, <<-JAVASCRIPT |
| 62 | + $.ajax({ |
| 63 | + type: 'POST', |
| 64 | + url: "https://listalous.herokuapp.com/lists", |
| 65 | + data: { name: 'YOUR-LIST-NAME-HERE' } |
| 66 | + }) |
| 67 | + JAVASCRIPT |
| 68 | + |
| 69 | + message <<-MARKDOWN |
| 70 | +Now click over to your browser's network tab. It should look something like this. |
| 71 | + |
| 72 | +<img src='img/network_tab.png' alt="image of chrome's network tab."> |
| 73 | + |
| 74 | +Find the request to listalous.herokuapp.com. Was it successful? If not, why |
| 75 | +do you think it failed? Once you've successfully created a list, move on to the next step. |
| 76 | + MARKDOWN |
| 77 | + end |
| 78 | + |
| 79 | + step do |
| 80 | + message <<-MARKDOWN |
| 81 | +Now that we've created a list, let's create our list's first item. We're |
| 82 | +going to use jQuery's AJAX function again to do so. Copy the following |
| 83 | +code into your browser's console, replacing 'YOUR-LIST-NAME-HERE' with |
| 84 | +your list's name, and 'DESCRIPTION-OF-YOUR-ITEM' with your item's description. |
| 85 | + MARKDOWN |
| 86 | + |
| 87 | + source_code :javascript, <<-JAVASCRIPT |
| 88 | + $.ajax({ |
| 89 | + type: 'POST', |
| 90 | + url: "https://listalous.herokuapp.com/lists/YOUR-LIST-NAME-HERE/items", |
| 91 | + data: { description: 'DESCRIPTION-OF-YOUR-ITEM', completed: false } |
| 92 | + }) |
| 93 | + JAVASCRIPT |
| 94 | + |
| 95 | + message <<-MARKDOWN |
| 96 | +Check the network tab again. Was your request successful? If it was, take a look at what |
| 97 | +the server's response. You'll notice that the item has an id attribute now. This is how |
| 98 | +the server will uniquely identify your item in the future. |
| 99 | + MARKDOWN |
| 100 | + end |
| 101 | + |
| 102 | + step do |
| 103 | + message <<-MARKDOWN |
| 104 | +Finally, lets fetch our list from the server. Your list application will need to do this |
| 105 | +every time someone refreshes the page, so that it can load previously created items. Copy |
| 106 | +the following code into your browser's console, replacing 'YOUR-LIST-NAME-HERE' with your |
| 107 | +list's name. |
| 108 | + MARKDOWN |
| 109 | + |
| 110 | + source_code :javascript, <<-JAVASCRIPT |
| 111 | + $.ajax({ |
| 112 | + type: 'GET', |
| 113 | + url: "https://listalous.herokuapp.com/lists/YOUR-LIST-NAME-HERE/" |
| 114 | + }) |
| 115 | + JAVASCRIPT |
| 116 | + |
| 117 | + message <<-MARKDOWN |
| 118 | +Check the network tab one more time. What data did the server send back? If your |
| 119 | +request was successful, it should have returned all the data associated with your list: |
| 120 | +your list's name, as well as all of your list's items. This is data will be basis |
| 121 | +of your list application! |
| 122 | + MARKDOWN |
| 123 | + end |
| 124 | +end |
| 125 | + |
| 126 | +explanation do |
| 127 | + message <<-MARKDOWN |
| 128 | + |
| 129 | +That was a lot of copying and pasting! What did it all mean? |
| 130 | + |
| 131 | +### Adding a Server to the Equation. |
| 132 | + |
| 133 | +Remember, web pages have short memories. Whenever you refresh the page, all the HTML, |
| 134 | +CSS, and JavaScript on that page has to reload and rerun. A common way for a website |
| 135 | +to remember information about you (the fact that you have logged in before, your previous |
| 136 | +purchases, etc.) is by getting a server involved. A server, attached to a database, can |
| 137 | +persist information about your web site as users come and go. |
| 138 | + |
| 139 | +In this case, we have a server that can be reached over the internet at |
| 140 | +https://listalous.herokuapp.com/. It's attached to a database that |
| 141 | +stores all lists and items, that can fetched to make hundreds of independent to do lists. |
| 142 | + |
| 143 | +In step 1, we used jQuery's AJAX function to ask the server to make a |
| 144 | +new list in its database. It only did so if the list's name was unique; |
| 145 | +otherwise, how could the server tell your list apart from someone else's list? |
| 146 | + |
| 147 | +In step 2, we asked the server to create an item associated with your |
| 148 | +list. The server responded with information regarding this item, including an |
| 149 | +id, that we can use to identify that item later (if we want to mark it as complete, |
| 150 | +or delete it.) |
| 151 | + |
| 152 | +In step 3, we asked the server to return all information associated with |
| 153 | +our list, including all of the list's items. When we start building our list |
| 154 | +application, we will use this data to load all items on our list every time we |
| 155 | +refresh the page. |
| 156 | + |
| 157 | +We did all of this in the browser's console. In the next lesson, we'll integrate |
| 158 | +some of this code into our site, so it runs automatically whenever a user |
| 159 | +visits your web site. |
| 160 | + |
| 161 | + MARKDOWN |
| 162 | +end |
| 163 | + |
| 164 | + |
| 165 | +next_step "loading_items" |
0 commit comments