What is the ES205 / JS6 way, to make a custom element equivalent to this one made by DartLang
Asked
Active
Viewed 869 times
-2
-
What did you find so far? – CoderPi Nov 26 '15 at 20:01
-
@CodeiSir I found this, but not catching the point https://github.com/domenic/webcomponents/blob/constructor-dmitry-revisions/proposals/Constructor-Dmitry.md – Hasan A Yousef Nov 26 '15 at 20:03
-
@CodeiSir, this site was helpful to me, and I posted my solution below: https://www.code-labs.io/codelabs/chrome-es2015/index.html?index=..%2F..%2Fchrome-dev-summit&viewga=UA-41980257-1#0 – Hasan A Yousef Nov 27 '15 at 21:02
-
1@Blazemonger this question is not a duplicate of what you are considering, pls re read both of them, and the techniques used in solving each – Hasan A Yousef Nov 30 '15 at 19:59
-
Perhaps you should spend some time **explaining** your answers, then, as well as providing details in your questions to indicate why they're different from other questions. As it is, you're just pasting similar-looking code over and over again in what looks like an effort to rack up points. – Blazemonger Nov 30 '15 at 21:07
1 Answers
1
I was able to do it, below is how I did it for those who are interested to do so:
"use strict";
// DART: class SaveBtn extends HtmlElement { static final tag = 'save-button';
// ES2015 / JS6
class SaveBtn extends HTMLElement {
// DART: factory SaveBtn([String name]) => (new Element.tag(tag) as SaveBtn)..name = name;
// ES2015 / JS6
constructor() {
super();
}
// DART: SaveBtn.created() : super.created() {
// ES2015 / JS6
createdCallback(){
// Create a Shadow Root, DART & ES2015
var shadow = this.createShadowRoot();
// Create a standard element and set it's attributes.
// DART: innerButton = new ButtonElement()..id='btn';
// ES2015 / JS6
var innerButton = document.createElement('button');
innerButton.id='btn';
innerButton.addEventListener('click',() => console.log('The button had been clicked'));
shadow.appendChild(innerButton);
}
// DART: void attached() { }
// ES2015 / JS6
attachedCallback(){
console.log(this.dataset);
console.log(this.textContent);
this.shadowRoot.querySelector('#btn').textContent = this.textContent != '' ? this.textContent : this.dataset['text'];
}
detachedCallback(){
}
attributeChangedCallback(textContent) {
}
set text(v) {
this.textContent = v;
}
get text() {
return this.textContent;
}
}
var MySaveBtn = document.registerElement("save-button", SaveBtn);
var x = new MySaveBtn;
x.text = 'test';
console.log(x.text);
document.querySelector('#placeholder').appendChild(x);
the HTML file I used was:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ES2015 / JS6 Custom Elements</title>
<script src="default.js" type="text/javascript"></script>
</head>
<body>
Here is some non-custom stuff.
<div id="placeholder"><p>Please wait, loading cool things<p></div>
<save-button data-text='click this'></save-button>
</body>
</html>
another way to solve it without using SHADOWROOT is here

Community
- 1
- 1

Hasan A Yousef
- 22,789
- 24
- 132
- 203