Added ref functionality to get a ref from any template once it's created. Next up is property callback cleanup and disconnection upon element being removed.

This commit is contained in:
2020-07-28 09:50:26 -07:00
parent b591a42370
commit 1adb844c97
3 changed files with 22 additions and 3 deletions

View File

@ -9,6 +9,7 @@ class IgniteTemplate {
this.tagName = null;
this.element = null;
this.properties = {};
this.refs = [];
if (items) {
for (var i = 0; i < items.length; i++) {
@ -64,11 +65,26 @@ class IgniteTemplate {
return this;
}
ref(item) {
if (item instanceof IgniteProperty) {
this.refs.push((element) => {
item.value = element;
});
} else {
this.refs.push(item);
}
return this;
}
construct(parent) {
//Construct this element if we haven't already
if (!this.element) {
this.element = window.document.createElement(this.tagName);
parent.appendChild(this.element);
//Invoke any refs we have
this.refs.forEach((ref) => { ref(this.element); });
}
//Set the classes on this element
@ -116,6 +132,8 @@ class IgniteTemplate {
onAttributeChanged(oldValue, newValue, attributeName) {
console.log(`Attribute changed, oldValue: ${oldValue} newValue: ${newValue} attribute: ${attributeName}`);
console.log("this element:");
console.log(this.element);
if (newValue == null || newValue == undefined) {
this.element.removeAttribute(attributeName);