وب کامپوننتها رویکرد جدیدی به توسعه وبسایتها و وب اپلیکیشنها میباشند. هدف از توسعه وبکامپوننتها ساخت عناصری (HTML Elements) مستقل بوده که توانایی کنش با یکدیگر را دارا باشند. به عنوان مثال وبکامپوننتها به شما توانایی تعریف و استفاده از تگهایی مانند و را میدهند. در حال حاضر مرورگرهای مدرن به صورت استاندارد و سایر مرورگرها به صورت polyfill از وبکامپوننتها پشتیبانی میکنند. وب کامپوننتها در سال 2012 توسط شرکت گوگل معرفی شدند. همچنین شرکت گوگل نسخه 1.0 کتابخانه Polymer را با هدف پیادهسازی و توسعه وبکامپوننتها در سال 2015 به صورت عمومی ارائه کرد.
6. We can do it better
1.Find a tag (element)
• bower install --save PolymerElements/paper-menu
2.Import
• <link rel="import" href="../bower_components/paper-menu/paper-menu.html">
3.Use it
• <paper-menu selected="0">
<paper-item>Inbox</paper-item>
<paper-item disabled>Starred</paper-item>
<paper-item>Sent mail</paper-item>
</paper-menu>
14. Templates
Template are inert chunks of DOM the can be reused.
<template id="my-element">
<h1>Awesome Trunk!</h1>
<img src="">
</template>
<script>
var tmpl = document.querySelector(‘#my-element’);
tmpl.content.querySelector("img").src = "trunk.gif";
document.body.appendChild(document.importNode(tmpl.content, true));
</script>
Content
15. Templates Limitations
• No built-in data interpolation
• {{}} don’t do anything...yet!
• Nested templates not activated automatically
• Append each template separately
17. • Provides style and markup encapsulation.
• The same technology used by browser makers to implement tags:
• <video>
• <video src="./be-happy.webm" controls>
Shadow DOM
18. Shadow Host
The node that contains all of our shadow DOM
<video>
Shadow Root
The first node in the shadow DOM
#document-fragment
Shadow Boundary
The barrier that protects our shadow DOM
<div> <div>
19. <template>
<style>
h3 { color: white; background: tomato}
</style>
<h3>A Shadow Header</h3>
</template>
<script>
var tmpl = document.querySelector('template');
var host = document.querySelector(.my-header');
var root = host.createShadowRoot();
root.appendChild(document.importNode(tmpl.content, true));
</script>
</body>
Style Encapsulation
A Shadow Header
<body>
<h3>A Default Header</h3> A Default Header
25. Life Cycle Callbacks
• createdCallback()
When a new instance is created. Use like a constructor
• attachedCallback()
When an element is added to the page
• detachedCallback()
When an element is removed from the page
• attributeChangedCallback(attrName, oldVal, newVal)
When one of an element's attributes changes
26. The Specs
• Templates
• Shadow DOM
• Custom Elements
• Imports
• Packaging
• Everything on one page
27. • Imports load external documents into your page.
• Use a link tag, just like css.
• Use rel type import
• <link rel="import" href="my-import.html">
Imports