0

Following situation:

My Wicket application works in combination with javascript.

There is a Page which has a Label for example. This label has "ON" as text set:

Label state = new Label("serverState", server.getStatus().getState());

server.getStatus().getState() will return "ON" at this moment.

Also i have some javascript on this page, which will update this "serverState" Label every 10 seconds. It might have been changed to "OFF" (by javascript).

My Question is: is there any way to get notified of this "text of the Label has been changed". I need that because i want to display (setVisible(true)) another Component, but only if the text has changed.

DaUser
  • 357
  • 3
  • 5
  • 19

4 Answers4

2

You can add a custom AjaxEvent-Listener to your label. Your Javascript should then trigger that event after changing the switch from ON to OFF and back:

Label state = new Label("serverState", server.getStatus().getState());
state.setMarkupId("mylabelid");
state.add(new AjaxEventBehavior("updatestateON") {
  protected void onEvent(AjaxRequestTarget target) {
    // Do something; switch is ON now
  }
});

state.add(new AjaxEventBehavior("updatestateOFF") {
  protected void onEvent(AjaxRequestTarget target) {
    // Do something; switch is OFF now
  }
});

in Javascript you call this function after the switch changed value:

function triggerWicket(newstate) {
  if (newstate == 'ON') {
    $('#mylabelid').trigger('updatestateON');
  } else {
    $('#mylabelid').trigger('updatestateOFF');
  }
}
Hans
  • 43
  • 3
1

I would use AbstractDefaultAjaxBehavior and do the updating server side

AbstractDefaultAjaxBehavior updateState = new AbstractDefaultAjaxBehavior()
{
  @Override
  protected void respond(AjaxRequestTarget _target)
  {
    String newState = RequestCycle.get().getRequest().getRequestParameters().getParameterValue("state").toString();
    state.setDefaultModel(newState)
    _target.add(state);
  }
};

Then have the webpage add a javascript function when the page loads and have javascript on the client call updateState(state); with the new value.

@Override
public void renderHead(IHeaderResponse response)
{
    super.renderHead(response);
    response.render(JavaScriptHeaderItem.forScript("function updateState(state) {Wicket.Ajax.get({'u':'"+ updateState.getCallbackUrl() +"&state=' + state})}", "updateState"));
}
papkass
  • 1,261
  • 2
  • 14
  • 20
0

Set the other Component visible by the condition you are mentioning.

  Component otherComponent = new Component ("otherComponent"){
     @Override
     protected void onConfigure() {
        super.onConfigure();
        setVisible(server.getStatus().getState().eqaulsIgnoreCase("OFF"));
     }
  };
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
  • That's not the right behaviour. It works only if i would press "F5" to reload the page. (NO, not even F5 because it holds the same Page instance, must enter the url by hand) But it is updated via javascript. This doesn't force the Page to reload. JS snippet: "xxx.find(".textToUpdate").text(server.status.state);" As you can see it only searches for the container with this id and set the right text. Wicket didn't get notified so far. – DaUser Aug 20 '13 at 14:36
  • How do you update the "serverState" Label? Beacuse like you said it's one page instance and i doubt that your JavaScript will ever change the displayed value. I recommend to to this updateing of the label i and toggling the display of the other component "the Wicket way". – Robert Niestroj Aug 20 '13 at 16:20
  • It's updated with javascript: it's like: $(document).ready(function() { loadServerState(); window.setInterval(loadServerState, 5000); } var loadServerState() = function() { var x = $("#" + server.id); ... x.find(".textToUpdate").text(server.status.state); //look for the label to update } – DaUser Aug 21 '13 at 08:09
  • I Suggest to use like Buurman said an `AjaxSelfUpdatingTimerBehavior` , configure components like i showed you and do it all in Java - "The Wicket Way". – Robert Niestroj Aug 21 '13 at 08:27
  • Yeah, sounds like the right way. I use that js because it was "already there" (migrated project from jsp to wicket). But i think i have to change it soon... "The Wicket Way" sounds smarter ;) – DaUser Aug 21 '13 at 09:08
0

Wicket is a server-side web framework. In order to change the contents or visibility of components it needs to know about the changed conditions server-side.

Assuming server.getStatus().getState() on the server will reflect the changes you mentioned, you could use an AjaxSelfUpdatingTimerBehavior to update (a subset of) the components on the page every X seconds/minutes/etc. (It is based on Duration).

EDIT: And when the component is updated (added to the ajaxrequest) you could use Robert Niestroj's answer to set the second component visible as onConfigure will be called for each component which is added to the ajarequest (either directly or by hierarchy).

Buurman
  • 1,914
  • 17
  • 26