I have this custom HTML element causing me trouble. Down in the StaffList connectedCallback() function, i have to call and outside function of the parrent but the super keyword for addStaff() is giving me an error. "Uncaught SyntaxError: use of super property accesses only valid within methods or eval code within methods". How can i access something outside the request?
class StaffList extends HTMLElement {
// A getter/setter for a disabled property.
get selected() {
return this.hasAttribute('selected');
}
set selected(val) {
// Reflect the value of the disabled property as an HTML attribute.
if (val) {
this.setAttribute('selected', '');
} else {
this.removeAttribute('selected');
}
}
constructor() {
// If you define a constructor, always call super() first!
// This is specific to CE and required by the spec.
super();
/* handle clicks */
for (const item of this.children) {
item.addEventListener('click', e => {
console.log(e);
});
}
}
connectedCallback() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
for (var item of JSON.parse(this.responseText)) {
console.log(item);
this.addStaff(item);
}
}
};
xhttp.open('GET', "/api/get_staff");
xhttp.send();
}
}
addStaff() {
console.log("add staff");
}
customElements.define('staff-list', StaffList);
I tried the keyword this but gives me the XML request object instead of my custom element.