Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
karelvuong
Posts: 10
Registered: ‎07-04-2013
My Carrier: Fido
Accepted Solution

BB10 - Dialog Dilemma & Callback Chaos

Hello all,

 

I'll try and make this short. Basically, when the user hits delete, I want a dialog to show up asking them to confirm. If they hit yes, the associated item ID will be deleted.

 


 

This works:

 

function setOnclickDelete(paramID) {
   document.getElementById('overflow-btn-delete').onclick = function() {

      var r = confirm("Permanently delete this Item?");
      if (r == true) {
         db.transaction(function (tx) {

            tx.executeSql('DELETE FROM ITEMS WHERE id=?', [paramID], function () {});
         });
         bb.popScreen(this);
      }

   };
}

 

But it's not the right way to do it. The title isn't customizable and thus reads "JavaScript Confirm". I have taken a look at the API and am about half way there.

 


 

Can detect what button the user presses (Delete or Cancel):

 

function setOnclickDelete(paramID) {
    document.getElementById('overflow-btn-delete').onclick = function() {

        standardDialog();

    };
}

 

function dialogCallBack(selection){
    alert(selection.return);
}

 

function standardDialog() {
   try {
      blackberry.ui.dialog.standardAskAsync(
      "Are you sure you want to permanently delete this item?",
      blackberry.ui.dialog.D_DELETE,
      dialogCallBack,
      {title : "Delete?"});
   }catch (e) {
      alert("Exception in standardDialog: " + e);
   }
}

 


 

Correctly passes the ID parameter but unable to detect what button was clicked. (Delete or Cancel):

 

function setOnclickDelete(paramID) {
    document.getElementById('overflow-btn-delete').onclick = function() {

        standardDialog();

    };
}

 

function dialogCallBack(paramID){
    alert(paramID);
}

 

function standardDialog() {
   try {
      blackberry.ui.dialog.standardAskAsync(
      "Are you sure you want to permanently delete this item?", 
      blackberry.ui.dialog.D_DELETE, 
      function() { dialogCallBack(paramID); }, 
      {title : "Delete?"});
   }catch (e) {
      alert("Exception in standardDialog: " + e);
   }
}

 

My solution appears to be a mix of the two, but I have been unsuccessful thus far in finding a solution. Any tips, solutions or help will be greatly appreciated.

 

Regards,

 

Karel Vuong

Please use plain text.
Developer
peardox
Posts: 1,227
Registered: ‎03-20-2011
My Carrier: 3, Orange, Vodafone

Re: BB10 - Dialog Dilemma & Callback Chaos

Why have you got a dialog within a dialog?

 

Anyway - your callback is wrong

 

Use dialogCallBack, not dialogCallBack(paramID)

 

The difference is like passing the address of a function vs calling a function in other languages




Click the like button if you find my posts useful!
Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,453
Registered: ‎04-12-2010
My Carrier: Bell

Re: BB10 - Dialog Dilemma & Callback Chaos

Hi karelvuong, did peardox's response answer your question? If so, can you please accept his reply as the solution to help future visitors to this issue? If not, please let us know and we'll do our best to help.

Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
Contributor
karelvuong
Posts: 10
Registered: ‎07-04-2013
My Carrier: Fido

Re: BB10 - Dialog Dilemma & Callback Chaos

So how would I go about passing a parameter? I want to delete the associated item using the given ID.

I want to first identify what button was clicked and if delete was selected then delete that item corresponding to the ID.
Please use plain text.
Developer
peardox
Posts: 1,227
Registered: ‎03-20-2011
My Carrier: 3, Orange, Vodafone

Re: BB10 - Dialog Dilemma & Callback Chaos

[ Edited ]

Ahh - right (not overly clear from the Q)

 

Here you go - one fully functional demo...

 

<!DOCTYPE html>
<html>
<head>
<title>Dialog Demo</title>
<meta name="viewport" content="user-scalable=no,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,height=device-height,width=device-width" />
<script type="text/javascript" src="local:///chrome/webworks.js"></script>

<script type="text/javascript">
var wwloaded = false;
var rippleLoaded = false;

function loader() {
	document.addEventListener('webworksready', function(e) {
		// This is a hack for Ripple calling webworksready multiple times
		if (rippleLoaded) return;
		rippleLoaded = true;

		wwloaded = true;
		});
	}
	
function dodelete(opt, param) {
	alert("Dialog returned " + opt + "\nParam = " + param);
	}
	
function dlgdemo(param) {
    if(!wwloaded) { alert('No WW'); return 0; };
try { blackberry.ui.dialog.standardAskAsync("Delete Item " + param + "?", blackberry.ui.dialog.D_DELETE, function(opt) { dodelete(opt.return, param); }, {title : "Delete Dialog"}); } catch (e) { alert("Exception in standardDialog: " + e); } } </script> </head> <body onload="loader();"> <!-- Button that when clicked calls dlgdemo passing a random number between 1 + 100 --> <input type="button" value="Open Dialog" onclick="dlgdemo(Math.floor((Math.random()*100)+1));" /> </body> </html>

 

Whenever you want to pass a parameter to a callback (you can't unless it's your own callback) you have to work around it as in dlgdemo() above using an anonymous function with the data you're interested in passing further on in scope of the callback.

 

The function(opt) {  dodelete(opt.return, param} } bit in dlgdemo() gets passed opt and has param in scope therefore dodelete() gets both the returned option from the dialog and the parameter you wanna pass

 

 

 

 




Click the like button if you find my posts useful!
Please use plain text.
Contributor
karelvuong
Posts: 10
Registered: ‎07-04-2013
My Carrier: Fido

Re: BB10 - Dialog Dilemma & Callback Chaos

Thanks for your help Simon. The BB developer community is seriously really helpful.

Is there a repository of these tutorials? It seems as though something like this should be included in the WebWorks samples.
Please use plain text.