I have a custom behavior that contributes Javascript to the header.  In order 
to prevent collisions with other components that may also be using that 
behavior, I include it using TextTemplateHeaderContributor.  Like so:

public class CustomBehavior extends AbstractBehavior {

  private Component component;

  . . .

  @Override
  public void bind(Component component) {
    this.component = component
    component.setOutputMarkupId(true);

    . . .

    component.add(TextTemplateHeaderContributor.forJavaScript(getClass(), 
"javascriptSource.js", variables));
  }

}


This works great, unless I add this behavior to a component that gets added to 
the page as part of an Ajax request.  In that case, the Javascript does not get 
added to the page, so when the component looks up the Javascript it needs to 
function, it's not there.

I'm not sure if this is related to 
https://issues.apache.org/jira/browse/WICKET-618 or not, but that bug was 
resolved 'Won't Fix'.

I tried tracing through the Ajax code, and if I understand it properly, the 
issue seems to come down to line 1445 of wicket-ajax.js:

1441    var text = Wicket.DOM.serializeNodeChildren(node);
1442
1443    var id = node.getAttribute("id");
1444
1445    if (typeof(id) == "string" && id.length > 0) {                          
        
1446      // add javascript to document head
1447      Wicket.Head.addJavascript(text, id);
1448    } else {
1449      try {
1450        eval(text);
1451      } catch (e) {
1452        Wicket.Log.error(e);
1453      }
1454    }


It would appear that because the script tag generated by 
TextTemplateHeaderContributor does not contain an 'id' attribute, the 
javascript is not rendered on the page.  (However it is still evaluated, via 
line 1450, which explains why when I added an alert() call to my javascript 
file, it got executed.  Imagine my initial confusion.)

Is this expected behavior?  A bug?  Is there a workaround?  Am I even 
interpreting the issue correctly?  If I were using a static Javascript file, I 
think there are calls I can use to include an 'id' attribute, and I may have to 
rewrite my javascript so I can make it static (if possible).

Picking up on suggestions I read on the mailing list archives, I also tried:

  public void renderHead(IHeaderResponse response) {
    
response.renderOnDomReadyJavascript(TextTemplateHeaderContributor.forJavaScript(getClass(),
 "popupBehavior.js", variables).toString());
  }

and:

  public void onRendered(Component component) {
    if(RequestCycle.get().getRequestTarget() instanceof AjaxRequestTarget) {
      AjaxRequestTarget target = (AjaxRequestTarget) 
RequestCycle.get().getRequestTarget();
      
target.appendJavascript(TextTemplateHeaderContributor.forJavaScript(getClass(), 
"popupBehavior.js", variables).toString());
    }
  }

Neither alternative worked.  At this point I'm not even sure they make sense, 
but I tried them before I had dug in and (hopefully) traced down the problem.

Thank you for any help that anyone can provide on this issue.

Joel


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to