;(function() {
"use strict";
const list = document.getElementById("list");
const templateWrapper = document.getElementById("template");
const URL = "https://cpv2api.com/pens/public/Konrud";
/*
* Gets pens according to the received url
* @param {String} url - URL to get data from
* @return {Array} - Array of data
*/
async function getPens (url) {
let pens = {};
try {
const result = await fetch(url);
debugger;
pens = await result.json();
} catch (err) {
debugger;
}
return pens.data;
};
/*
* Fills received list with data from the provided url
* @param {HTMLElement} list - HTML element to fill with data
* @param {String} url - URL to get data from
*/
async function fillListWithData (list, url) {
const pens = await getPens(url);
pens.forEach(function(penItem) {
const template = getTemplate(penItem);
if(template) {
list.insertAdjacentHTML("beforeend", template);
}
});
};
/*
* Create HTML template for the received data object (Using Handlebars)
* @param {Object} dataObj - Object with data to fill the template
* @return {String} - HTML template in string representation
*/
function getTemplate (dataObj) {
debugger;
if(templateWrapper.innerHTML) {
const handlebarsTemplate = Handlebars.compile(templateWrapper.innerHTML)(dataObj);
return handlebarsTemplate;
}
};
fillListWithData(list, URL);
})();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.