{"id":291,"date":"2011-01-02T17:43:01","date_gmt":"2011-01-02T16:43:01","guid":{"rendered":"http:\/\/www.speich.net\/articles\/?p=291"},"modified":"2019-10-06T12:28:19","modified_gmt":"2019-10-06T10:28:19","slug":"creating-a-blocking-confirm-dialog-with-dojo","status":"publish","type":"post","link":"https:\/\/www.speich.net\/articles\/en\/2011\/01\/02\/creating-a-blocking-confirm-dialog-with-dojo\/","title":{"rendered":"Creating a &#8216;blocking&#8217; confirm dialog with dojo"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Update 30.03.2018<\/strong>: Version 1.10 of dojotoolkit includes a <a href=\"https:\/\/dojotoolkit.org\/reference-guide\/1.10\/dijit\/ConfirmDialog.html\">dijit\/DialogConfirm<\/a> now, which is simply a dijit\/Dialog featuring &#8216;OK&#8217; and &#8216;Cancel&#8217; buttons, no blocking though.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Update 15.10.2012:<\/strong> Code is available on <a title=\"dojo DialogConfirm widget\" href=\"https:\/\/github.com\/speich\/DialogConfirm\">githup <\/a>now. I also fixed updating the content of the dialog after creation (e.g. dialog.set(&#8216;content&#8217;, newContent) overwrote the confirm\/cancel buttons because they were part of the content.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Update 03.12.2011:<\/strong> I updated the code to use dojo 1.7 with AMD. I also moved the creation of the Deferred to the show() method <a href=\"http:\/\/www.speich.net\/articles\/2011\/01\/02\/creating-a-blocking-confirm-dialog-with-dojo\/#comment-111\">as suggested by ben<\/a>, which will make the instance of the dialog reusable. Added new property hasUnderlay: true, which can be set to false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JavaScript has a <a href=\"https:\/\/developer.mozilla.org\/en\/DOM\/window.confirm\">window.confirm()<\/a> method, which opens a dialog and stops code execution until the user presses either the &#8216;OK&#8217; or &#8216;Cancel&#8217; button. Dojo also comes with a <a title=\"dijit.Dialog\" href=\"http:\/\/dojotoolkit.org\/reference-guide\/1.10\/dijit\/Dialog.html\">dijit.Dialog <\/a>widget, but that doesn&#8217;t exhibit the same blocking behavior. Since I needed to simulate the native blocking behavior for an application I am currently developing, I extended the widget and created the <a title=\"dojo DialogConfirm widget\" href=\"https:\/\/github.com\/speich\/DialogConfirm\/blob\/master\/DialogConfirm.js\">DialogConfirm widget<\/a> with the following features:<\/p>\n\n\n\n<ul class=\"main wp-block-list\"><li>shows&nbsp;an &#8216;OK&#8217; and &#8216;Cancel&#8217; button by default (can be set to false)<\/li><li>shows a check box, which when set will remember the decision and not ask again (can be set to false)<\/li><li>the show() method returns a <a href=\"http:\/\/dojotoolkit.org\/api\/1.5\/dojo\/Deferred\">dojo.Deferred()<\/a> which allows you to simulate the code blocking<\/li><li>underlay can be hidden<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <a title=\"ConfirmDialog widget\" href=\"\/articles\/dojo-confirmdialog.php\">demo of the DialogConfirm widget<\/a> shows you how to halt code execution by just using the then() method of the returned deferred as follows:<\/p>\n\n\n\n<pre><code class=\"language-javascript\">var dialog = new snet.DialogConfirm({\n   title: 'Confirm',\n   content: '&lt;p&gt;Dialog content lore ipsum&lt;\/p&gt;'\n});\ndialog.show().then(function(remember) {\n   \/\/ user pressed ok button\n &nbsp; \/\/ remember is true, when user wants you to remember his decision (user ticked the check box)\n   if (remember) {\n      \/\/ do something and never show this dialog again\n   }\n   else {\n      \/\/ do something\n   }\n}, function() {\n   \/\/ user pressed cancel button\n   \/\/ do something\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Update 30.03.2018: Version 1.10 of dojotoolkit includes a dijit\/DialogConfirm now, which is simply a dijit\/Dialog featuring &#8216;OK&#8217; and &#8216;Cancel&#8217; buttons, no blocking though. Update 15.10.2012: Code is available on githup now. I also fixed updating the content of the dialog after creation (e.g. dialog.set(&#8216;content&#8217;, newContent) overwrote the confirm\/cancel buttons because they were part of the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.speich.net\/articles\/en\/2011\/01\/02\/creating-a-blocking-confirm-dialog-with-dojo\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating a &#8216;blocking&#8217; confirm dialog with dojo&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[19,24,26],"class_list":["post-291","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-confirm","tag-dijit","tag-dojo","entry"],"_links":{"self":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/comments?post=291"}],"version-history":[{"count":2,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/291\/revisions"}],"predecessor-version":[{"id":1690,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/291\/revisions\/1690"}],"wp:attachment":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/media?parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/categories?post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/tags?post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}