Skip to content

Instantly share code, notes, and snippets.

@rkpattnaik780
Created July 7, 2021 18:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rkpattnaik780/acc683d3796102c26c1abb03369e31f8 to your computer and use it in GitHub Desktop.
Save rkpattnaik780/acc683d3796102c26c1abb03369e31f8 to your computer and use it in GitHub Desktop.
const template = document.createElement('template');
template.innerHTML = `
<div>
Hello Weather App
</div>
`
class WeatherCard extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ 'mode': 'open' });
this._shadowRoot.appendChild(template.content.cloneNode(true));
}
connectedCallback() {
var xmlHttp = new XMLHttpRequest();
const url = `http://api.openweathermap.org/data/2.5/weather?lat=${this.latitude}&lon=${this.longitude}&appid=b86d21440d8c9a110912a2eb0845abb4`
xmlHttp.open( "GET", url , false );
xmlHttp.send( null );
console.log(xmlHttp.responseText)
this.$card = this._shadowRoot.querySelector('div');
let responseObj = JSON.parse(xmlHttp.responseText);
let $townName = document.createElement('p');
$townName.innerHTML = `Town: ${responseObj.name}`;
this._shadowRoot.appendChild($townName);
let $temperature = document.createElement('p');
$temperature.innerHTML = `${parseInt(responseObj.main.temp - 273)} &deg;C`
this._shadowRoot.appendChild($temperature);
}
get longitude() {
return this.getAttribute('longitude');
}
get latitude() {
return this.getAttribute('latitude');
}
}
window.customElements.define('weather-card', WeatherCard);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment