views:

178

answers:

2

I'm having trouble with adding and then removing an AjaxSelfUpdatingTimerBehavior in Apache Wicket. The behaviour gets added okay but then as soon as I remove the behavior I get a "Page Expired" come up in the browser very soon, I guess because the removal wasn't clean. My set up is basically a label which starts changing by timer, and two links: "go" and "stop". I want to be able to click "go" and then "stop" (obviously I know it will never work the other way around!). Here's my complete markup:

<html>    
    <body>            
        <span wicket:id="message">message will be here</span><br/>
        <a wicket:id="go">Go</a><br/>        
        <a wicket:id="stop">Stop</a>        
    </body>
</html>

and here's my code:

// imports all from standard wicket
public class HomePage extends WebPage {

    private static final int INTERVAL = 500;

    public HomePage(final PageParameters parameters) {

        final Component label = new Label("message",
            "Hello").setOutputMarkupId(true);

        add(label);

        final IBehavior updater = new AjaxSelfUpdatingTimerBehavior(Duration
            .milliseconds(INTERVAL)) {
            @Override
            protected void onPostProcessTarget(AjaxRequestTarget target) {                    
                label.setDefaultModelObject(String.valueOf(System.nanoTime()));    
            }
        };

        AjaxLink<String> go = new AjaxLink<String>("go") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                label.add(updater);        
                target.addComponent(label);
            }     
        };

        AjaxLink<String> stop = new AjaxLink<String>("stop") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                label.remove(updater);
                target.addComponent(label);
            }     
        };

        add(go, stop);
    }
}

I'm using Wicket 1.4.3.

Any help much appreciated. Thanks.

A: 

I resolved this by using the stop() method instead of attempting to remove the behavior entirely.

I actually do want to remove it entirely at some point after stopping it (because my solution involves newing a behavior up everytime I hit "go" and I want to continue to stop and start without accruing a million behaviors) so I got round that by maintaining a list of behaviors to be removed on some later round trip.

jjerms
A: 

It would have been nice to be able to call restart() or something on AbstractAjaxTimerBehavior to re-enable the timer behavior.

Matt