Skip to content

Pushing javascript to the client

uklance edited this page Aug 28, 2012 · 15 revisions

Custom javascript can be pushed to the client when an event is received using the AjaxResponseRenderer

TML

<t:block t:id="messageBlock">
   <h2>${message}</h2>
</t:block>

<t:cometd.PushTarget topic="/myTopic" event="messageReceived" update="replace" />

Java

@Inject
private AjaxResponseRenderer ajaxResponseRenderer;

@Inject
private Block messageBlock;

@Property
private String message;

Block onMessageReceived(final String message) {
    ajaxResponseRenderer.addCallback(new JavaScriptCallback() {
        public void run(JavaScriptSupport jss) {
            jss.addScript("alert('%s')", message);
        }
    });

    // returning a block is optional, this event handler could return void
    this.message = message;
    return messageBlock;
}
Clone this wiki locally