0

My problem is better explained in code:

//This code is triggered before ajax ObBegin. But I need f1 to return a boolean to either cancel or continue the event.
f1();

function f1(){
    $.modalWindow.Open(); //This is an async method, this is where my problem lies.
    //I need to freeze here and wait on a return value from one of the events below.
}

//In the modal window:

//An event which waits for the click event
$('.cancelBtn').click(function(){
    //How do I send false back to f1?
    closeModalWindow();
});

$('.yesBtn').click(function(){
    //How do I send true back to f1?
    closeModalWindow();
});

So basically what happens is this:

  1. openModalWindow() opens a modal window that waits on a button click.
  2. I want to pass the value back to f1 and return it.

Is there a way to fix this?

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
  • 1
    Do you realize that `f2` doesn't actually return anything? – Matt Ball Apr 06 '11 at 02:53
  • @Matt Ball, I know, because I have no idea how to return something from an async function. – Shawn Mclean Apr 06 '11 at 03:16
  • You don't - you run a callback function (like @Alex's answer) or something fancier like `jQuery.Deferred` (my answer). But your question is unclear - could you show us some more code? – Matt Ball Apr 06 '11 at 03:35
  • @Matt Ball, I edited my question to better show what I want. – Shawn Mclean Apr 06 '11 at 16:49
  • Thanks for the edit - I edited my answer accordingly. I know that it doesn't exactly match the code structure in your question, but it should be pretty workable. – Matt Ball Apr 06 '11 at 17:48

3 Answers3

2

Use jQuery's Deferred objects. There's a good tutorial on it here, but you haven't actually shown enough of your own code for me to demonstrate how to wire it up with $.Deferred.


Here's a very basic demo of how to do this: http://jsfiddle.net/mattball/fNQ8J/. Basically, you have to pass callbacks around for asynchronous execution.

function openModalWindow(callback) {
    if (typeof callback !== 'function') callback = $.noop;
    $("#dialog-confirm").show().dialog({
        resizable: false,
        modal: true,
        buttons: {
            Yes: function() {
                $(this).dialog("close");
                callback(true);
            },
            No: function() {
                $(this).dialog("close");
                callback(false);
            }
        }
    });
}

function f1() {
    return $.Deferred(function(dfd) {
        openModalWindow(dfd.resolve);
    }).promise();
}

$('#clickme').click(function() {
    f1().then(function(result) {
        alert('f1 async returned: ' + result);
    });
});
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • the last code block of `click` should be an `OnBegin` for an ajax request, which is listening for a return value of true or false. – Shawn Mclean Apr 06 '11 at 20:16
  • unfortunately no :(. The ajax request gets sent anyways because f1 is async and didn't return a true/false (which should tell the `OnBegin` whether or not to send the query. – Shawn Mclean Apr 06 '11 at 20:33
  • You need to put the `OnBegin` inside of the `.then()` callback. – Matt Ball Apr 06 '11 at 20:43
  • The ASP.NET MVC part of the question is here: http://stackoverflow.com/questions/5572573/ajax-beginform-onbegin-confirmation-via-jquery-modal – Shawn Mclean Apr 06 '11 at 20:47
0

There's no good way to do this, no. You'll have to refactor f1 so it can deal with asynchronicity.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
0

f1() should be implemented as a callback for someAsyncFunc():

function someAsyncFunc(callback) {
    // open your modal window
    $(".theBtm").click(function() {
      // do your stuff
      if (typeof(callback) === "function") {
        callback(theValueYouWantToPass);
      }
    });
}

Called something like this:

someAsyncFunc(function(value) { f1(value); });
Alex
  • 64,178
  • 48
  • 151
  • 180