0

Let's say I have a function that is creating a 'confirm or cancel' dialog dynamically and binding click events to the OK and Cancel links.

function confirmOrCancelDialog() {
    //already created $dialog to popup on screen

    $dialog.find('a.confirm').click(function() {
        //close dialog
        return true;
    });

    $dialog.find('a.cancel').click(function() {
        //close dialog
        return false;
    });

}

Then, I am invoking the creation of this dialog from another function. I want to pass the result of the interaction to the invoking function.

function performAction() {
    var clickResult = confirmOrCancelDialog();

    if (clickResult === true) {
        //do some stuff
    }
}

Any guidance on how to do this would be appreciated. Thanks.

4
  • Impossible, it is an asynchronous event Commented Oct 9, 2013 at 22:32
  • Well then, some way to structure it so that it is synchronous. Commented Oct 9, 2013 at 22:32
  • You can use a deferred object. Commented Oct 9, 2013 at 22:34
  • Just use a callback, or call out of the click function. The click could occur a very long time in the future and it would make no sense to block while waiting (which doesn't happen, the function will just complete). Commented Oct 9, 2013 at 22:42

3 Answers 3

3
function confirmOrCancelDialog(someStuff) {
    //already created $dialog to popup on screen

    $dialog.find('a.confirm').click(function() {
        //close dialog
        someStuff(true);
        return true;
    });

    $dialog.find('a.cancel').click(function() {
        //close dialog
        someStuff(false);
        return false;
    });

}
function performAction() {
    confirmOrCancelDialog(function(clickResult){
        if (clickResult === true) {
            //do some stuff
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

This pattern is PERFECT. Thank you!
0

You could just add an on click event to all anchors within the dialog object, and then check to see if the clicked anchor has the confirm class (or cancel, either one) and return accordingly:

$('.dialog a').on('click', function(event) {
  if ($(this).hasClass('confirm')) { return true; }

  return false;
});

1 Comment

Would have done it in a similar fashion if I had any control over it... Thanks for your input sir!
0

Try this:

function confirmOrCancelDialog(callback) {
  //already created $dialog to popup on screen
  $dialog.find('a.confirm').click(function() {
    //close dialog
    callback(true);
  });

  $dialog.find('a.cancel').click(function() {
    //close dialog
    callback(false);
  });
}

function performAction() {
  confirmOrCancelDialog(function(clickResult){
    if (clickResult === true) {
      //do some stuff
    }
  });
}

3 Comments

I don't remember offhand if callback will still be in scope when it is called from within the click handlers. If you get a callback is undefined error, post a comment (even better, a jsfiddle) and I'll modify this to use a closure to save the callback reference.
Ha sorry @user1990577 I promise I didn't intentionally copy your answer <3
(why can't I post short comments...) @Plato ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.