Skip to content

Instantly share code, notes, and snippets.

@jcgregorio
Created December 28, 2017 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcgregorio/eea793f335603808576d2d0fde6f8ffd to your computer and use it in GitHub Desktop.
Save jcgregorio/eea793f335603808576d2d0fde6f8ffd to your computer and use it in GitHub Desktop.
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