Last active
March 3, 2023 19:39
-
-
Save barredterra/369e8499288a3a50104b950dd3e0ac04 to your computer and use it in GitHub Desktop.
Comfortably work with dialogs in Frappe Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dialog_promise (title, fields, primary_action_label) { | |
// Return a function that returns a promise | |
// The promise resolves when the dialog is submitted and rejects when the dialog is closed | |
// On resolve, the promise returns the values from the dialog. | |
return () => new Promise((resolve, reject) => { | |
const dialog = new frappe.ui.Dialog({ | |
title: title, | |
fields: fields, | |
onhide: () => reject(), | |
primary_action_label: primary_action_label, | |
primary_action: () => { | |
resolve(dialog.get_values() || {}); | |
dialog.hide(); | |
}, | |
}); | |
dialog.show(); | |
}); | |
}; | |
const ask_for_name = dialog_promise( | |
__("What's your name?"), | |
[ | |
{ | |
fieldname: "full_name", | |
fieldtype: "Data" | |
} | |
], | |
__("Say hi") | |
); | |
ask_for_name() | |
.then(({ full_name }) => frappe.show_alert(__("Hi {0}, nice to meet you!", [full_name]))) | |
.catch(() => frappe.show_alert(__("Guess you're not in the mood to talk"))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment