等待确认对话框返回值

发布于 2024-12-18 10:24:09 字数 1647 浏览 1 评论 0原文

我有以下 jQuery 代码:

  <script type="text/javascript" charset="utf-8">
    $(function() { 

       initConfirmbinding();

       $('#dialog').dialog({
        autoOpen: false,
        width: 'auto',
        modal: true,

        buttons: {
              "Delete": function() { 
                     $(this).dialog("close");            
                     alert("You pressed OK!");
                     return true;
                     }, 
              "Cancel": function() { 
                     $(this).dialog("close"); 
                     return false;
                     } 
        }
      });

    function initConfirmbinding(){
           // Dialog Link 
           $('.confirm').click(function(){
                 return $('#dialog').dialog('open'); // Here it doesn't stops for the return value!!
           });
    }
  </script>

以及其他代码:

 ...
<c:foreach ...>
 <h:commandLink styleClass="confirm" action="#{myBean.delete}">
      <img src="/resources/images/delete-bw.png"/>    
 </h:commandLink>
<c/:foreach>

 ...
 <!-- ui-dialog -->
<div id="dialog" title="Question" style="display: none;">
        This item will be deleted, confirm?         
</div>

在用户确认 dialog 之后,我需要调用 myBean.delete,但它只是继续并使得调用时对话框仍打开。如何使对话框返​​回同步值?所以commandLink可以知道是否执行该动作。

我知道它可以使用回调,但回调不是一个选项,因为commandLink正在等待结果来决定执行操作,如果我使用回调,我将需要直接调用 commandLink.click 这会造成无限循环。

即使想,再想可能有一种方法可以使用回调,但是我如何发送到对话框在完成时调用哪个回调。

I have the following jQuery code:

  <script type="text/javascript" charset="utf-8">
    $(function() { 

       initConfirmbinding();

       $('#dialog').dialog({
        autoOpen: false,
        width: 'auto',
        modal: true,

        buttons: {
              "Delete": function() { 
                     $(this).dialog("close");            
                     alert("You pressed OK!");
                     return true;
                     }, 
              "Cancel": function() { 
                     $(this).dialog("close"); 
                     return false;
                     } 
        }
      });

    function initConfirmbinding(){
           // Dialog Link 
           $('.confirm').click(function(){
                 return $('#dialog').dialog('open'); // Here it doesn't stops for the return value!!
           });
    }
  </script>

And this other code:

 ...
<c:foreach ...>
 <h:commandLink styleClass="confirm" action="#{myBean.delete}">
      <img src="/resources/images/delete-bw.png"/>    
 </h:commandLink>
<c/:foreach>

 ...
 <!-- ui-dialog -->
<div id="dialog" title="Question" style="display: none;">
        This item will be deleted, confirm?         
</div>

I need to make the call to myBean.delete after the user confirms the dialog, but it just continues and makes the call with the dialog still open. How can I make the dialog to return a synchronized value? So the commandLink can know whether to execute the action or not.

I know it can use callbacks, but callbacks is not an option since the commandLink is waiting for the result to decide to execute the action, and if I use callbacks I will need to make a direct call to the commandLink.click which would make an infinite loop.

Even thought, thinking again there could be a way to use callbacks but how could I send to the dialog which callback to call when it's done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-12-25 10:24:09

没有办法让代码的执行停止并等待。您唯一能做的就是通过屏蔽对话框下的页面来创建模式对话框,以便用户必须在继续之前选择某些内容。

There is no way to make the execution of code stop and wait. The only thing you can do is to make a modal dialog by blocking out the page under the dialog so the user has to select something before continuing.

折戟 2024-12-25 10:24:09

在我看来,如果您将 bean 的 #{myBean.delete} 方法公开为 JavaScript 函数,并在 jQueryUI 对话框的 Delete 中相应地调用它,您的问题就可以解决。和取消处理程序。
如果您可以使用 RichFaces 库,请参阅 a4j:jsFunction
但在本例中,删除按钮单击必须是纯 ajax 调用。即,您可以在 onclick 中调用对话框,并且不指定任何操作。
否则,您将无法阻止回发的发生。

The way I see it, your problem can be solved if you expose the #{myBean.delete} method of bean as a javascript function and call it accordingly in jQueryUI dialog's Delete and Cancel handlers.
If you can use the RichFaces library, refer to a4j:jsFunction
But in this case the delete button click has to be pure ajax call. i.e. you can have the dialog invoked in the onclick and no action specified.
Otherwise there's no way you can stop the postback from happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文