1

My preRenderView event invokes OmniFaces Ajax utility's oncomplete() method to execute javascript on the client. I just tested this, and the javascript is not executed on the client.

My f:event is below.

<f:metadata>
    <o:enableRestorableView/>
    <f:event type="javax.faces.event.PreRenderViewEvent" listener="#{pageNavigationController.preRenderView()}"/>
</f:metadata>

My pageNavigationController.preRenderView() is below,

public void preRenderView() {
    if (initLayoutForPage) {
        // initialize layout for page
        layoutController.initializeForPage(page);
        initLayoutForPage = false;
    }
    if (usersController != null) {
        usersController.preRenderView();
    }
}

which calls usersController.preReviewView() below, which invokes Ajax utility's oncomplete().

public void preRenderView() {
        Ajax.oncomplete("document.getElementById('menuBarForm:btnLogout').click();");
}

Currently in production, the javascript above works as designed; this javascript is called as follows.

    <script>window.onbeforeunload = function() { document.getElementById('menuBarForm:btnLogout').click(); }</script>

So, why is the javascript not being executed on the client?

EDIT: I no longer need this functionality as I just figured out a workaround for the original problem, but I would appreciate an answer to this question. Thanks. :)

Howard
  • 792
  • 8
  • 43
  • I can't reproduce your problem in MyFaces nor Mojarra. To exclude the one and other, is the oncomplete script present in the ajax response or not? (check HTTP traffic log in browser's webdeveloper toolset) Try substituting with an `alert('peek-a-boo')` to have some visual feedback. – BalusC Dec 12 '12 at 11:18
  • I just confirmed that Ajax.oncomplete() works as designed even when executed during/via preRenderView event. OmniFaces 'Ajax' utility is for 'Ajax' requests. I was under the assumption that OmniFaces Ajax.oncomplete() would send javascript to client during preRenderView on a FPR (full page refresh or non-AJAX request). Thanks for the response, BalusC. – Howard Dec 12 '12 at 18:37
  • You'd rather like to use `` instead or maybe ``, depending on the concrete functional requirement. – BalusC Dec 12 '12 at 18:39
  • Excellent idea/recommendation! Hmmm, I only need the javascript to be executed under one #{applicationScopeBean.property == true}, and since I'm using TomEE/OpenEJB, render="..." is quite expensive, so I'd rather include this via ui:include src="#{applicationScopeBean.property ? '/onloadScriptToFireAfterLogin.xhtml' : 'noOnloadScriptToFireAfterLogin.xhtml')}". – Howard Dec 12 '12 at 19:09
  • You can if necessary also just use `` around the component. – BalusC Dec 12 '12 at 19:15
  • Okay, researching here on SO, how to use , but I'm wondering how expensive is compared to ui:include, ui:fragment, and/or rendered="..." – Howard Dec 12 '12 at 19:29
  • JSTL runs once during view build time (`` also, but that isn't meant to include scripts). But I really wonder, what is TomEE doing that `rendered` attribute is "quite expensive"? Doesn't sound very good. – BalusC Dec 12 '12 at 19:35
  • Okay, I read your answer here, http://stackoverflow.com/a/3343681/933054. Good question about TomEE and rendered. All I know, my CDI-managed-bean app was running very very slow in TomEE until I tuned my JPA queries and replaced frequently-accessed-pages with rendered="#{EL expression}" with a ui:include src="..." equivalent, but now I see I could have used . – Howard Dec 12 '12 at 19:40
  • Oh, just remembered, I was told that rendered="#{EL}" gets evaluated 6 times (at least) with/by TomEE. – Howard Dec 12 '12 at 20:01

1 Answers1

1

I just confirmed that Ajax.oncomplete() works as designed even when executed during/via preRenderView event. OmniFaces 'Ajax' utility is for 'Ajax' requests. I was under the assumption that OmniFaces Ajax.oncomplete() would send javascript to client during preRenderView on a FPR (full page refresh or non-AJAX request).

Howard
  • 792
  • 8
  • 43