showMessageBox promise is never called - javascript

The message box appears properly, but when I select one of the buttons, the closure doesn't trigger. Here is my code:
dialog.showMessageBox(options, (response, checkboxChecked) => {
/* More code like log statements, opening error boxes, etc*/
}
Options is defined as choices = ["Yes", "No"], message = "Confirm".
The code inside of the curly braces is never run.

Try:
const options = {
type: 'question',
buttons: ['Cancel', 'Yes, please', 'No, thanks'],
defaultId: 2,
title: 'Question',
message: 'Do you want to do this?',
detail: 'It does not really matter',
checkboxLabel: 'Remember my answer',
checkboxChecked: true,
};
dialog.showMessageBox(null, options, (response, checkboxChecked) => {
console.log(response);
console.log(checkboxChecked);
});

Related

Primeng confirm dialog show when it shouldn't

I have a method that checks if any property of object in array is equal to 'not ready'. If it is it should inform user about that and ask if he still wishes to continue. If no, there is return that breaks the method and if yes, method continues and there is one more confirm dialog
createOrder() {
if(this.orders.filter(e => e.statusName === 'not ready').length > 0){
this.confirmationService.confirm({
message: 'There is already ongoing order. Do you want start another?',
header: 'Order',
icon: 'fa fa-exclamation-triangle',
acceptLabel: 'Yes',
rejectLabel: 'No',
key: 'ongoin',
accept: () => {
},
reject: () => {
return
}
})
}
this.confirmationService.confirm({
message: 'Are you sure?',
header: 'Order',
icon: 'fa fa-exclamation-triangle',
acceptLabel: 'Yes',
rejectLabel: 'No',
key: 'confirm',
accept: () => {
*//add object code*
}
});
This how it looks like in html:
<button pButton tooltipPosition="left" (click)="createOrder()" icon="fa fa-plus-square"
class="ui-button-warning">
</button>
<p-confirmDialog key='ongoin'></p-confirmDialog>
<p-confirmDialog key='confirm'></p-confirmDialog>
My problem is that both confirm dialogs shows up at the same time. I guess it is because of using twice in html. But if i change that to
<p-confirmDialog></p-confirmDialog>
Only the first one works ('There is already ongoin...') and second one('Are you sure') after clicking yes doesn't show up. How do I make it work?
You should be calling the second dialog after the confirmation happens. Which happens in accept or reject callbacks. But you are immediately calling this.confirmationService.confirm again and it is causing both dialogs to be shown.
You should be having something like below :
createOrder() {
if(this.orders.filter(e => e.statusName === 'not ready').length > 0){
this.confirmationService.confirm({
message: 'There is already ongoing order. Do you want start another?',
header: 'Order',
icon: 'fa fa-exclamation-triangle',
acceptLabel: 'Yes',
rejectLabel: 'No',
key: 'ongoin',
accept: () => {
// do something here
this.showAnotherDialog();
},
reject: () => {
// if you want to show another dialog on reject you should call it here
return
}
})
} else {
this.showAnotherDialog();
}
}
showAnotherDialog() {
this.confirmationService.confirm({
message: 'Are you sure?',
header: 'Order',
icon: 'fa fa-exclamation-triangle',
acceptLabel: 'Yes',
rejectLabel: 'No',
key: 'confirm',
accept: () => {
*//add object code*
}
});
}

Bootbox callback function not being called

I am trying to use Bootbox within an Angular2 application. I have the following code:
bootbox.confirm({
message: "Are you sure you want to complete this action?",
buttons: {
confirm: {label: 'Yes', className: 'btn-success'},
cancel: {label: 'No', className: 'btn-danger'}
},
callback: function (result: any) {
console.log('Response equals: ' + result);
}
});
The confirm box displays correctly when called and when clicking on either the "yes" or "no" button the confirm box disappears as it should. However, the callback function is not firing because I'm not getting the console message.
This is my first attempt trying to inject Bootbox into an application so I'm not sure why the callback function is not being called.
Any ideas?
Have you tried with function(result) remove ':any'.
bootbox.confirm({
message: "This is a confirm with custom button text and color! Do you like it?",
buttons: {
confirm: {
label: 'Yes',
className: 'btn-success'
},
cancel: {
label: 'No',
className: 'btn-danger'
}
},
callback: function (result) {
console.log('This was logged in the callback: ' + result);
}
});
http://bootboxjs.com/examples.html#bb-confirm-dialog. Call back takes only one argument that is result.

"Never ask again" feature on Electron dialog.showMessageBox method

I have a dialog.showMessageBox that works well and do what I tell it to do.
electron.dialog.showMessageBox({
type: 'info',
buttons: ['Yes', 'No'],
message: 'Are you sure?',
}, resp => {
if (resp === 0) {
// User selected 'Yes'
foo.bar();
}
});
But it's annoying to answer each times this function is called, I'd like to insert a "Never ask me again" like this.
electron.dialog.showMessageBox({
type: 'info',
buttons: ['Yes', 'No'],
message: 'Are you sure?',
checkboxLabel: 'Never ask me again',
checkboxChecked: false
}, resp => {
if (resp === 0) {
// User selected 'Yes'
foo.bar();
}
});
The documentation says that the boolean checkboxChecked can be used in the callback function but I'd like to know how to make it global so that the question isn't ask anymore.
Ok so here's what I've done:
const settings = require('electron-settings');
settings.set('neverAskMeAgain', {
state: false
});
if (!settings.get('neverAskMeAgain.state')) {
electron.dialog.showMessageBox({
type: 'info',
buttons: ['Yes', 'No'],
message: 'Are you sure?',
checkboxLabel: 'Never ask me again',
checkboxChecked: false
}, (resp, checkboxChecked) => {
if (resp === 0) {
foo.bar();
settings.set('neverAskMeAgain.state', checkboxChecked);
}
});
} else if (settings.get('neverAskMeAgain.state')) {
foo.bar();
}
} else {
foo.bar();
}
It works like a charm within an instance of the app when running npm install && npm start but when I quit it and relaunch it, the dialog box is displayed again...
I would recommend using a lib named electron-settings. Link here.
That way, your code would look something like that:
const settings = require('electron-settings');
electron.dialog.showMessageBox({
type: 'info',
buttons: ['Yes', 'No'],
message: 'Are you sure?',
checkboxLabel: 'Never ask me again',
checkboxChecked: false
},
resp => {
if (resp === 0) {
// User selected 'Yes'
settings.set('never_ask_again_answer', true);
foo.bar();
}
});
// get the answer later
let answer = settings.get('never_ask_again_answer')
EDIT: I think it doesn't work when you restart because you're just setting it to false again right at the start. Little fix will do:
const settings = require('electron-settings');
if(!settings.has('neverAskMeAgain')){ // this if might do it
settings.set('neverAskMeAgain', {
state: false
});
}
if (!settings.get('neverAskMeAgain.state')) {
electron.dialog.showMessageBox({
type: 'info',
buttons: ['Yes', 'No'],
message: 'Are you sure?',
checkboxLabel: 'Never ask me again',
checkboxChecked: false
}, (resp, checkboxChecked) => {
if (resp === 0) {
foo.bar();
settings.set('neverAskMeAgain.state', checkboxChecked);
}
});
} else if (settings.get('neverAskMeAgain.state')) {
foo.bar();
}

Bootbox 4.1.0: how to pass localized strings such as Ok, Cancel to Bootbox's confirm?

In Bootbox 3.2.0, I was able to use confirm with strings passed as below:
bootbox.confirm(
confirm_string,
cancel_string,
yes_string,
function(r) {
if (r) {
//do something
}
}
);
I am upgrading to 4.1.0 and I got errors with the above function call.
According to the documentation (http://bootboxjs.com/documentation.html) of Bootbox 4.1.0, there are two ways to call confirm:
bootbox.confirm(str message, fn callback)
bootbox.confirm(object options)
I tested the fist way with the message string and a callback function and it works. For the second way, I was able to pass an object as follows:
{
message: message_string
callback: function(r) {
//do something
}
}
How can I pass strings for OK, Cancel buttons in the second way?
Thanks and regards.
As an alternative this can also be done directly with bootbox.confirm, like so:
bootbox.confirm({
buttons: {
confirm: {
label: 'Localized confirm text',
className: 'confirm-button-class'
},
cancel: {
label: 'Localized cancel text',
className: 'cancel-button-class'
}
},
message: 'Your message',
callback: function(result) {
console.log(result);
},
title: "You can also add a title",
});
OR use localization - option, to change ALL Buttons per Default:
bootbox.setDefaults({
/**
* #optional String
* #default: en
* which locale settings to use to translate the three
* standard button labels: OK, CONFIRM, CANCEL
*/
locale: "de"
});
seen: http://bootboxjs.com/documentation.html, "Helper methods"
You can use the "Custom dialog" (bootbox.dialog) to change these strings.
bootbox.dialog({
message: "Custom message",
title: "Custom title",
buttons: {
danger: {
label: "Custom Cancel",
className: "btn-danger",
callback: function() {
//do something
}
},
main: {
label: "Custom OK",
className: "btn-primary",
callback: function() {
//do something else
}
}
}
});

How do I return true or false using jQuery noty plugin?

I have the following jQuery using the noty plugin and plum shopping cart jQuery. The first block of code correctly alerts a yes / no and empties the cart correctly.
The second block of code shows the yes / no message correctly using noty BUT it does not return the true / false so the cart isnt emptied.
Im really new to jQuery so I'm probably missing something obvious ! Any help would be appreciated:
//This works..
emptycart: function () {
if (!confirm('Are you sure you want to empty your cart?')) {
return false;
}
},
//This doesnt work..
emptycart: function () {
confirm.call(this, noty({
text: 'Are you sure you want to empty the cart ?',
buttons: [
{type: 'button green', text: 'Ok', click: function() { return false; } },
{type: 'button pink', text: 'Cancel', click: function() { return true; } }
],
closable: false,
timeout: false
}),
true
);
return false;
},
Well, the plugin is receiving callbacks, so when you use callbacks you must think in a asynchronous way.
/**
* #param {function} onConfirm : Receives true or false as argument, depending on the user choice
*/
emptycart: function (onConfirm) {
confirm.call(this, noty({
text: 'Are you sure you want to empty the cart ?',
buttons: [
{
type: 'button green',
text: 'Ok',
click: function() {
//Do other stuff if you want...
onConfirm(true);
}
},
{
type: 'button pink',
text: 'Cancel',
click: function() {
//Do other stuff if you want...
onConfirm(false);
}
}
],
closable: false,
timeout: false
}), true);
}
Above, you will send a callbackfunction "onConfirm" which will be called when the user clicks on either buttons. The function will receive one boolean argument telling if was a click on Ok or Cancel.

Categories

Resources