GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — move folders · jokoss92/fullstack-javascript@8e8f1a4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • chapter3
    • chapter3.md
    • media
      • weather-1.png
      • weather-2.png
  • code/03-weather
    • css
      • bootstrap.css.map
      • bootstrap.css
      • bootstrap.min.css.map
      • bootstrap.min.css
    • index.html
    • js
      • bootstrap.js
      • bootstrap.min.js
      • jquery.js
      • npm.js
      • umd
        • alert.js
        • button.js
        • carousel.js
        • collapse.js
        • dropdown.js
        • modal.js
        • popover.js
        • scrollspy.js
        • tab.js
        • tooltip.js
        • util.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎chapter3/chapter3.md‎

Copy file name to clipboardExpand all lines: chapter3/chapter3.md
+130-137Lines changed: 130 additions & 137 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -632,22 +632,33 @@ the main Message Board application covered in later chapters. The goal
632632
is to just illustrate the combination of jQuery, JSONP, and REST API
633633
technologies.
634634

635+
636+
The idea of this weather application is to show you an input field for
637+
the city name and buttons for metric and imperial systems. The first page of the Weather application has the input field for the city name and two buttons for metric and imperial forecasts (Figure 3-1).
638+
639+
![alt](media/weather-1.png)
640+
641+
***Figure 3-1.** Weather App has a text input field and two button for the forecasts.*
642+
643+
Once you enter the city name and click one of the buttons, the app will fetch the
644+
forecast from OpenWeatherMap. Depending on what button is pressed, the app will fetch the forecast in metric (C) or imperial (F) degrees. For example, I like in the heart of tech innovation, San Francisco and we use imperial F degrees here so my result will be similar to the one shown in Figure 3-2. The forecast will be for several days with a 3-hour difference between predictions.
645+
646+
![alt](media/weather-2.png)
647+
648+
***Figure 3-2.** Weather App show the forecast for San Francisco.*
649+
650+
635651
Note that this example uses [OpenWeatherMap
636652
API](http://openweathermap.org) 2.5. The API requires an authentication
637653
(an app ID) for REST calls. You can get the necessary keys at
638-
[`openweathermap.org/``appid`](http://openweathermap.org/appid). The API
654+
<http://openweathermap.org/appid>. The API
639655
documentation is available at
640-
[`openweathermap.org/``api`](http://openweathermap.org/api).
641-
642-
The idea of this weather application is to show you an input field for
643-
the city name and buttons for metric and imperial systems. Once you
644-
enter the city name and click one of the buttons, the app will fetch the
645-
forecast from OpenWeatherMap.
656+
<http://openweathermap.org/api>. If you are starting the weather app from the code folder of the repository for this book, then make sure you update the API key as you might have my API key there which might not work in the future.
646657

647-
In this example, we'll use jQuery's `$.``ajax``()` function. It has
658+
In this example, we'll use jQuery's `$.ajax()` function. It has
648659
the following syntax:
649660

650-
var request = $.ajax({
661+
const request = $.ajax({
651662
url: url,
652663
dataType: 'jsonp',
653664
data: {q: cityName, appid: appId, units: units},
@@ -678,7 +689,7 @@ the following parameters:
678689
There is also a chained method `.fail`, which has logic for what to do
679690
when the request has an error (i.e., it fails).
680691

681-
For more parameters and examples of the `ajax``(``)` function, go to
692+
For more parameters and examples of the `ajax()` function, go to
682693
[`api.jquery.com/``jQuery.ajax`](http://api.jquery.com/jQuery.ajax/).
683694

684695
To assign our function to a user-triggered event, we need to use the
@@ -688,7 +699,7 @@ To assign our function to a user-triggered event, we need to use the
688699
...
689700
}
690701

691-
`$(``'``#``btn``'``)` is a jQuery object that points to an HTML element
702+
`$('#btn')` is a jQuery object that points to an HTML element
692703
in the DOM with the `id` of `btn`.
693704

694705
To make sure that all of the elements we want to access and use are in
@@ -739,10 +750,10 @@ The idea is simple: We have button and event listeners to do something
739750
once a user clicks the buttons. The aforementioned buttons call the
740751
`prepareData``()` method. This is its definition:
741752

742-
var openWeatherAppId = 'GET-YOUR-KEY-AT-OPENWEATHERMAP',
753+
const openWeatherAppId = 'GET-YOUR-KEY-AT-OPENWEATHERMAP',
743754
openWeatherUrl = 'http://api.openweathermap.org/data/2.5/forecast'
744755

745-
var prepareData = function(units) {
756+
const prepareData = function(units) {
746757
var cityName = $('#city-name').val()
747758
if (cityName && cityName != ''){
748759
cityName = cityName.trim()
@@ -762,7 +773,7 @@ the browser gets the response from the OpenWeatherMap API. Needless to
762773
say, we must pass the city name, app ID, and units as follows:
763774

764775
function getData (url, cityName, appId, units) {
765-
var request = $.ajax({
776+
const request = $.ajax({
766777
url: url,
767778
dataType: 'jsonp',
768779
data: {
@@ -790,24 +801,16 @@ found is the same as the one we requested in the input box.
790801

791802
function fetchData (forecast) {
792803
console.log(forecast)
793-
var html = '',
804+
let html = '',
794805
cityName = forecast.city.name,
795806
country = forecast.city.country
796807

797808
Now we form the HTML by iterating over the forecast and concatenating
798809
the string:
799810

800-
html += '<h3> Weather Forecast for '
801-
+ cityName
802-
+ ', '
803-
+ country
804-
+ '</h3>'
811+
html += `<h3> Weather Forecast for ${cityName}, ${country}</h3>`
805812
forecast.list.forEach(function(forecastEntry, index, list){
806-
html += '<p>'
807-
+ forecastEntry.dt_txt
808-
+ ': '
809-
+ forecastEntry.main.temp
810-
+ '</p>'
813+
html += `<p>${forecastEntry.dt_txt}:${forecastEntry.main.temp}</p>`
811814
})
812815

813816
Finally, we get a jQuery object for the div with ID `log`, and inject
@@ -820,113 +823,106 @@ In a nutshell, there is a button element that triggers
820823
is `fetchData``()`. If you found that confusing, here's the full code of
821824
the `index.html` file:
822825

823-
<!DOCTYPE html>
824-
<html lang="en">
825-
<head>
826-
<link type="text/css" rel="stylesheet" href="css/bootstrap.css" />
827-
<script src="js/jquery.js" type="text/javascript"></script>
828-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
829-
<script>
830-
var openWeatherAppId = 'GET-YOUR-KEY-AT-OPENWEATHERMAP',
831-
openWeatherUrl = 'http://api.openweathermap.org/data/2.5/forecast'
832-
833-
var prepareData = function(units) {
834-
var cityName = $('#city-name').val()
835-
if (cityName && cityName != ''){
836-
cityName = cityName.trim()
837-
getData(openWeatherUrl, cityName, openWeatherAppId, units)
838-
}
839-
else {
840-
alert('Please enter the city name')
841-
}
842-
}
843-
844-
$(document).ready(function(){
845-
$('.btn-metric').click(function() {
846-
prepareData('metric')
847-
})
848-
$('.btn-imperial').click(function() {
849-
prepareData('imperial')
850-
})
851-
})
852-
853-
function getData (url, cityName, appId, units) {
854-
var request = $.ajax({
855-
url: url,
856-
dataType: 'jsonp',
857-
data: {
858-
q: cityName,
859-
appid: appId,
860-
units: units
861-
},
862-
jsonpCallback: 'fetchData',
863-
type: 'GET'
864-
}).fail(function(error){
865-
console.error(error)
866-
alert('Error sending request')
867-
})
868-
}
869-
870-
function fetchData (forecast) {
871-
console.log(forecast)
872-
var html = '',
873-
cityName = forecast.city.name,
874-
country = forecast.city.country
875-
876-
html += '<h3> Weather Forecast for '
877-
+ cityName
878-
+ ', '
879-
+ country
880-
+ '</h3>'
881-
forecast.list.forEach(function(forecastEntry, index, list){
882-
html += '<p>'
883-
+ forecastEntry.dt_txt
884-
+ ': '
885-
+ forecastEntry.main.temp
886-
+ '</p>'
887-
})
888-
889-
$('#log').html(html)
890-
}
891-
</script>
892-
</head>
893-
<body>
894-
895-
<div class="container">
896-
897-
<div class="row">
898-
<div class="span4 offset 3">
899-
<h2>Weather App</h2>
900-
<p>Enter city name to get the weather forecast</p>
901-
</div>
902-
<div class="span6 offset1"><input class="span4" type="text" placeholder="Enter the city name" id="city-name" value=""/>
903-
</div>
904-
905-
</div>
906-
<div class="row">
907-
<div class="span6 offset1"><input type="button" class="btn-primary btn btn-metric" value="Get forecast in metric"/>
908-
<div class="span6 offset1"><input type="button" class="btn-danger btn btn-imperial" value="Get forecast in imperial"/>
909-
</div>
910-
<div class="span3">
911-
<p id="info"></p>
912-
</div>
913-
</div>
914-
915-
<div class="row">
916-
<div class="span6 offset1">
917-
<div id="log">Nothing to show yet</div>
918-
</div>
919-
</div>
920-
921-
<div class="row">
922-
<hr/>
923-
<p>Azat Mardan (<a href="http://twitter.com/azat_co">@azat_co</a>)</p>
924-
</div>
925-
926-
</div>
927-
928-
</body>
929-
</html>
826+
```html
827+
<!DOCTYPE html>
828+
<html lang="en">
829+
<head>
830+
<link type="text/css" rel="stylesheet" href="css/bootstrap.css" />
831+
<script src="js/jquery.js" type="text/javascript"></script>
832+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
833+
<style type="text/css">
834+
.row {
835+
padding-top:1.5em;
836+
}
837+
</style>
838+
<script>
839+
const openWeatherAppId = 'GET-YOUR-KEY-AT-OPENWEATHERMAP',
840+
openWeatherUrl = 'http://api.openweathermap.org/data/2.5/forecast'
841+
842+
const prepareData = function(units) {
843+
// Replace loading image
844+
let cityName = $('#city-name').val()
845+
// Make ajax call, callback
846+
if (cityName && cityName != ''){
847+
cityName = cityName.trim()
848+
getData(openWeatherUrl, cityName, openWeatherAppId, units)
849+
}
850+
else {
851+
alert('Please enter the city name')
852+
}
853+
}
854+
$(document).ready(function(){
855+
$('.btn-metric').click(function() {
856+
prepareData('metric')
857+
})
858+
$('.btn-imperial').click(function() {
859+
prepareData('imperial')
860+
})
861+
862+
})
863+
function getData (url, cityName, appId, units) {
864+
const request = $.ajax({
865+
url: url,
866+
dataType: "jsonp",
867+
data: {q: cityName, appid: appId, units: units},
868+
jsonpCallback: "fetchData",
869+
type: "GET"
870+
}).fail(function(error){
871+
console.error(error)
872+
alert('Error sending request')
873+
})
874+
}
875+
function fetchData (forecast) {
876+
console.log(forecast)
877+
let html = '',
878+
cityName = forecast.city.name,
879+
country = forecast.city.country
880+
881+
html += `<h3> Weather Forecast for ${cityName}, ${country}</h3>`
882+
forecast.list.forEach(function(forecastEntry, index, list){
883+
html += `<p>${forecastEntry.dt_txt}:${forecastEntry.main.temp}</p>`
884+
})
885+
886+
$('#log').html(html)
887+
}
888+
</script>
889+
</head>
890+
<body>
891+
<div class="container">
892+
893+
<div class="row">
894+
<div class="span4 offset 3">
895+
<h2>Weather App</h2>
896+
<p>Enter city name to get the weather forecast</p>
897+
</div>
898+
<div class="span6 offset1"><input class="span4" type="text" placeholder="Enter the city name" id="city-name" value=""/>
899+
</div>
900+
901+
</div>
902+
<div class="row">
903+
<div class="span6 offset1">
904+
<input type="button" class="btn-primary btn btn-metric" value="Get forecast in metric"/>
905+
<div class="span6 offset1">
906+
<input type="button" class="btn-danger btn btn-imperial" value="Get forecast in imperial"/>
907+
</div>
908+
</div>
909+
910+
<div class="row">
911+
<div class="span6 offset1">
912+
<div id="log">Nothing to show yet</div>
913+
</div>
914+
</div>
915+
916+
<div class="row">
917+
<hr/>
918+
<p>Azat Mardan (<a href="http://twitter.com/azat_co">@azat_co</a>)</p>
919+
</div>
920+
921+
</div>
922+
923+
</body>
924+
</html>
925+
```
930926

931927
Try launching it and see if it works with or without the local HTTP
932928
server (just opening `index.html` in the browser). It should not work
@@ -935,11 +931,8 @@ can get `http-static` or `http-server` command-line tools as described
935931
in Chapter 2.
936932

937933
The source code is available in the
938-
0[`2-weather`](https://github.com/azat-co/fullstack-javascript/tree/master/2-weather)
939-
folder and on GitHub (
940-
https://github.com/azat-co/fullstack-javascript/tree/master/02-weather
941-
). There's a [screencast video on YouTube](http://bit.ly/1RKxyxA) which
942-
walks you through the implementation and demonstrates the app.
934+
[`03-weather`](https://github.com/azat-co/fullstack-javascript/tree/master/code/03-weather)
935+
folder and on GitHub (https://github.com/azat-co/fullstack-javascript/tree/master/code/03-weather). Don't forget that there's a [screencast video on YouTube](http://bit.ly/1RKxyxA) which walks you through the implementation and demonstrates the app.
943936

944937
This example was built with OpenWeatherMap API v2.5 and might not work
945938
with later versions. Also, you need the API key called app ID. You can
Collapse file

‎chapter3/media/weather-1.png‎

Copy file name to clipboard
74.2 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎chapter3/media/weather-2.png‎

Copy file name to clipboard
106 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎code/02-weather/css/bootstrap.css‎ ‎code/03-weather/css/bootstrap.css‎code/02-weather/css/bootstrap.css renamed to code/03-weather/css/bootstrap.css

Copy file name to clipboard
File renamed without changes.
Collapse file

‎code/02-weather/css/bootstrap.css.map‎ ‎code/03-weather/css/bootstrap.css.map‎code/02-weather/css/bootstrap.css.map renamed to code/03-weather/css/bootstrap.css.map

Copy file name to clipboard
File renamed without changes.
Collapse file

‎code/02-weather/css/bootstrap.min.css‎ ‎code/03-weather/css/bootstrap.min.css‎code/02-weather/css/bootstrap.min.css renamed to code/03-weather/css/bootstrap.min.css

Copy file name to clipboard
File renamed without changes.
Collapse file

‎code/02-weather/css/bootstrap.min.css.map‎ ‎code/03-weather/css/bootstrap.min.css.map‎code/02-weather/css/bootstrap.min.css.map renamed to code/03-weather/css/bootstrap.min.css.map

Copy file name to clipboard
File renamed without changes.
Collapse file

‎code/02-weather/index.html‎ ‎code/03-weather/index.html‎code/02-weather/index.html renamed to code/03-weather/index.html

Copy file name to clipboardExpand all lines: code/03-weather/index.html
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
}
1111
</style>
1212
<script>
13-
var openWeatherAppId = '2de143494c0b295cca9337e1e96b00e0',
13+
const openWeatherAppId = '6ea35fbcd51d5e917b7d8309292bc51c',
1414
openWeatherUrl = 'http://api.openweathermap.org/data/2.5/forecast'
1515

16-
var prepareData = function(units) {
16+
const prepareData = function(units) {
1717
// Replace loading image
18-
var cityName = $('#city-name').val()
18+
let cityName = $('#city-name').val()
1919
// Make ajax call, callback
2020
if (cityName && cityName != ''){
2121
cityName = cityName.trim()
@@ -35,7 +35,7 @@
3535

3636
})
3737
function getData (url, cityName, appId, units) {
38-
var request = $.ajax({
38+
const request = $.ajax({
3939
url: url,
4040
dataType: "jsonp",
4141
data: {q: cityName, appid: appId, units: units},
@@ -48,13 +48,13 @@
4848
}
4949
function fetchData (forecast) {
5050
console.log(forecast)
51-
var html = '',
51+
let html = '',
5252
cityName = forecast.city.name,
5353
country = forecast.city.country
5454

55-
html += '<h3> Weather Forecast for ' + cityName + ', ' + country + '</h3>'
55+
html += `<h3> Weather Forecast for ${cityName}, ${country}</h3>`
5656
forecast.list.forEach(function(forecastEntry, index, list){
57-
html += '<p>' + forecastEntry.dt_txt + ': ' + forecastEntry.main.temp + '</p>'
57+
html += `<p>${forecastEntry.dt_txt}:${forecastEntry.main.temp}</p>`
5858
})
5959

6060
$('#log').html(html)
Collapse file

‎code/02-weather/js/bootstrap.js‎ ‎code/03-weather/js/bootstrap.js‎code/02-weather/js/bootstrap.js renamed to code/03-weather/js/bootstrap.js

Copy file name to clipboard
File renamed without changes.
Collapse file

‎code/02-weather/js/bootstrap.min.js‎ ‎code/03-weather/js/bootstrap.min.js‎code/02-weather/js/bootstrap.min.js renamed to code/03-weather/js/bootstrap.min.js

Copy file name to clipboard
File renamed without changes.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.