If I understand you correctly then you're looking for the builder pattern.
Here's an implementation based on your code:
class DialogService {
private _title: string;
private _message: string;
private _okBtn: string;
private _cancelBtn: string;
private _confirm: string;
private _cancel: string;
constructor() {}
public title(value: string): DialogService {
this._title = value;
return this;
}
public message(value: string): DialogService {
this._message = value;
return this;
}
public okBtn(value: string): DialogService {
this._okBtn = value;
return this;
}
public cancelBtn(value: string): DialogService {
this._cancelBtn = value;
return this;
}
public confirm(value: () => {}): DialogService {
this._confirm = value;
return this;
}
public cancel(value: () => {}): DialogService {
this._cancel = value;
return this;
}
public show(): void {
bootbox.dialog({
title: this._title,
message: this._message,
buttons: {
cancel: {
label: this._cancelBtn,
className: 'btn-inverse btn-inverse-primary',
callback: this._cancel
},
okay: {
label: okBtn,
className: 'btn-inverse btn-inverse-primary',
callback: this._confirm
}
}
});
}
}
Then:
new DialogService()
.title('Some title')
.message('Some message')
.okBtn('Sounds Good')
.cancelBtn('No way!')
.confirm(() => {})
.cancel(() => {})
.show();
Edit
I saw your changed question after posting this edit, so I'm re-editing it:
interface ButtonData {
label: string;
className: string;
callback: () => void;
}
class DialogService {
private static BUTTON_CLASS_NAME = "btn-inverse btn-inverse-primary";
private _title: string;
private _message: string;
private _okay: ButtonData;
private _cancel: ButtonData;
constructor() {}
public title(value: string): DialogService {
this._title = value;
return this;
}
public message(value: string): DialogService {
this._message = value;
return this;
}
public okay(label: string, callback?: () => void): DialogService {
this._okay = {
label: label,
className: DialogService.BUTTON_CLASS_NAME,
callback: callback || function() {}
};
return this;
}
public cancel(label: string, callback?: () => void): DialogService {
this._cancel = {
label: label,
className: DialogService.BUTTON_CLASS_NAME,
callback: callback || function() {}
};
return this;
}
public alert(): void {
bootbox.dialog({
title: this._title,
message: this._message,
buttons: {
okay: this.okay
}
});
}
public confirm(): void {
bootbox.dialog({
title: this._title,
message: this._message,
buttons: {
cancel: this._cancel,
okay: this.okay
}
});
}
}
older edit
I'm still not sure what exactly you're looking for, I changed things a bit and made sure that there are confirm and alert methods, but I'm not sure what they are supposed to do...
The confirm uses the bootbox.dialog your code has, but I had no idea what to do with the alert so I invented the bootbox.alert function.
It's probably wrong and you'll need to implement that yourself...
interface ButtonData {
label: string;
className: string;
callback: () => void;
}
interface ServiceData {
title: string;
message: string;
buttons: {
cancel: ButtonData,
okay: ButtonData
};
}
class DialogService {
private static BUTTON_CLASS_NAME = "btn-inverse btn-inverse-primary";
private data: ServiceData
constructor() {
this.data = <ServiceData> {
buttons: {
cancel: <ButtonData> {},
okay: <ButtonData> {}
}
};
}
public title(value: string): DialogService {
this.data.title = value;
return this;
}
public message(value: string): DialogService {
this.data.message = value;
return this;
}
public okay(label: string, callback?: () => void): DialogService {
this.data.buttons.okay = {
label: label,
className: DialogService.BUTTON_CLASS_NAME,
callback: callback || function() {}
}
return this;
}
public cancel(label: string, callback?: () => void): DialogService {
this.data.buttons.cancel = {
label: label,
className: DialogService.BUTTON_CLASS_NAME,
callback: callback || function() {}
}
return this;
}
public confirm(): void {
bootbox.dialog(this.data);
}
public alert(): void {
bootbox.alert(this.data);
}
}