JSF 2.0 javascript onload / oncomplete [英] JSF 2.0 javascript onload/oncomplete

查看:155
本文介绍了JSF 2.0 javascript onload / oncomplete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用MyFaces 1.1 + Tomahawk + ​​Ajax4JSF的长项目之后,我正在使用Myfaces 2.0。 2.0绝对是一个巨大的进步。虽然我似乎无法获得一个onload javascript工作。我把它放在脑子里:

I'm working with Myfaces 2.0 after a long project using MyFaces 1.1 + Tomahawk + Ajax4JSF. 2.0 is definitely a huge improvement. Although I can't seem to get an onload javascript to work. I stuck this in my head:

<script type="text/javascript">
    window.onload = function () {
        var fileDownload = document.getElementById('userManagementForm:fileDownload');
        if (fileDownload.value == 'true') {
            fileDownload.value = false;
            window.open('Download');
        }
    }                   
    </script>

但它没有执行。我看了一下源myfacecs JS,我注意到他们用onload做了一些事情。没有太多细节,但它似乎阻止了我重写onload事件的尝试。还有另外一种方法,可能直接调用JSF库?

But it doesn't execute. I looked a bit at the source myfacecs JS and I noticed they do some stuff with the onload. Didn't get too much into the details but it seems to block my attempt at overriding the onload event. Is there another way around this, maybe a direct call to the JSF library?

另一个问题,关于同一主题。 Ajax4JSF有一个属性oncomplete,它可以让你在Ajax请求完成后运行一些Javascript。 MyFaces 2.0中是否有与f:ajax等效的?想想看,我没有尝试过event =oncomplete,也许它会起作用。

Another question, sort of on the same topic. Ajax4JSF had an attribute "oncomplete" which would let you run some Javascript after the Ajax request completed. Is there an equivalent in MyFaces 2.0 with the f:ajax? Come to think of it I haven't tried event="oncomplete", maybe it'll work.

推荐答案

好的,所以我想出来了。

Okay so I figured it out.

加载脚本稍后被覆盖的页面顶部

Loading the script at the top of page later gets overridden

如果我在页面末尾加载脚本它会覆盖onload,但它也会破坏JSF机制。因此,更好的解决方案是使用JSF的onload事物并将脚本放在页面的末尾。在挖掘了JSF源之后,我找到了这个函数。

If I load the script at the end of the page it overrides the onload, but it also breaks the JSF mechanism. So the better solution was to user JSF's onload thing and put the script at the end of the page. After digging through the JSF source a bit I found the function.

<script type="text/javascript">
        myfaces._impl.core._Runtime.addOnLoad(window, checkDownloadFile);
</script>

当然,我必须定义checkDownloadFile,它可以完成原始帖子中的内容。

And of course I had to define checkDownloadFile which does what I had in the original post.

我也想出了oncomplete。 f:ajax标签有一个被调用3次的onevent属性。执行前一次和之后两次。我相信在完成请求后立即,然后在更新页面组件后再次。回调函数采用具有状态的事件参数。因此,在您的回调函数中,您可以检查状态以决定是否执行某些操作。状态是开始,完成和成功。所以对于我的情况,我有:

I also figured out for the oncomplete. The f:ajax tag has an onevent attribute which gets called 3 times. Once before execution and twice after. I believe once immediately after completing the request, then again after updating the page components. The callback function takes an event parameter which has a status. So in your callback function you check the status to decide whether or not to do something. The statuses are begin, complete, and success. So for my case I have:

<f:ajax execute="userManagementForm" event="click" onevent="myEvent" render="userManagementForm:results userManagementForm:workarea userManagementForm:fileDownload" />

然后脚本:

    function myEvent(e) {
        if (e.status == 'success') {
            checkDownloadFile();
        }
    }

这篇关于JSF 2.0 javascript onload / oncomplete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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