From f4fb2b3a0d1240bc06e9b07457fa48474da7e19c Mon Sep 17 00:00:00 2001 From: Idan magled Date: Sat, 20 Nov 2021 13:52:51 +0200 Subject: [PATCH 1/2] - Some small refactor. --- app.js | 73 +++++++++++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/app.js b/app.js index b331d64..ed90425 100644 --- a/app.js +++ b/app.js @@ -1,48 +1,39 @@ -const appid = ''; -let weatherData; +const appid = 'eb98981901058b162b2802e1ddd392b9'; +//You gonna need this for caching. +// let globalWeatherData; -$.getJSON("./il.cities.json", function (data) { - let items = []; - $.each(data, function (key, val) { - items.push(""); - }); - - $("", { + "class": "my-city-list", + html: data.map((item) => "").join("") + }).appendTo("#forecast"); + }); } From 65ba6f399968b4e4cfb71757eda49521557f5d30 Mon Sep 17 00:00:00 2001 From: Idan magled Date: Sat, 20 Nov 2021 14:19:16 +0200 Subject: [PATCH 2/2] - Some more refactor. --- app.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index ed90425..f722c1b 100644 --- a/app.js +++ b/app.js @@ -5,21 +5,20 @@ const appid = 'eb98981901058b162b2802e1ddd392b9'; getInitData(); $('#forecast').on('change', () => { - let selectedCity = $('.my-city-list option').filter(':selected').text(); - $.getJSON('https://api.openweathermap.org/data/2.5/weather?', `q=${selectedCity}&appid=${appid}&units=metric`, function (mWeatherData, textStatus) { + $.getJSON('https://api.openweathermap.org/data/2.5/weather?', `q=${$('.my-city-list option').filter(':selected').text()}&appid=${appid}&units=metric`, function (mWeatherData, textStatus) { (textStatus !== 'success') ? alert('Can\'t get data') : populateWeatherData(mWeatherData) }); }) function populateWeatherData(weatherData) { // globalWeatherData = weatherData - $('[data-city]').html(weatherData.name + ", " + weatherData.sys.country); - $('[data-feel]').html('feels like\n ' + weatherData.main.feels_like + "°c"); - $('[data-humidity]').html('Humidity\n ' + weatherData.main.humidity + '%'); - $('[data-temp]').html('Temp\n ' + weatherData.main.temp + "°c"); - $('[data-maxTemp]').html('max\n ' + weatherData.main.temp_max + "°c"); - $('[data-minTemp]').html('min\n ' + weatherData.main.temp_min + "°c"); - $('[data-weather]').html('Info: ' + weatherData.weather[0].description); + $('[data-city]').text(weatherData.name + ", " + weatherData.sys.country); + $('[data-feel]').text('feels like\n ' + weatherData.main.feels_like + "°c"); + $('[data-humidity]').text('Humidity\n ' + weatherData.main.humidity + '%'); + $('[data-temp]').text('Temp\n ' + weatherData.main.temp + "°c"); + $('[data-maxTemp]').text('max\n ' + weatherData.main.temp_max + "°c"); + $('[data-minTemp]').text('min\n ' + weatherData.main.temp_min + "°c"); + $('[data-weather]').text('Info: ' + weatherData.weather[0].description); $('[data-wind]').html(`Wind: ${setDirection(weatherData.wind.deg)}(${weatherData.wind.deg}&weatherData.wind.deg)
at speed: ${weatherData.wind.speed} [Km/h]`); } @@ -31,9 +30,9 @@ function setDirection(deg) { function getInitData() { $.getJSON("./il.cities.json", function (data) { - $("", { "class": "my-city-list", html: data.map((item) => "").join("") - }).appendTo("#forecast"); + })); }); }