Ketika menggunakan custom element, mungkin terdapat keadaan di mana kita membutuhkan custom element berada di dalam custom element lain. Contohnya, banyak website saat ini yang menampilkan data berupa list, entah itu daftar artikel ataupun item belanja. 
Biasanya setiap daftar yang ditampilkan ditampung dalam sebuah container <div>. Kemudian item yang sama ditampilkan secara berulang dengan data yang berbeda pada container tersebut.
20200310191307a4fe82d6dd70b4daae920036121ba5c4.gif
Web component dapat memudahkan dalam mengorganisir daftar item yang ditampilkan dalam bentuk list menggunakan container. Caranya kita membuat dua custom element yatu container, dan itemnya. Container digunakan untuk menampung elemen item di dalamnya. Selain itu pada container juga data (array) diberikan. Nantinya container-lah yang akan membuat elemen item di dalamnya berdasarkan data yang diberikan.
Belum terbayang seperti apa? Berikut contohnya:

  1. <!DOCTYPE html>

  2. <html>

  3.  <head>

  4.    <meta charset="utf-8">

  5.    <meta name="viewport" content="width=device-width">

  6.    <title>repl.it</title>

  7.    <link href="style.css" rel="stylesheet" type="text/css" />

  8.  </head>

  9.  <body>

  10.    <script src="script.js" type="module"></script>

  11.  </body>

  12. </html>



  1. import "./article-list.js";

  2. import articles from "./articles.js";

  3.  

  4.  

  5. const articleListElement = document.createElement("article-list");

  6. articleListElement.articles = articles;

  7.  

  8.  

  9. document.body.appendChild(articleListElement);



  1. import "./article-item.js"

  2.  

  3.  

  4. class ArticleList extends HTMLElement {

  5.  set articles(articles) {

  6.    this._articles = articles;

  7.    this.render();

  8.  }

  9.  

  10.  

  11.  render() {

  12.    this._articles.forEach(article => {

  13.      const articleItemElement = document.createElement("article-item");

  14.      // memanggil fungsi setter article() pada article-item.

  15.      articleItemElement.article = article;

  16.      this.appendChild(articleItemElement);

  17.    })

  18.  }

  19. }

  20.  

  21. customElements.define("article-list", ArticleList);




  1. class ArticleItem extends HTMLElement {

  2.  set article(article) {

  3.    this._article = article;

  4.    this.render();

  5.  }

  6.  

  7.  

  8.  render() {

  9.    this.innerHTML = `

  10.     <img class="featured-image" src="${this._article.featuredImage}">

  11.     <div class="article-info">

  12.         <h2><a href="${this._article.id}">${this._article.title}</a></h2>

  13.         <p>${this._article.description}</p>

  14.     </div>

  15.   `;

  16.  }

  17. }

  18.  

  19.  

  20. customElements.define("article-item", ArticleItem);



  1. const articles = [

  2.  {

  3.    id: 1,

  4.    title: "Lorem Ipsum Dolor",

  5.    featuredImage: "https://i.picsum.photos/id/204/536/354.jpg",

  6.    description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

  7.  

  8.  

  9.  },

  10.  {

  11.    id: 2,

  12.    title: "Lorem Ipsum Dolor",

  13.    featuredImage: "https://i.picsum.photos/id/209/536/354.jpg",

  14.    description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

  15.  

  16.  

  17.  },

  18.  {

  19.    id: 3,

  20.    title: "Lorem Ipsum Dolor",

  21.    featuredImage: "https://i.picsum.photos/id/206/536/354.jpg",

  22.    description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

  23.  

  24.  

  25.  },

  26.  {

  27.    id: 4,

  28.    title: "Lorem Ipsum Dolor",

  29.    featuredImage: "https://i.picsum.photos/id/212/536/354.jpg",

  30.    description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

  31.  

  32.  

  33.  }

  34. ]

  35.  

  36.  

  37. export default articles;




  1. * {

  2.     padding: 0;

  3.     margin: 0;

  4.     box-sizing: border-box;

  5.   }

  6.   

  7.   body {

  8.     padding: 16px;

  9.   }

  10.   

  11.   article-list {

  12.     display: block;

  13.     max-width: 800px;

  14.     margin: 0 auto;

  15.   }

  16.   

  17.   article-item {

  18.     display: block;

  19.     margin-bottom: 18px;

  20.     box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);

  21.     border-radius: 10px;

  22.     overflow: hidden;

  23.   }

  24.   

  25.   article-item > .featured-image {

  26.     width: 100%;

  27.     max-height: 300px;

  28.     object-fit: cover;

  29.     object-position: center;

  30.   }

  31.   

  32.   article-item > .article-info {

  33.     padding: 24px;

  34.   }

  35.   

  36.   article-item > .article-info > p {

  37.     margin-top: 10px;

  38.   }



Pada kode di atas kita bisa melihat bahwa terdapat dua buah custom component yaitu <article-list> dan <article-item>. Pada article-list.js terdapat fungsi setter articles yang berfungsi untuk menyimpan nilai articles pada properti this._articles. 

  1. set articles(articles) {

  2.    this._articles = articles;

  3.    this.render();

  4. }


