Created
December 28, 2017 03:04
-
-
Save jcgregorio/eea793f335603808576d2d0fde6f8ffd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.customElements.define('subreddit-van', class extends HTMLElement { | |
// Only get callbacks when our 'name' attribute changes. | |
static get observedAttributes() { return ['name']; } | |
// Called when our 'name' attribute changes. | |
attributeChangedCallback(attr, oldValue, newValue) { | |
if (newValue === '') { | |
return | |
} | |
fetch(`https://www.reddit.com/r/${ newValue }/top.json?limit=5`).then(resp => { | |
resp.json().then(json => { | |
this.innerHTML=`<h2>${ newValue.toUpperCase() }</h2>`; | |
json.data.children.forEach(item => { | |
let ele = document.createElement('post-van'); | |
ele.item = item; | |
this.appendChild(ele); | |
}); | |
}); | |
}); | |
} | |
// Provide default content before the reddit content is loaded. | |
connectedCallback() { | |
this.textContent = 'Loading...'; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment