I am using ES6 because of its syntactical class features whose are similar to Java.
I want to use functions passed in the object constructor.
class DialogOkCancel extends Dialog {
constructor(title, okEvent, cancelEvent) {
super(title);
this._okEvent = okEvent;
this._cancelEvent = cancelEvent;
}
buildDialog() {
super.buildDialog();
$("#" + this._id).dialog({
buttons: [
{
text: "Ok",
click: this._okEvent
},
{
text: "Abbrechen",
click: this._cancelEvent
}
]
});
}
}
And this is my object code.
var dialog = new DialogOkCancel("test", function () {
alert("Ok");
},
function () {
alert("Cancel");
});
I can use the variables okEvent and cancelEvent and their member vars. But when I leave the scope the script engine says undefined or not a function.
I also tried:
buildDialog() {
super.buildDialog();
var that = this;
$("#" + this._id).dialog({
buttons: [
{
text: "Ok",
click: that._okEvent
},
{
text: "Cancel",
click: that._cancelEvent
}
]
});
}
Dialog class: class Dialog {
constructor(title) {
this._id = IdGenerator.generate();
this._title = title;
this._dialog = document.createElement("div");
this._dialog.setAttribute("id", this._id);
document.body.appendChild(this._dialog);
this.buildDialog();
}
getId() {
return this._id;
}
getTitle() {
return this._title;
}
getDialog() {
return this._dialog;
}
buildDialog() {
$("#" + this._id).dialog({
autoOpen: false,
//width: 400,
hide: { effect: "scale", duration: 200 },
modal: true,
show: { effect: "blind", duration: 200 },
title: this._title
});
}
}