Skip to content

Instantly share code, notes, and snippets.

@dosandk
Created September 1, 2021 11:49
Show Gist options
  • Select an option

  • Save dosandk/2d4316e98cd29aab22a1eb48140ccf29 to your computer and use it in GitHub Desktop.

Select an option

Save dosandk/2d4316e98cd29aab22a1eb48140ccf29 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="UTF-8">
<body></body>
<div id="root"></div>
<script>
class Component {
json;
constructor(data = []) {
this.data = data;
this.getData()
.then(() => {
this.update();
});
this.render();
}
getData () {
return fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => {
this.json = json;
})
}
update () {
const pre = this.element.querySelector('pre');
pre.innerHTML = JSON.stringify(this.json, null, 2);
}
render () {
const element = document.createElement('div');
element.innerHTML = `
<h1>Hello, <span style="color: green">World!</span>
<pre>
</pre>
</h1>
`;
this.element = element;
}
}
const obj = new Component();
root.append(obj.element);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment