Wicket 1.5 Ajax has been edited by Matej Knopp (Aug 27, 2008).

(View changes)

Content:

I'm in process of rewriting the Ajax support for 1.5. The current (very experimental) code is available at http://svn.apache.org/repos/asf/wicket/sandbox/knopp/experimental in package org.apache.wicket.ajaxng (will be later renamed to ajax replacing current ajax classes).

Some of the improvements over current Ajax implementation.

  • Should be much more stable and solid than current implementation. It supports timeout for the Ajax request and timeout for the processing afterwards. The new ajax pipeline should never hang with "chanel busy. postponing" like to current one can.
  • Much smaller impact on generated markup file. Current ajax generates markup such as
    code-keyword">var wcall=wicketAjaxGet('../?wicket:interface=:2:c1::IBehaviorListener:1:1',null,null, function() {return Wicket.$('c12') != null;}.bind(this));

    while for the same effect new Ajax implementation only generates

    W.e('click',{c:"c12",b:0});
  • The call is no longer part of actual element, rather than that it's executed as onDomReady _javascript_
  • Adding custom before/after handlers, preconditions and parameters no longer needs ugly string concatenations
  • Improved throttling
  • TextField/TextArea selection and cursor position is preserved when the element is refreshed with Ajax
  • AjaxRequestTarget allows to register _javascript_ executed right before and right after component replacement. The _javascript_ is asynchronous and the rest of the pipeline waits until the _javascript_ calls the notify method (can be used for animations). Also the actual replacement call can be customized.
  • AjaxRequestTarget also allows prepend and append _javascript_s to be asynchronous.


Usage of new API

AjaxRequestAttributes defines possible configuration options for AjaxRequest.

Example of creating Ajax link that displays an confirmation window:

add(new AjaxLink("link") 
  {
    public AjaxRequestAttributes getAttributes()
    {
      return new AjaxRequestAttributes(super.getAttributes()) 
      {
        public FunctionList getPreconditions()
        {
          return super.getPreconditions()
           .add("function(requestQueueItem) { return confirm('Really?'); }");
        } 
      }
    }
    public void onClick(AjaxRequestTarget target)
    {
      ...
    }
  }

Note: Asynchronous preconditions are also supported, see AjaxRequestAttributes#getPreconditions.

Example of Ajax link with custom URL parameters.

add(new AjaxLink("link") 
  {
    public AjaxRequestAttributes getAttributes()
    {
      return new AjaxRequestAttributes(super.getAttributes()) 
      {
        public Map<String, Object> getUrlArguments()
        {
          Map<String, Object> args = super.getUrlArguments();
          args.put("param1", "value1");
          return args;
	}
      }
    }
    public void onClick(AjaxRequestTarget target)
    {
      ...
    }
  }

Example of Ajax link with URL parameters added dynamically from _javascript_.

add(new AjaxLink("link") 
  {
    public AjaxRequestAttributes getAttributes()
    {
      return new AjaxRequestAttributes(super.getAttributes()) 
      {
        public FunctionList getUrlArgumentMethods()
        {
          return super.getUrlArgumentMethods()
            .add("function(requestQueueItem) { return { x:4, y:someJavascriptExpression() }; }");
        }
      }
    }
    public void onClick(AjaxRequestTarget target)
    {
      ...
    }
  }

Reply via email to