PDA

View Full Version : Procedural alert dialog?


krunchy
09-16-2008, 05:13 PM
I'm struggling with how to right code to enact a generic dialog for prompting the user a yes or no question. The class will be a modal dialog similar to Javascript alert that requires them to decide before proceding. I want to do it like this, but I don't really think this kind of flow is supported:


<button>Submit
<handler name="onclick">
var result = confirm("Are you sure?");
if (result) { // Yes } else { // No }
</handler>
</button>


Instead, I imagine it's like:


<button>Submit
<handler name="onclick">
confirm_window.appear('Are you sure?');
// And then confirm_window's appear() makes it modal and visible
</handler>
<handler name="onvalue" reference="confirm_window.decision">
if (confirm_window.decision) { // Yes } else { // No }
</handler>
</button>


Is there a better way to do it? I want it to be the execution of it as generic as possible, so I can use this dialog anywhere.

rcyeager
09-17-2008, 08:49 AM
The pattern I use is based on the alertDialog class, using it like this:


<method name="handleUserAction">
<![CDATA[
if (needToConfirmActionFirst)
{
var dlg = new alertDialog(canvas, {button1: "Yes", button2: "No"});
new LzDelegate(this, "doYesNoResult", dlg, "onresult");
dlg.open("Confirm", "Do you want to...?");
}
else
{
doAction();
}
]]>
</method>

<method name="doYesNoResult" args="result">
if (result) doAction();
</method>


The main difference in my code is that I instantiate new modal dialogs, in order to associate the result handlers in methods physically next to where the dialogs are created. This makes following the code logic easier IMO. Note that my sample code is not cleaning up the delegate, which should be done to avoid a leak.

I've also created a new base "modalWindow" class to keep a stack of modal dialog references, in order to be able to have modal dialogs display on top of each other. With the default implementation, when the topmost modal dialog is closed the underlying modal dialogs do not retain their "modalness".

Robert Yeager
http://www.qrowd.com
http://www.cooqy.com

krunchy
09-18-2008, 07:47 AM
Thanks for the advice... your solution seems like a pretty good combination of my two ideas, and I'll just fiddle around to see what I can come up with. I don't think I have to worry about multiple modal windows open at once, but your tip regarding that will be handy just in case. Thanks again.

PS ... both your sites are fantastic work!

rcyeager
09-18-2008, 08:58 AM
Thanks so much for the kind feedback!

As you can tell, I obviously have too much time on my hands...a developer gone wild. :-)

Robert Yeager
http://www.qrowd.com
http://www.cooqy.com