Saturday, February 12, 2011

Wicket push

Wicketstuff push has dramatically changed at 1.4.13. But the API changes so much at that version, and I cannot make it work. Since the 1.4.12 uses very OLD dojo libraries, I want to migrate to the new version.

While looking the release documents of Cometd 2.1.0, I find that it supports jQuery bindings and Dojo bindings. So, I try to change the bindings to jQuery based on 1.4.12.

The files I have changed are two, CometdAbstractBehavior.java, CometdBehavior.java. And import javascripts from cometd-javascript-jquery-2.1.0.war.

public abstract class CometdAbstractBehavior extends AbstractDefaultAjaxBehavior {
    private static final long serialVersionUID = 1L;

    // FIXME: put this in application scope, we may have several webapp using
    // CometdBehavior in the same web container!
    private final static String cometdServletPath = getCometdServletPath();
 
    private static final ResourceReference COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "org/cometd.js");  
    private static final ResourceReference JQ_JSON2 =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/json2.js");
    private static final ResourceReference JQ_COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cometd.js");

    @Override
    public void renderHead(final IHeaderResponse response) {
        super.renderHead(response);
        if (channelId == null) {
            throw new IllegalArgumentException("ChannelId in a CometdBehavior can not be null");
        }
        response.renderJavascriptReference(COMETD);
        response.renderJavascriptReference(JQ_JSON2);
        response.renderJavascriptReference(JQ_COMETD);

        response.renderJavascript(getInitCometdScript(), "initCometd");
        final String cometdInterceptorScript = getCometdInterceptorScript();
        if (cometdInterceptorScript != null) {
            response.renderJavascript(cometdInterceptorScript, "Interceptor"
                    + getBehaviorMarkupId());
        }
        response.renderJavascript(getSubscriberScript(), "Subscribe"
                + getBehaviorMarkupId());
    }

    protected final CharSequence getInitCometdScript() {
        return getConfigureCometdScript() + getHandshakeCometdScript(); 
    }

    protected final String getConfigureCometdScript() {
        return "$.cometd.configure('" + cometdServletPath + "')\n";
    }
 
    protected String getHandshakeCometdScript() {
        return "$.cometd.handshake()\n";
    }
 
    public final CharSequence getSubscriberScript() {
        return "$.cometd.subscribe('/" + getChannelId() + "', "
                + getPartialSubscriber() + ");\n";
    }

public class CometdBehavior extends CometdAbstractBehavior {

    @Override
    public final String getCometdInterceptorScript() {
  
        final Map map = new HashMap();
        map.put("behaviorMarkupId", getBehaviorMarkupId());
        map.put("url", getCallbackUrl().toString());
  
        return new PackagedTextTemplate(CometdBehavior.class, "CometdDefaultBehaviorTemplate.js").asString(map);
    }

    @Override
    public final CharSequence getPartialSubscriber() {
        return "onEventFor" + getBehaviorMarkupId();
    }

Yes, it works. Thanks for chrome's developer tool. It helps me lot! And I also refer this.

No comments:

Post a Comment