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?

5
  • 1
    Do you realize that f2 doesn't actually return anything? Commented Apr 6, 2011 at 2:53
  • @Matt Ball, I know, because I have no idea how to return something from an async function. Commented Apr 6, 2011 at 3: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? Commented Apr 6, 2011 at 3:35
  • @Matt Ball, I edited my question to better show what I want. Commented Apr 6, 2011 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. Commented Apr 6, 2011 at 17:48

3 Answers 3

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);
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
You need to put the OnBegin inside of the .then() callback.
The ASP.NET MVC part of the question is here: stackoverflow.com/questions/5572573/…
0

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

Comments

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); });

1 Comment

f1() was triggered by an event

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.