Last active
December 25, 2017 23:03
-
-
Save stardustxx/4f6d55d3660aa2ad230977ee96a1052e to your computer and use it in GitHub Desktop.
DOMParser
This file contains 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
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})); | |
} |
This file contains 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
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); | |
} | |
} |
This file contains 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
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); | |
} | |
} | |
} | |
} | |
} |
This file contains 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
<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