改变异步jQuery的对话是同步的? [英] Change the asynchronous jQuery Dialog to be synchronous?

查看:202
本文介绍了改变异步jQuery的对话是同步的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的工作与jQuery的对话框取代警报/确认。

Currently, I'm working to replace "alert'/"confirm" with the jquery dialog.

但是大多数传统codeS的是用一些异步的方式,这使得它很难改变。有没有什么办法,使以同步的方式jQuery的对话工作? (不要使用循环或回调函数)

   For example:
   function run()
   { 
      var result = confirm("yes or no");
      alert( result );
      \\more codes here
   }

在这个例子中,警报等codeS将用户的选择后执行。

In this example the alert and other codes will be executed after user's choice.

如果我们使用jQuery对话框
VAR的结果= $ dialog.open()
它将继续执行该警报,这是异步的。

If we use jquery dialog var result = $dialog.open() It will continue to execute the alert, which is asynchronous.

目前,我的解决方案是使用回拨功能在确定|取消功能。
        例如:

Currently, my solution is to use call back function in the OK|Cancel function. For example:

    OK: function ()
   {
       $dialog.close();
       alert("yes");
       //more codes here
    }

这个方法有效,但也很难让所有的同步codeS成为异步的,这需要大量的变化(见下面的例子)。 所以我在寻找同步jQuery的对话,这可能??

This method works but it is difficult to make all the synchronous codes become asynchronous, which requires a lot of change (see the following example). So I'm looking for the synchronous jQuery Dialog, is it possible??

例如:(真正codeS比下面的例子复杂得多)

For example: ( The real codes are much more complicated than the following example)

     function f1()
    {
          if ( confirm("hello") )    f2();
          alert("no");
    } 

    function f2()
    {
          if( confirm("world") )    f3();
          alert("no");
    }

    function f3()
    {
          return confirm("!") ;
    }


另外一个例子:


Another example:

vendorObject.on('some-event', function() {
    if(confirm("Do you really want to do that?")) {
        return true;
    }
    else {
        return false; // cancel the event
    }
});

...这里的供应商对象触发一个事件,其中有如果用户确认要取消。 。同步

... here the vendor object fires an event, which has to be cancelled if the user confirms. The event can only be cancelled if the event handler returns false - synchronously.

推荐答案

简短的回答是否定的,你将无法保持你的code同步。这里的原因:

The short answer is no, you won't be able to keep your code synchronous. Here's why:


  • 在为了使这是同步的,当前执行的脚本将不得不等待为用户提供输入,然后继续

  • 虽然是一个当前正在执行的脚本,用户无法与UI交互。实际上,用户界面​​甚至不更新,直到后脚本完成执行。

  • 如果脚本无法继续下去,直到用户提供输入,直到脚本完成后用户无法提供输入,你永远得到的最接近的是挂起浏览器。

要说明此行为,调试code和设置就行了一个破发点之后,改变用户界面的一行:

To illustrate this behavior, debug your code and set a break point on the line following a line that changes the UI:

$("body").css("backgroundColor", "red");
var x = 1;  // break on this line

请注意,您的网页背景尚不红色。直到你恢复执行和脚本执行完它不会变成红色。你也无法点击在你的页面的所有链接,而你已经有了脚本执行暂停与你的调试器。

Notice that your page background is not yet red. It won't change to red until you resume execution and the script finishes executing. You are also unable to click any links in your page while you've got script execution paused with your debugger.

有一个是例外的警报()确认()。这些浏览器控件,并区别对待不是实际的网​​页用户界面元素。

There is an exception to this rule for alert() and confirm(). These are browser controls, and are treated differently than actual web page UI elements.

好消息是,它真的不应该是很困难的转换你的code。 presumably,你的code目前看起来是这样的:

The good news is that it really shouldn't be very hard to convert your code. Presumably, your code currently looks something like this:

if (confirm("continue?")) {
    // several lines of code if yes
}
else {
    // several lines of code if no
}
// several lines of code finally 

您异步版本可以创建一个函数 ifConfirm(文字,yesFn,noFn,finallyFn)和你的code看起来大同小异:

Your asynchronous version could create a function ifConfirm(text, yesFn, noFn, finallyFn) and your code would look very much the same:

ifConfirm("continue?", function () {
    // several lines of code if yes
},
function () {
    // several lines of code if no
},
function () {
    // several lines of code finally 
});

编辑:在回答您添加到您的问题更多的例子,不幸的code将需要进行重构。这只不过是无法的有自定义同步确认对话框。要使用在一个事件需要继续或取消该方案自定义的确认对话框中,你只需要随时取消该事件,并模仿在 yesFn 回调事件

In response to the additional example you added to your question, unfortunately that code will need to be refactored. It is simply not possible to have synchronous custom confirmation dialogs. To use a custom confirmation dialog in the scenario where an event needs to either continue or cancel, you'll just have to always cancel the event and mimic the event in the yesFn callback.

例如,一个链接:

$("a[href]").click(function (e) {
    e.preventDefault();
    var link = this.href;
    ifConfirm("Are you sure you want to leave this awesome page?", function () {
        location.href = link;
    });
});

或者,一个表单:

Or, a form:

$("form").submit(function (e) {
    e.preventDefault();
    var form = this;
    ifConfirm("Are you sure you're ready to submit this form?", function () {
        form.submit();
    });
});

这篇关于改变异步jQuery的对话是同步的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