Created
October 30, 2015 15:49
-
-
Save pstjvn/a0e1156c5ad15c18c2c9 to your computer and use it in GitHub Desktop.
Custom elements / es6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
/** | |
* Class in es6 | |
* @constructor | |
* @extends {HTMLElement} | |
*/ | |
wc.element.Test = class extends HTMLElement { | |
constructor() { | |
this.value_ = 0; | |
} | |
createdCallback() { | |
console.log('Created callback'); | |
this.value_ = 0; | |
} | |
attachedCallback() { | |
console.log('Attached callback', this.value_); | |
} | |
detachedCallback() { | |
console.log('Detached'); | |
} | |
set value(newv) { | |
console.log('Value setter', newv); | |
this.value_ = newv; | |
this.dispatchEvent(new Event('change')); | |
} | |
get value() { | |
console.log('Value getter'); | |
return this.value_; | |
} | |
attributeChangedCallback(name, oldv, newv, ns) { | |
console.log('typeof ', typeof newv); | |
console.log('Attribute changed', name, oldv, newv, ns); | |
} | |
}; | |
// Register the element. | |
document.registerElement('pstj-test', wc.element.Test); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
setTimeout(function() { | |
var pstj = document.querySelector('pstj-test'); | |
pstj.addEventListener('change', function(e) { | |
console.log('PSTJ change event'); | |
}); | |
console.log(pstj.value); | |
pstj.value = 1; | |
console.log(pstj.value); | |
pstj.setAttribute('value', 2); | |
console.log('get from attribute', pstj.getAttribute('value'), | |
typeof pstj.getAttribute('value')); | |
pstj.value = 3; | |
console.log('get from getter', pstj.value); | |
pstj.parentElement.removeChild(pstj); | |
setTimeout(function() { | |
document.body.appendChild(pstj); | |
pstj.setAttribute('value', '3'); | |
console.log('get from attribute', pstj.getAttribute('value'), | |
typeof pstj.getAttribute('value')); | |
console.log('get from getter', pstj.value); | |
}, 1000); | |
}, 1000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment