Skip to content

Instantly share code, notes, and snippets.

@stardustxx
Last active December 25, 2017 23:03
Show Gist options
  • Save stardustxx/4f6d55d3660aa2ad230977ee96a1052e to your computer and use it in GitHub Desktop.
Save stardustxx/4f6d55d3660aa2ad230977ee96a1052e to your computer and use it in GitHub Desktop.
DOMParser
async function getData() {
let url = "your api url";
let data = await (await fetch(url)).json();
let productReviewTemplate = $('#template').html();
let template = _.template(productReviewTemplate);
$("#container").append(template({productReviews : data}));
}
async function getData() {
let url = "your api url";
let data = await (await fetch(url)).json();
let elem = document.createElement("div");
let content = document.createTextNode(data.content);
elem.appendChild(content);
let container = document.querySelector("#container");
if (container) {
container.appendChild(elem);
}
}
async function getData() {
let url = "your api url";
let reviews = await (await fetch(url)).json();
let reviewTemplateHtml = document.querySelector("#product-review-tpl");
if (reviewTemplateHtml) {
let template = _.template(reviewTemplateHtml.innerHTML);
let productReviews = document.querySelector(".product-reviews .list");
if (productReviews) {
let elems = new DOMParser().parseFromString(template({reviews}), "text/html");
if (elems) {
for (let elem of elems.body.childNodes) {
productReviews.appendChild(elem);
}
}
}
}
}
<script type="text/template" id="template">
<% _.each(productReviews, function(review){ %>
<div>
<%= review.content %>
</div>
<% }) %>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment