Git Product home page Git Product logo

kontent-tutorial-plain-javascript's Introduction

Kentico Kontent plain JavaScript tutorial

Live Demo Stack Overflow

This is simple website written in plain JavaScript that retrieves and displays content from Kontent by Kentico. It serves as a source code reference for the Building your first plain JavaScript app with Kontent tutorial.

The project uses the Kontent JavaScript Delivery SDK.

To run the website, download the files and open index.html.

To pull content from your own project, change the projectId in both article.js and articleList.js to the ID of your project.

kontent-tutorial-plain-javascript's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kontent-tutorial-plain-javascript's Issues

Handling errors

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)

  • Tip: Instead of
console.log('error...');

use

console.error('error...');

Check for defined values when getting indexed items from array

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) {
   /// 
}

Create shared js file

How about if you created a shared 'core.js' file where you defined your delivery client, app and other shared code?

Unify let/var/const

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);
    });
  });

IE Compatibility (question?)

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.

Repo rename

Motivation/Proposed solution

Repo should be renamed to kontent-tutorial-plain-javascript to be in line with other repo naming conventions of product-project-type-language

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.