Kemudian properti tersebut digunakan pada fungsi render() untuk ditampilkan satu persatu melalui <article-item>.

  1. render() {

  2.    this._articles.forEach(article => {

  3.      const articleItemElement = document.createElement("article-item");

  4.      // memanggil fungsi setter article() pada article-item.

  5.      articleItemElement.article = article;

  6.      this.appendChild(articleItemElement);

  7.    })

  8. }


Dengan begitu, untuk menampilkan data pada script.js akan lebih mudah. Kita tidak perlu melakukan proses perulangan lagi di sana karena proses tersebut langsung dilakukan ketika menggunakan element <article-list>. Kita cukup memberikan nilai array yang akan ditampilkan.

  1. import "./article-list.js";

  2. import articles from "./articles.js";

  3.  

  4. const articleListElement = document.createElement("article-list");

  5. articleListElement.articles = articles;

  6.  

  7. document.body.appendChild(articleListElement);


Semakin mudah kita menggunakan sebuah element maka akan semakin baik bukan? Walaupun terlihat agak sedikit merepotkan dalam membuatnya, perlu Anda ingat bahwa  web component ini bersifat reusable. Artinya, jika kita ingin membuat komponen serupa, kita tidak perlu membuatnya dari awal.
Dengan menjalankan kode di atas, maka hasilnya akan tampak seperti ini:
202003101921481bcb54550f43e9958256358a3c73709b.png

Contoh Nested Custom Element Di Javascript
Bookmark

Contoh Nested Custom Element Di Javascript

Type:
Genre:
Status:
Country:
Keywords: Watch online Contoh Nested Custom Element Di Javascript bahasa Indonesia, Streaming anime Contoh Nested Custom Element Di Javascript sub indo, Mau nonton anime terbaik dengan subtitle Indonesia? Streaming anime Jepang dari anime Contoh Nested Custom Element Di Javascript, website streaming anime subtitle indonesia dan nonton anime indo update setiap hari, TV Contoh Nested Custom Element Di Javascript, Terbaru Terupdate dan Terlengkap dengan Kualitas 1080p, 720p. 480p, 360p Anime Sub Indo.

Comments

Comment Report Rule
Show Comments
9Anime Comment Policies & Guidelines
  1. Spoiling Content

    Definition

    “Anime Comment” is a spoiler-free section and is usually used for a general discussion about anime without spoiling any content.

    “Episode Comment” may contain spoilers for current or previous episodes. Contents from manga or future episodes are considered as spoilers.

    Hiding spoilers with spoiler tags is not allowed either.

    Consequences

    Minor Spoiler:-

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

    Major Spoiler:-

    1st Offense: Permanent Ban

  2. Diversion From Topic

    Definition

    The comment section should be about the anime on hand. Although references from other anime may be used, the core topic of the comments should be about the anime at hand.

    This rule also applies on advertising other streaming websites which results in an instant ban.

    This rule also applies to spam, posting comments like “first”, “second”, etc. are considered spam.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  3. Flame War

    Definition

    Exchange of angry or abusive messages between the comments. Any form of insulting is also considered as flame war.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  4. Posting Harmful Link

    Definition

    Post of hyperlinks for websites that may contain viruses or may harm your PC or device.

    Consequences

    1st Offense: Permanent Ban + Deletion of All Comments

  5. Posting NSFW Content

    Definition

    Posting contents that are not safe for work, such as pornographic images or video, ecchi anime, hentai, etc.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  6. Inappropriate Profile Picture

    Definition

    Using a profile picture that is not safe for work. Although minor ecchi or fanservice is acceptable.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  7. Use of Common Sense

    Definition

    If you think that you’ll get in trouble for what you’re about to do, don’t do it.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  8. Upvote Abusing

    Definition

    Creating multiple alternative accounts to upvote your own comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts + Deletion of All Comments

  9. Downvote Abusing

    Definition

    Targeting a user and downvoting all his comments, or creating multiple accounts to downvote one or more comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts

  10. Swearing

    Definition

    Swearing is fine, as long as it’s not directed towards anyone, even as a joke. It may be misinterpreted by a moderator.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  11. Moderator Verdict

    Definition

    A moderator’s verdict is final and arguing with them will only cause further punishment. If you have a complaint against a staff member, or wish to file a ban appeal, go to the 9Anime Discord Server and post your issue in the #disqus-appeals channel.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

Think you have been unfairly warned or banned?

If you believe that you’ve been unfairly banned or if you have a complaint against one or more Disqus Staff Members, you may post an appeal or complaint on 9Anime Discord Server.

STEPS

  1. Join 9Anime Discord Server: https://discord.gg/xxxxxxx
  2. Read the #rules channel and follow instructions. (Optional, but highly recommended)
  3. Head towards #disqus-appeals channel.
  4. Use the appropriate template to appeal a ban or file a complaint.