有什么方法可以中止AjaxBehaviorEvent侦听器或将其他信息传递给f:ajax onevent? [英] is any way to abort AjaxBehaviorEvent listener or pass additional info to f:ajax onevent?

查看:72
本文介绍了有什么方法可以中止AjaxBehaviorEvent侦听器或将其他信息传递给f:ajax onevent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在控制器中创建了侦听器,诸如此类.

I have created listener in my controller, something like that..

public class FooController {
    public void doSomething(AjaxBehaviorEvent evt) {
        closeDialogFlag = true;
        ..
        if(!isValid){
            closeDialogFlag = false;
        }
    }
}

还有我的JSF按钮.

<h:commandButton value="Do somenthing">
    <f:ajax 
        listener="#{fooController.doSomething}"
        execute=":editDialogForm:processTab" render="@this :editDialogForm:processTab :messageViewer:messageViewerPanel :results"
        onevent="function(data){ if(data.status === 'success' &amp;&amp; #{fooController.closeDialogFlag} ) { $('#migrationEditDialogModal').modal('hide'); } }"
    />
</h:commandButton>

但是我的 closeDialogFlag 在ajax请求完成后不起作用,因为值#{fooController.closeDialogFlag} 会在页面加载时提取,并且在AJAX之后不会更新要求.我需要刷新页面才能在输出代码中看到新值.

But my closeDialogFlag doesn't work after ajax request is completed, bacause value #{fooController.closeDialogFlag} is extracted when page is loading and is not updated after AJAX request. I need to refresh a page to see new value in output code.

问题是:如何在侦听器的 onevent = function(data)中将其他信息传递给 data ?(此解决方案更适合我,因为它在各种情况下都会有用) OR 是否可以通过任何方法在侦听器中终止我的请求并强制我的AJAX请求返回除 success ?

The question is: How can I pass additional information to data in onevent=function(data) in my listener? (This solution is more appropriate to me because it will be useful in various cases) OR Is any way to abort my request in my listener and force my AJAX request to return another status than success?

PS:我不想使用Primefaces,Richfaces等.

PS: I don't want use Primefaces, Richfaces etc.

修改:小修正.如果我添加到我的 f:ajax 中以赋予属性 render @this ,则成功提取我的标志为 true 但实际上在我的AJAX请求之后,我的对话框没有被JavaScript函数隐藏.

Edit: small correction. If i add to my f:ajax to attribute render value @this, than my flag is extracted successful as true but in fact after my AJAX request my dialog is not hidding by JavaScript function.

推荐答案

以下是针对您的特定问题的小技巧:

Here's a little trick for your specific problem:

  1. 在同一< h:form> 中创建一个< h:inputHidden id ="closeDialogFlag" value =#{fooController.closeDialogFlag}"/> >作为您的< h:commandButton .../> .在这里,我想#{fooController.closeDialogFlag} 的类型为 boolean .
  2. 在您的f:ajax中:
  1. Create a <h:inputHidden id="closeDialogFlag" value="#{fooController.closeDialogFlag}"/> in the same <h:form> as your <h:commandButton ... />. Here, I suppose #{fooController.closeDialogFlag} is of type boolean.
  2. In your f:ajax:
  1. 呈现组件 closeDialogFlag .
  2. 将事件的条件更改为: function(data){if(data.status ==='success'& amp;& amp; $('input#closeDialogFlag').val()==='true'){$('#migrationEditDialogModal').modal('hide');}}

您将必须更改 $('input#closeDialogFlag')指向正确的ID(取决于父母的ID).

You will have to change $('input#closeDialogFlag') to point to the correct ID (will depend on the parents' id).

之所以可行,是因为当 data.status ==='成功'时,组件已经使用其新值进行渲染.

This works because when data.status === 'success', the components are already rendered with their new values.

这篇关于有什么方法可以中止AjaxBehaviorEvent侦听器或将其他信息传递给f:ajax onevent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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