I'm currently having a problem and don't know how to solve it correctly.
I'm developing a web application with js using the WebMidi API.
I want to load all midi devices into a dropdown after WebMidi is loaded. As far as I understood it I have to pass the enable function of Webmidi a callback function:
WebMidi.enable(function (err) {
if (err) {
console.log("WebMidi could not be enabled.", err);
} else {
console.log("WebMidi enabled!");
}
});
The problem I am having is I cannot reference class methods inside the callback function, but I have to call the method that's fills the dropdown list with all midi inputs, as soon as WebMidi is enabled.
My code looks like this:
class MidiListener{
constructor(){
this.enable();
this.fillDropdown(Webmidi.inputs);
}
enable(){
WebMidi.enable(function (err) {
//Enable WebMidi
if (err) {
console.log("WebMidi could not be enabled.", err);
window.alert("WebMidi is not supported by your Browser.")
}
else {
console.log("WebMidi enabled!");
}
});
}
...
The problem is fillDropdown() is called before WebMidi finished activating, and the array is empty at the time. Can I somehow reference class methods inside the callback function?