每次ajax调用之前和之后的Javascript

时间:2022-10-08 09:06:47

Since I'm working with TinyMCE (please don't go into "Primefaces has an editor" or anything similar) I need to execute a small piece of Javascript before and after every Ajax-call. I'd prefer not to edit every Ajax-call for this since there are a lot (and doing so will be bad practice for any future maintenance).

由于我使用的是TinyMCE(请不要进入“Primefaces has an editor”或类似的内容),所以我需要在每次ajax调用前后执行一小段Javascript。我不希望对每个ajax调用都进行编辑,因为有很多这样做(这样做对将来的任何维护都是不好的做法)。

What would be the most elegant solution to execute Javascript pre- and post- any Ajax-call on the page?

在页面上执行任何ajax调用的最优雅的解决方案是什么?

Note: I'm using a custom composite for the TinyMCE-textarea. Any events too this object would also suffice. Though keep in mind that the actual Ajax-trigger might be invoked by a totally different object (though could nevertheless rerender the composite).

注意:我正在为TinyMCE-textarea使用自定义组合。任何事件,这个对象也足够了。尽管要记住,实际的ajax触发器可能被一个完全不同的对象调用(尽管仍然可以重新运行复合)。

2 个解决方案

#1


7  

Use the jsf.ajax.addOnEvent handler.

使用jsf.ajax。addOnEvent处理程序。

jsf.ajax.addOnEvent(function(data) {
    switch(data.status) {
        case "begin":
            // This is invoked right before ajax request is sent.
            break;

        case "complete":
            // This is invoked right after ajax response is returned.
            break;

        case "success":
            // This is invoked right after successful processing of ajax response and update of HTML DOM.
            // In case you're interested in error handling, use jsf.ajax.addOnError handler.
            break;
    }
});

Just put it in a JS file which is included by <h:outputScript target="head"> in the <h:body> of the desired pages. This will ensure that it's loaded after JSF's own jsf.js containing the jsf namespace.

只需将其放入一个JS文件中,该文件包含在所需页面的 中的 。这将确保在JSF自己的JSF之后加载它。包含jsf名称空间的js。

#2


0  

There is an onevent attribute of JSF's <f:ajax> tag, that's used to perform AJAX tasks within a JSF application. Basically it specifies a JavaScript function that's called thrice: before AJAX call is sent, after AJAX response is received and after HTML DOM is updated. Exactly for this purpose PrimeFaces has split those callbacks into onstart, onsuccess, oncomplete and onerror respectively.

JSF的 标记有一个onevent属性,用于在JSF应用程序中执行ajax任务。基本上,它指定了一个JavaScript函数,这个函数被称为三次:在发送AJAX调用之前,在接收AJAX响应之后,在更新HTML DOM之后。正是出于这个目的,PrimeFaces将这些回调分别分为onstart、onsuccess、oncomplete和onerror。

Not to be repetitive, you can find usage example in the answer to How to re-execute javascript function after form reRender?.

不要重复,在如何在表单重新运行后重新执行javascript函数的答案中可以找到用法示例。

#1


7  

Use the jsf.ajax.addOnEvent handler.

使用jsf.ajax。addOnEvent处理程序。

jsf.ajax.addOnEvent(function(data) {
    switch(data.status) {
        case "begin":
            // This is invoked right before ajax request is sent.
            break;

        case "complete":
            // This is invoked right after ajax response is returned.
            break;

        case "success":
            // This is invoked right after successful processing of ajax response and update of HTML DOM.
            // In case you're interested in error handling, use jsf.ajax.addOnError handler.
            break;
    }
});

Just put it in a JS file which is included by <h:outputScript target="head"> in the <h:body> of the desired pages. This will ensure that it's loaded after JSF's own jsf.js containing the jsf namespace.

只需将其放入一个JS文件中,该文件包含在所需页面的 中的 。这将确保在JSF自己的JSF之后加载它。包含jsf名称空间的js。

#2


0  

There is an onevent attribute of JSF's <f:ajax> tag, that's used to perform AJAX tasks within a JSF application. Basically it specifies a JavaScript function that's called thrice: before AJAX call is sent, after AJAX response is received and after HTML DOM is updated. Exactly for this purpose PrimeFaces has split those callbacks into onstart, onsuccess, oncomplete and onerror respectively.

JSF的 标记有一个onevent属性,用于在JSF应用程序中执行ajax任务。基本上,它指定了一个JavaScript函数,这个函数被称为三次:在发送AJAX调用之前,在接收AJAX响应之后,在更新HTML DOM之后。正是出于这个目的,PrimeFaces将这些回调分别分为onstart、onsuccess、oncomplete和onerror。

Not to be repetitive, you can find usage example in the answer to How to re-execute javascript function after form reRender?.

不要重复,在如何在表单重新运行后重新执行javascript函数的答案中可以找到用法示例。