4

I have some jQuery code that runs on every $(document).ready() event. I also have some <f:ajax> tags that do re-rendering of some parts of the page.

I noticed that when I rerender a component, the $(document).ready() doesn't get called.

Is there a way to run javascript code after every <f:ajax> rerendering? (I can technically use the onevent tag but that's the unfavourable solution to me since I will have to call the function on every <f:ajax>)

Just for illustration, this is the jQuery Code:

$(document).ready(function() { ...CODE... }

And the JSF code:

<f:ajax event="click" render=":someComponentID"/>

Thanks!

UPDATE This question might be a duplicate of this one. I'm looking into it.

UPDATE2 This is easily solvable using the JSF Javascript event binding:

jsf.ajax.addOnEvent(function(data){
  if (data.status === 'success') {
    // Do stuff here
  }
}

Read more about it under the JSF 2 reference

Community
  • 1
  • 1
Ben
  • 10,020
  • 21
  • 94
  • 157

2 Answers2

4

The $(document).ready() event fires once, when the DOM has fully loaded from the initial load.

An ajax event may change the DOM, but it doesn't reload the page, so the $(document).ready() won't fire again.

I noticed the way Google solved this problem in Gmail is by using a high frequency timer.

Chris May
  • 607
  • 7
  • 12
  • Interesting Article! This might be the answer. I'll wait and see if there are more ideas. – Ben Jun 02 '11 at 13:01
  • on second thought, a Timer may not work here. I need to know when the event took place. – Ben Jun 02 '11 at 13:14
0

Use events and listeners..

Have a read at Ajax tag events and listeners

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • As I said in the question "I can technically use the onevent tag but that's the unfavourable solution to me since I will have to call the function on every ". – Ben Jun 02 '11 at 13:41
  • The post i linked to does not use the `onevent` attribute but the `listener` attribute. In anyway, jsf provide a callback model (event/listeners).. don't know why you do not want to use it.. You ***want*** to call the function on every `f:ajax` .. right ? – Gabriele Petrioli Jun 02 '11 at 14:28
  • would using a `h:outputScript` and targeting with the `render` attribute of the `ajax` call work ? (*and in that script do what you want..*) – Gabriele Petrioli Jun 02 '11 at 14:39