@@ -632,22 +632,33 @@ the main Message Board application covered in later chapters. The goal |
632 | 632 | is to just illustrate the combination of jQuery, JSONP, and REST API |
633 | 633 | technologies. |
634 | 634 |
|
| 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 | + |
| 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 | + |
| 647 | + |
| 648 | +***Figure 3-2.** Weather App show the forecast for San Francisco.* |
| 649 | + |
| 650 | + |
635 | 651 | Note that this example uses [OpenWeatherMap |
636 | 652 | API](http://openweathermap.org) 2.5. The API requires an authentication |
637 | 653 | (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 |
639 | 655 | 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. |
646 | 657 |
|
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 |
648 | 659 | the following syntax: |
649 | 660 |
|
650 | | - var request = $.ajax({ |
| 661 | + const request = $.ajax({ |
651 | 662 | url: url, |
652 | 663 | dataType: 'jsonp', |
653 | 664 | data: {q: cityName, appid: appId, units: units}, |
@@ -678,7 +689,7 @@ the following parameters: |
678 | 689 | There is also a chained method `.fail`, which has logic for what to do |
679 | 690 | when the request has an error (i.e., it fails). |
680 | 691 |
|
681 | | -For more parameters and examples of the `ajax``(``)` function, go to |
| 692 | +For more parameters and examples of the `ajax()` function, go to |
682 | 693 | [`api.jquery.com/``jQuery.ajax`](http://api.jquery.com/jQuery.ajax/). |
683 | 694 |
|
684 | 695 | 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 |
688 | 699 | ... |
689 | 700 | } |
690 | 701 |
|
691 | | -`$(``'``#``btn``'``)` is a jQuery object that points to an HTML element |
| 702 | +`$('#btn')` is a jQuery object that points to an HTML element |
692 | 703 | in the DOM with the `id` of `btn`. |
693 | 704 |
|
694 | 705 | 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 |
739 | 750 | once a user clicks the buttons. The aforementioned buttons call the |
740 | 751 | `prepareData``()` method. This is its definition: |
741 | 752 |
|
742 | | - var openWeatherAppId = 'GET-YOUR-KEY-AT-OPENWEATHERMAP', |
| 753 | + const openWeatherAppId = 'GET-YOUR-KEY-AT-OPENWEATHERMAP', |
743 | 754 | openWeatherUrl = 'http://api.openweathermap.org/data/2.5/forecast' |
744 | 755 |
|
745 | | - var prepareData = function(units) { |
| 756 | + const prepareData = function(units) { |
746 | 757 | var cityName = $('#city-name').val() |
747 | 758 | if (cityName && cityName != ''){ |
748 | 759 | cityName = cityName.trim() |
@@ -762,7 +773,7 @@ the browser gets the response from the OpenWeatherMap API. Needless to |
762 | 773 | say, we must pass the city name, app ID, and units as follows: |
763 | 774 |
|
764 | 775 | function getData (url, cityName, appId, units) { |
765 | | - var request = $.ajax({ |
| 776 | + const request = $.ajax({ |
766 | 777 | url: url, |
767 | 778 | dataType: 'jsonp', |
768 | 779 | data: { |
@@ -790,24 +801,16 @@ found is the same as the one we requested in the input box. |
790 | 801 |
|
791 | 802 | function fetchData (forecast) { |
792 | 803 | console.log(forecast) |
793 | | - var html = '', |
| 804 | + let html = '', |
794 | 805 | cityName = forecast.city.name, |
795 | 806 | country = forecast.city.country |
796 | 807 |
|
797 | 808 | Now we form the HTML by iterating over the forecast and concatenating |
798 | 809 | the string: |
799 | 810 |
|
800 | | - html += '<h3> Weather Forecast for ' |
801 | | - + cityName |
802 | | - + ', ' |
803 | | - + country |
804 | | - + '</h3>' |
| 811 | + html += `<h3> Weather Forecast for ${cityName}, ${country}</h3>` |
805 | 812 | 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>` |
811 | 814 | }) |
812 | 815 |
|
813 | 816 | 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 |
820 | 823 | is `fetchData``()`. If you found that confusing, here's the full code of |
821 | 824 | the `index.html` file: |
822 | 825 |
|
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 | +``` |
930 | 926 |
|
931 | 927 | Try launching it and see if it works with or without the local HTTP |
932 | 928 | 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 |
935 | 931 | in Chapter 2. |
936 | 932 |
|
937 | 933 | 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. |
943 | 936 |
|
944 | 937 | This example was built with OpenWeatherMap API v2.5 and might not work |
945 | 938 | with later versions. Also, you need the API key called app ID. You can |
|