kontent-ai-learn / kontent-tutorial-plain-javascript Goto Github PK
View Code? Open in Web Editor NEWA simple website written in plain JavaScript with data from Kentico Kontent.
Home Page: https://kontent.ai
License: MIT License
A simple website written in plain JavaScript with data from Kentico Kontent.
Home Page: https://kontent.ai
License: MIT License
When you get data from array:
item.video_host.value[0]
you should first check whether the value exists and whether it has defined length, otherwise js will throw an exception.
var youtube = item.video_host && item.video_host.length ? item.video_host[0] : undefined;
if (youtube) {
///
}
See https://codeburst.io/javascript-double-equals-vs-triple-equals-61d4ce5a121a
Original:
if (link.type == "article") {
return {
url: `article.html#${link.urlSlug}`
};
}
New
if (link.type === "article") {
return {
url: `article.html#${link.urlSlug}`
};
}
Depending on your browser support (IE doesn't support const for instance), you could unify the way you use const/var/let. For example in following code everything can be const, there is no need for 'Kc' to be var or for 'card' to be 'let' as you are not modifying it later on.
const app = document.getElementById("app");
const articleList = document.createElement("div");
articleList.setAttribute("id", "article-list");
app.appendChild(articleList);
var Kc = window["kenticoCloudDelivery"];
var deliveryClient = new Kc.DeliveryClient({
projectId: "975bf280-fd91-488c-994c-2f04416e5ee3"
});
deliveryClient
.items()
.type("article")
.toPromise()
.then(response => {
response.items.forEach(item => {
let card = document.createElement("div");
card.setAttribute("class", "card");
let link = document.createElement("a");
link.href = "./article.html#" + item.url_pattern.value;
let teaser = document.createElement("img");
teaser.setAttribute("class", "article-teaser");
teaser.src = item.teaser_image.value[0].url + "?w=500&h=500";
let title = document.createElement("h2");
title.setAttribute("class", "article-title");
title.innerText = item.title.value;
let description = document.createElement("div");
description.setAttribute("class", "article-description");
description.innerHTML = item.summary.value;
articleList.appendChild(card);
card.appendChild(link);
link.appendChild(teaser);
link.appendChild(title);
link.appendChild(description);
});
});
It's currently not compatible with IE11 - it's probably not required, but just to let you know. There would have to be few changes in code such as avoiding backticks, arrow functions and using dedicated legacy UMD bundle of the delivery-cloud-js sdk.
Works fine in Opera & Chrome.
How about if you created a shared 'core.js' file where you defined your delivery client, app and other shared code?
Repo should be renamed to kontent-tutorial-plain-javascript
to be in line with other repo naming conventions of product-project-type-language
In
.catch(err => {
app.innerHTML = notFound
console.log('error: ' + err)
});
you don't actually know whether the issue is a 404 or something completely different. For example, if you access your article.html
like this:
file:///C:/dev/plain-javascript-tutorial/article.html#smudla
The console shows this message:
error: TypeError: Cannot read property 'teaser_image' of undefined
This doesn't seem right as it happens because in
.then(response => {
var article = response.items[0];
let headerImage = document.createElement("img");
headerImage.setAttribute("class", "article-header");
headerImage.src = article.teaser_image.value[0].url + "?w=500&h=500";
let title = document.createElement("h2");
title.setAttribute("class", "article-title");
title.innerText = article.title.value;
let body = document.createElement("div");
body.setAttribute("class", "article-description");
body.innerHTML = article.body_copy.resolveHtml();
articleContainer.appendChild(headerImage);
articleContainer.appendChild(title);
articleContainer.appendChild(body);
})
You don't verify if article was found or not. You could use something like this:
.then(response => {
const article = response.items.length ? response.items[0] : undefined;
if (!article) {
app.innerHTML = notFound
return;
}
let headerImage = document.createElement("img");
headerImage.setAttribute("class", "article-header");
headerImage.src = article.teaser_image.value[0].url + "?w=500&h=500";
let title = document.createElement("h2");
title.setAttribute("class", "article-title");
title.innerText = article.title.value;
let body = document.createElement("div");
body.setAttribute("class", "article-description");
body.innerHTML = article.body_copy.resolveHtml();
articleContainer.appendChild(headerImage);
articleContainer.appendChild(title);
articleContainer.appendChild(body);
})
The catch could look as simple as:
.catch(err => {
console.error(err);
app.innerHTML = `I'm unknown error :(`;
});
There is a way to handle cloud specific errors, but that doesn't currently work in plain js without explicitely referencing kentico-cloud-core
package. I'll have to move objects around to make it work as explained here (https://github.com/Kentico/kentico-cloud-js/blob/master/packages/delivery/DOCS.md#handling-errors)
console.log('error...');
use
console.error('error...');
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.