You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by CrocodileShoes <ma...@logica.com> on 2009/07/23 17:09:53 UTC

Re: Is it possible to restart an AjaxSelfUpdatingBehaviour after it has been stopped?

Well I was adding this behaviour in the constructor of the Panel, i.e. 

this.add(new AjaxSelfUpdatingBehaviour(...));

which I assumed resulted in the same effect.

I have just changed it to the following (see below) and now get a null
pointer when the behaviour tries to update the panel after I switch to
another one, i.e. when the panel is no longer visible.

Note, I am storing the panels in a List (tabsList) to prevent them being
recreated every time a user switches tabs.  I did this to preserve the
state.  Perhaps this is causing some problems.

So to recap, this now throws a null pointer (at
org.apache.wicket.request.target.component.listener.BehaviorRequestTarget.processEvents(BehaviorRequestTarget.java:96)
presumably because the panel isn't in the markup to update.

tabs.add(new AbstractTab(new Model("Administration")) {
  public Panel getPanel(String panelId) {
    if (tabsList.containsKey("Administration")) {
      return tabsList.get("Administration");
    }
    else {
      AdminPanel adminPanel = new AdminPanel(panelId);
      adminPanel.setOutputMarkupPlaceholderTag(true);
      adminPanel.add(new
AjaxSelfUpdatingTimerBehavior(Duration.seconds(5)));
      tabsList.put("Administration", adminPanel);
      return adminPanel;
    }
  }
});



igor.vaynberg wrote:
> 
> cant you add the behavior directly to the panel?
> 
> -igor
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24627960.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Is it possible to restart an AjaxSelfUpdatingBehaviour after it has been stopped?

Posted by Igor Vaynberg <ig...@gmail.com>.
hm, not really sure. maybe you can try rolling your own behavior and
customize it to your exact needs.

-igor

On Thu, Jul 23, 2009 at 8:09 AM, CrocodileShoes<ma...@logica.com> wrote:
>
> Well I was adding this behaviour in the constructor of the Panel, i.e.
>
> this.add(new AjaxSelfUpdatingBehaviour(...));
>
> which I assumed resulted in the same effect.
>
> I have just changed it to the following (see below) and now get a null
> pointer when the behaviour tries to update the panel after I switch to
> another one, i.e. when the panel is no longer visible.
>
> Note, I am storing the panels in a List (tabsList) to prevent them being
> recreated every time a user switches tabs.  I did this to preserve the
> state.  Perhaps this is causing some problems.
>
> So to recap, this now throws a null pointer (at
> org.apache.wicket.request.target.component.listener.BehaviorRequestTarget.processEvents(BehaviorRequestTarget.java:96)
> presumably because the panel isn't in the markup to update.
>
> tabs.add(new AbstractTab(new Model("Administration")) {
>  public Panel getPanel(String panelId) {
>    if (tabsList.containsKey("Administration")) {
>      return tabsList.get("Administration");
>    }
>    else {
>      AdminPanel adminPanel = new AdminPanel(panelId);
>      adminPanel.setOutputMarkupPlaceholderTag(true);
>      adminPanel.add(new
> AjaxSelfUpdatingTimerBehavior(Duration.seconds(5)));
>      tabsList.put("Administration", adminPanel);
>      return adminPanel;
>    }
>  }
> });
>
>
>
> igor.vaynberg wrote:
>>
>> cant you add the behavior directly to the panel?
>>
>> -igor
>>
>
> --
> View this message in context: http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24627960.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Is it possible to restart an AjaxSelfUpdatingBehaviour after it has been stopped?

Posted by CrocodileShoes <ma...@logica.com>.
Yeah, the headRendered flag is still set to true so the JsTimeout call is
never rendered.  I'm not sure why it's was removed in the first place,
perhaps it's something to do with the precondition?


CrocodileShoes wrote:
> 
> Hi,
> 
> No it's definitely not that.  I tried your class to be sure and it still
> doesn't work.
> 
> I've done some more debugging with Firefox's Firebug and discovered that
> when I switch back to the tab with the updating behaviour it does not
> contain the evaluate part.
> 
> I'll print this, it's easier:
> 
> This is in the response after I switch to the self updating tab for the
> first time:
> </component><evaluate><![CDATA[setTimeout("var
> wcall=wicketAjaxGet('?wicket:interface=:5:tabs:panel:logOutput::IBehaviorListener:0:-1',null,null,
> function() {var c = Wicket.$('logOutput611'); return typeof(c) !=
> 'undefined' && c != null}.bind(this));",
> 5000);]]></evaluate></ajax-response>
> 
> Now, if I navigate away, and stay away (on another tab), until the timer
> fires it fails the precondition check.  When I go back to the self
> updating tab the response contains:
> </component></ajax-response>
> 
> There is no evaluate section.
> 
> The  renderHead() seems like it is responsible for this, as follows:
> 
> if (!stopped && (!headRendered || !request.isAjax())) {
>     headRendered = true;
>     response.renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
> }
> 
> Now, stopped is definitely false so one of the other if-conditions must be
> responsible.  I'll do some more work on this now.
> 
> Thanks for the help so far.
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24646324.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Is it possible to restart an AjaxSelfUpdatingBehaviour after it has been stopped?

Posted by CrocodileShoes <ma...@logica.com>.
Hi,

No it's definitely not that.  I tried your class to be sure and it still
doesn't work.

I've done some more debugging with Firefox's Firebug and discovered that
when I switch back to the tab with the updating behaviour it does not
contain the evaluate part.

I'll print this, it's easier:

This is in the response after I switch to the self updating tab for the
first time:
</component><evaluate><![CDATA[setTimeout("var
wcall=wicketAjaxGet('?wicket:interface=:5:tabs:panel:logOutput::IBehaviorListener:0:-1',null,null,
function() {var c = Wicket.$('logOutput611'); return typeof(c) !=
'undefined' && c != null}.bind(this));",
5000);]]></evaluate></ajax-response>

Now, if I navigate away, and stay away (on another tab), until the timer
fires it fails the precondition check.  When I go back to the self updating
tab the response contains:
</component></ajax-response>

There is no evaluate section.

The  renderHead() seems like it is responsible for this, as follows:

if (!stopped && (!headRendered || !request.isAjax())) {
    headRendered = true;
    response.renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
}

Now, stopped is definitely false so one of the other if-conditions must be
responsible.  I'll do some more work on this now.

Thanks for the help so far.


dtoffe wrote:
> 
> Hi,
> 
>     First, let's see if we are talking about the same code, here is the
> actual code I use:
> 
> 
> <SNIP>
> 
> 
>     If I recall correctly, I had to make a small change in other method, I
> believe it's in renderHead() or in respond(), please check that, I'm in a
> hurry right now.
> 
> hth,
> 
> Daniel
> 
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24645797.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Is it possible to restart an AjaxSelfUpdatingBehaviour after it has been stopped?

Posted by dtoffe <dt...@yahoo.com.ar>.
Hi,

    First, let's see if we are talking about the same code, here is the
actual code I use:


import org.apache.wicket.Page;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.util.time.Duration;

/**
 * @author toffedan
 */
public class RestartebleAjaxSelfUpdatingTimerBehavior extends
AbstractDefaultAjaxBehavior {

    private static final long serialVersionUID = 1L;

	/** The update interval */
	private Duration updateInterval;

	private boolean stopped = false;

	private boolean headRendered = false;

	/**
	 * Construct.
	 *
	 * @param updateInterval
	 *            Duration between AJAX callbacks
	 */
	public RestartebleAjaxSelfUpdatingTimerBehavior(final Duration
updateInterval)
	{
		if (updateInterval == null || updateInterval.getMilliseconds() <= 0)
		{
			throw new IllegalArgumentException("Invalid update interval");
		}
		this.updateInterval = updateInterval;
	}

	/**
	 * Stops the timer
	 */
	public final void stop()
	{
		stopped = true;
	}

	/**
	 * Starts the timer
	 */
	public final void start(final AjaxRequestTarget target)
	{
		stopped = false;
    
target.getHeaderResponse().renderOnLoadJavascript(getJsTimeoutCall(updateInterval));

	}

	/**
	 * Sets the update interval duration. This method should only be called
within the
	 * {@link #onTimer(AjaxRequestTarget)} method.
	 *
	 * @param updateInterval
	 */
	protected final void setUpdateInterval(Duration updateInterval)
	{
		if (updateInterval == null || updateInterval.getMilliseconds() <= 0)
		{
			throw new IllegalArgumentException("Invalid update interval");
		}
		this.updateInterval = updateInterval;
	}

	/**
	 * Returns the update interval
	 *
	 * @return The update interval
	 */
	public final Duration getUpdateInterval()
	{
		return updateInterval;
	}

	/**
	 * @see
org.apache.wicket.behavior.AbstractAjaxBehavior#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
	 */
    @Override
	public void renderHead(IHeaderResponse response)
	{
		super.renderHead(response);

		WebRequest request = (WebRequest)RequestCycle.get().getRequest();

		if (!stopped && (!headRendered || !request.isAjax()))
		{
			headRendered = true;
			response.renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
		}
	}

	/**
	 * @param updateInterval
	 *            Duration between AJAX callbacks
	 * @return JS script
	 */
	protected final String getJsTimeoutCall(final Duration updateInterval)
	{
		// this might look strange, but it is necessary for IE not to leak :(
		return "setTimeout(\"" + getCallbackScript() + "\", " +
updateInterval.getMilliseconds() +
			");";
	}

    @Override
	protected CharSequence getCallbackScript()
	{
		return generateCallbackScript("wicketAjaxGet('" +
getCallbackUrl(onlyTargetActivePage()) +
			"'");
	}

	/**
	 * @see
org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getPreconditionScript()
	 */
    @Override
	protected CharSequence getPreconditionScript()
	{
		String precondition = null;
		if (!(getComponent() instanceof Page))
		{
			String componentId = getComponent().getMarkupId();
			precondition = "var c = Wicket.$('" + componentId +
				"'); return typeof(c) != 'undefined' && c != null";
		}
		return precondition;
	}

	protected boolean onlyTargetActivePage()
	{
		return true;
	}

	/**
	 *
	 * @see
org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget)
	 */
	protected final void respond(final AjaxRequestTarget target)
	{
		if (!stopped) {

    		onTimer(target);

		
target.getHeaderResponse().renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
		}

	}

	/**
	 * @see
org.apache.wicket.ajax.AbstractAjaxTimerBehavior#onTimer(org.apache.wicket.ajax.AjaxRequestTarget)
	 */
	protected final void onTimer(final AjaxRequestTarget target)
	{
		target.addComponent(getComponent());
		onPostProcessTarget(target);
	}

	/**
	 * Give the subclass a chance to add something to the target, like a
javascript effect call.
	 * Called after the hosting component has been added to the target.
	 *
	 * @param target
	 *            The AJAX target
	 */
	protected void onPostProcessTarget(final AjaxRequestTarget target)
	{
	}

}


    If I recall correctly, I had to make a small change in other method, I
believe it's in renderHead() or in respond(), please check that, I'm in a
hurry right now.

hth,

Daniel



CrocodileShoes wrote:
> 
> Thanks Daniel,
> 
> I've just tried this and it's not worked.  Obviously it's a slightly
> different use case, that is, changing tabs and popping up a modal window
> but I'd hoped it would work.
> 
> It turns out that the stopped flag is not actually set to true.  That's
> why your method doesn't work.  When I change tabs the timer is still
> running and when it fires I get the following message:
> 
> INFO: Ajax GET stopped because of precondition check,
> url:?wicket:interface=:0:tabs:panel:logOutput::IActivePageBehaviorListener:0:-1&wicket:ignoreIfNotActive=true
> 
> 
> 
> dtoffe wrote:
>> 
>> Hi,
>> 
>>     Also take a look at this thread:
>> 
>>  
>> http://www.nabble.com/AjaxSelfUpdatingTimerBehavior-and-ModalWindow-to22202102.html
>> 
>>     I don't mean it's a correct or better solution, but so far it works
>> for me.
>> 
>> Hth,
>> 
>> Daniel
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24644786.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Is it possible to restart an AjaxSelfUpdatingBehaviour after it has been stopped?

Posted by CrocodileShoes <ma...@logica.com>.
Thanks Daniel,

I've just tried this and it's not worked.  Obviously it's a slightly
different use case, that is, changing tabs and popping up a modal window but
I'd hoped it would work.

It turns out that the stopped flag is not actually set to true.  That's why
your method doesn't work.  When I change tabs the timer is still running and
when it fires I get the following message:

INFO: Ajax GET stopped because of precondition check,
url:?wicket:interface=:0:tabs:panel:logOutput::IActivePageBehaviorListener:0:-1&wicket:ignoreIfNotActive=true



dtoffe wrote:
> 
> Hi,
> 
>     Also take a look at this thread:
> 
>  
> http://www.nabble.com/AjaxSelfUpdatingTimerBehavior-and-ModalWindow-to22202102.html
> 
>     I don't mean it's a correct or better solution, but so far it works
> for me.
> 
> Hth,
> 
> Daniel
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24643947.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Is it possible to restart an AjaxSelfUpdatingBehaviour after it has been stopped?

Posted by dtoffe <dt...@yahoo.com.ar>.
Hi,

    Also take a look at this thread:

 
http://www.nabble.com/AjaxSelfUpdatingTimerBehavior-and-ModalWindow-to22202102.html

    I don't mean it's a correct or better solution, but so far it works for
me.

Hth,

Daniel



CrocodileShoes wrote:
> 
> Well I was adding this behaviour in the constructor of the Panel, i.e. 
> 
> this.add(new AjaxSelfUpdatingBehaviour(...));
> 
> which I assumed resulted in the same effect.
> 
> I have just changed it to the following (see below) and now get a null
> pointer when the behaviour tries to update the panel after I switch to
> another one, i.e. when the panel is no longer visible.
> 
> Note, I am storing the panels in a List (tabsList) to prevent them being
> recreated every time a user switches tabs.  I did this to preserve the
> state.  Perhaps this is causing some problems.
> 
> So to recap, this now throws a null pointer (at
> org.apache.wicket.request.target.component.listener.BehaviorRequestTarget.processEvents(BehaviorRequestTarget.java:96)
> presumably because the panel isn't in the markup to update.
> 
> tabs.add(new AbstractTab(new Model("Administration")) {
>   public Panel getPanel(String panelId) {
>     if (tabsList.containsKey("Administration")) {
>       return tabsList.get("Administration");
>     }
>     else {
>       AdminPanel adminPanel = new AdminPanel(panelId);
>       adminPanel.setOutputMarkupPlaceholderTag(true);
>       adminPanel.add(new
> AjaxSelfUpdatingTimerBehavior(Duration.seconds(5)));
>       tabsList.put("Administration", adminPanel);
>       return adminPanel;
>     }
>   }
> });
> 
> 
> 
> igor.vaynberg wrote:
>> 
>> cant you add the behavior directly to the panel?
>> 
>> -igor
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24629265.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org