You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Artur W." <a_...@gazeta.pl> on 2007/12/17 22:33:26 UTC

How to refresh component many times in one ajax request

Hi!

I try to do something like this

		final Label counterLabel = new Label("counter", new PropertyModel(this,
"counter"));
		counterLabel.setOutputMarkupId(true);
		add(counterLabel);

		Form form = new Form("form");
		add(form);

		form.add(new AjaxButton("confirmButton") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form form) {
				for (int i=0; i &lt;100; i++ ) {
					synchronized (this) {
						try {
							wait(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						counter += i;
						target.addComponent(counterLabel);
					}
				}
			}
		});


The component doesn't refresh. Only for the last time. How to make it work?


Thanks,
Artur

-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14374053.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: How to refresh component many times in one ajax request

Posted by Johan Compagner <jc...@gmail.com>.
Nope
thats already done:

*

final* Iterator iter = behaviors.iterator();

*while* (iter.hasNext())

{

IBehavior behavior = (IBehavior)iter.next();

*if* (behavior *instanceof* IHeaderContributor &&
isBehaviorAccepted(behavior))

{

((IHeaderContributor)behavior).renderHead(container.getHeaderResponse());

}

}






On Dec 18, 2007 2:24 PM, Gerolf Seitz <ge...@gmail.com> wrote:

> On Dec 18, 2007 2:15 PM, Matej Knopp <ma...@gmail.com> wrote:
>
> > I'm not entirely sure isEnabled() works properly on timer behaviors.
> > If it doesn't please add a feature request, the current implementation
> > is way too simplistic and needs to be enhanced.
> >
>
> seems like we need to check for isEnabled(Component) in #renderHead
>
>  Gerolf
>
>
> >
> > -Matej
> >
> > On Dec 18, 2007 12:43 PM, Artur W. <a_...@gazeta.pl> wrote:
> > >
> > >
> > > Alex Objelean wrote:
> > > >
> > > > AjaxSelfUpdatingTimerBehavior implements IBehavior interface, which
> > has
> > > > the following method:
> > > > boolean isEnabled(Component component);
> > > >
> > > > Override it and provide your business logic there.
> > > >
> > >
> > > I must do something wrong becouse it doesn't start refreshing.
> > >
> > > My refreshing label:
> > >
> > >                 counterLabel.add(new AjaxSelfUpdatingTimerBehavior(
> > Duration.seconds(2)) {
> > >                         @Override
> > >                         protected void
> > onPostProcessTarget(AjaxRequestTarget target) {
> > >                                 if (counter == 0 && confirmed) {
> > >                                         window.close(target);
> > >                                 }
> > >                         }
> > >                         @Override
> > >                         public boolean isEnabled(Component component)
> {
> > >                                 return confirmed;
> > >                         }
> > >                 });
> > >                 counterLabel.setOutputMarkupId(true);
> > >                 add(counterLabel);
> > >
> > > My confirm button:
> > >
> > >                 form.add(new IndicatingAjaxButton("confirmButton",
> form)
> > {
> > >                         @Override
> > >                         protected void onSubmit(final
> AjaxRequestTarget
> > target, Form form) {
> > >
> > >                                 confirmed = true;
> > >
> > >
> > >                                 setEnabled(false);
> > >                                 setOutputMarkupId(true);
> > >
> > >                                 new Thread() {
> > >
> > >                                         public void run() {
> > >                                                 //do something here
> > >                                                 counter--;
> > >                                         }
> > >
> > >                                 }.start();
> > >                                 target.addComponent(this);
> > >                                 target.addComponent(counterLabel);
> > >                         }
> > >
> > > Artur
> > >
> > > --
> > > View this message in context:
> >
> http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14395329.html
> > >
> > > Sent from the Wicket - User mailing list archive at Nabble.com<http://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: How to refresh component many times in one ajax request

Posted by Gerolf Seitz <ge...@gmail.com>.
On Dec 18, 2007 2:15 PM, Matej Knopp <ma...@gmail.com> wrote:

> I'm not entirely sure isEnabled() works properly on timer behaviors.
> If it doesn't please add a feature request, the current implementation
> is way too simplistic and needs to be enhanced.
>

seems like we need to check for isEnabled(Component) in #renderHead

  Gerolf


>
> -Matej
>
> On Dec 18, 2007 12:43 PM, Artur W. <a_...@gazeta.pl> wrote:
> >
> >
> > Alex Objelean wrote:
> > >
> > > AjaxSelfUpdatingTimerBehavior implements IBehavior interface, which
> has
> > > the following method:
> > > boolean isEnabled(Component component);
> > >
> > > Override it and provide your business logic there.
> > >
> >
> > I must do something wrong becouse it doesn't start refreshing.
> >
> > My refreshing label:
> >
> >                 counterLabel.add(new AjaxSelfUpdatingTimerBehavior(
> Duration.seconds(2)) {
> >                         @Override
> >                         protected void
> onPostProcessTarget(AjaxRequestTarget target) {
> >                                 if (counter == 0 && confirmed) {
> >                                         window.close(target);
> >                                 }
> >                         }
> >                         @Override
> >                         public boolean isEnabled(Component component) {
> >                                 return confirmed;
> >                         }
> >                 });
> >                 counterLabel.setOutputMarkupId(true);
> >                 add(counterLabel);
> >
> > My confirm button:
> >
> >                 form.add(new IndicatingAjaxButton("confirmButton", form)
> {
> >                         @Override
> >                         protected void onSubmit(final AjaxRequestTarget
> target, Form form) {
> >
> >                                 confirmed = true;
> >
> >
> >                                 setEnabled(false);
> >                                 setOutputMarkupId(true);
> >
> >                                 new Thread() {
> >
> >                                         public void run() {
> >                                                 //do something here
> >                                                 counter--;
> >                                         }
> >
> >                                 }.start();
> >                                 target.addComponent(this);
> >                                 target.addComponent(counterLabel);
> >                         }
> >
> > Artur
> >
> > --
> > View this message in context:
> http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14395329.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: How to refresh component many times in one ajax request

Posted by "Artur W." <a_...@gazeta.pl>.

Johan Compagner wrote:
> 
> it should work fine.
> 

I updated from rc1 to rc2 and it works!!!


Thanks!
Artur

-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14399364.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: How to refresh component many times in one ajax request

Posted by Johan Compagner <jc...@gmail.com>.
it should work fine.

A self updating timer only works once.. as far as i know
then it does the call. And the SelfUpdating add its component back to the
ajax request target
so that normally the timer is inserted again and started when the ajax
request finishes.

and the isEnabled() is checked when the component renders its behaviors.

johan



On Dec 18, 2007 2:15 PM, Matej Knopp <ma...@gmail.com> wrote:

> I'm not entirely sure isEnabled() works properly on timer behaviors.
> If it doesn't please add a feature request, the current implementation
> is way too simplistic and needs to be enhanced.
>
> -Matej
>
> On Dec 18, 2007 12:43 PM, Artur W. <a_...@gazeta.pl> wrote:
> >
> >
> > Alex Objelean wrote:
> > >
> > > AjaxSelfUpdatingTimerBehavior implements IBehavior interface, which
> has
> > > the following method:
> > > boolean isEnabled(Component component);
> > >
> > > Override it and provide your business logic there.
> > >
> >
> > I must do something wrong becouse it doesn't start refreshing.
> >
> > My refreshing label:
> >
> >                 counterLabel.add(new AjaxSelfUpdatingTimerBehavior(
> Duration.seconds(2)) {
> >                         @Override
> >                         protected void
> onPostProcessTarget(AjaxRequestTarget target) {
> >                                 if (counter == 0 && confirmed) {
> >                                         window.close(target);
> >                                 }
> >                         }
> >                         @Override
> >                         public boolean isEnabled(Component component) {
> >                                 return confirmed;
> >                         }
> >                 });
> >                 counterLabel.setOutputMarkupId(true);
> >                 add(counterLabel);
> >
> > My confirm button:
> >
> >                 form.add(new IndicatingAjaxButton("confirmButton", form)
> {
> >                         @Override
> >                         protected void onSubmit(final AjaxRequestTarget
> target, Form form) {
> >
> >                                 confirmed = true;
> >
> >
> >                                 setEnabled(false);
> >                                 setOutputMarkupId(true);
> >
> >                                 new Thread() {
> >
> >                                         public void run() {
> >                                                 //do something here
> >                                                 counter--;
> >                                         }
> >
> >                                 }.start();
> >                                 target.addComponent(this);
> >                                 target.addComponent(counterLabel);
> >                         }
> >
> > Artur
> >
> > --
> > View this message in context:
> http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14395329.html
> >
> > Sent from the Wicket - User mailing list archive at Nabble.com<http://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: How to refresh component many times in one ajax request

Posted by Matej Knopp <ma...@gmail.com>.
I'm not entirely sure isEnabled() works properly on timer behaviors.
If it doesn't please add a feature request, the current implementation
is way too simplistic and needs to be enhanced.

-Matej

On Dec 18, 2007 12:43 PM, Artur W. <a_...@gazeta.pl> wrote:
>
>
> Alex Objelean wrote:
> >
> > AjaxSelfUpdatingTimerBehavior implements IBehavior interface, which has
> > the following method:
> > boolean isEnabled(Component component);
> >
> > Override it and provide your business logic there.
> >
>
> I must do something wrong becouse it doesn't start refreshing.
>
> My refreshing label:
>
>                 counterLabel.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(2)) {
>                         @Override
>                         protected void onPostProcessTarget(AjaxRequestTarget target) {
>                                 if (counter == 0 && confirmed) {
>                                         window.close(target);
>                                 }
>                         }
>                         @Override
>                         public boolean isEnabled(Component component) {
>                                 return confirmed;
>                         }
>                 });
>                 counterLabel.setOutputMarkupId(true);
>                 add(counterLabel);
>
> My confirm button:
>
>                 form.add(new IndicatingAjaxButton("confirmButton", form) {
>                         @Override
>                         protected void onSubmit(final AjaxRequestTarget target, Form form) {
>
>                                 confirmed = true;
>
>
>                                 setEnabled(false);
>                                 setOutputMarkupId(true);
>
>                                 new Thread() {
>
>                                         public void run() {
>                                                 //do something here
>                                                 counter--;
>                                         }
>
>                                 }.start();
>                                 target.addComponent(this);
>                                 target.addComponent(counterLabel);
>                         }
>
> Artur
>
> --
> View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14395329.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: How to refresh component many times in one ajax request

Posted by "Artur W." <a_...@gazeta.pl>.

Alex Objelean wrote:
> 
> AjaxSelfUpdatingTimerBehavior implements IBehavior interface, which has
> the following method:
> boolean isEnabled(Component component); 
> 
> Override it and provide your business logic there.
> 
 
I must do something wrong becouse it doesn't start refreshing.

My refreshing label:

		counterLabel.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(2)) {
			@Override
			protected void onPostProcessTarget(AjaxRequestTarget target) {
				if (counter == 0 && confirmed) {
					window.close(target);
				}
			}
			@Override
			public boolean isEnabled(Component component) {
				return confirmed;
			}
		});
		counterLabel.setOutputMarkupId(true);
		add(counterLabel);

My confirm button:

		form.add(new IndicatingAjaxButton("confirmButton", form) {
			@Override
			protected void onSubmit(final AjaxRequestTarget target, Form form) {

				confirmed = true;

		
				setEnabled(false);
				setOutputMarkupId(true);

				new Thread() {

					public void run() {
						//do something here
						counter--;
					}
					
				}.start();
				target.addComponent(this);
				target.addComponent(counterLabel);
			}

Artur

-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14395329.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: How to refresh component many times in one ajax request

Posted by Alex Objelean <al...@isdc.ro>.
AjaxSelfUpdatingTimerBehavior implements IBehavior interface, which has the
following method:
boolean isEnabled(Component component); 

Override it and provide your business logic there.

Alex.


Artur W. wrote:
> 
> 
> Alex Objelean wrote:
>> 
>> I think that the better approach would be to enable/disable the behavior,
>> instead of adding it.
>> 
> 
> Is there a convenience method to do this?
> 
> There is no enable/disable method in the AjaxSelfUpdatingTimerBehavior.
> 
> 
> Thanks,
> Artur
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14393058.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: How to refresh component many times in one ajax request

Posted by "Artur W." <a_...@gazeta.pl>.

Alex Objelean wrote:
> 
> I think that the better approach would be to enable/disable the behavior,
> instead of adding it.
> 

Is there a convenience method to do this?

There is no enable/disable method in the AjaxSelfUpdatingTimerBehavior.


Thanks,
Artur

-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14391636.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: How to refresh component many times in one ajax request

Posted by Alex Objelean <al...@isdc.ro>.
I think that the better approach would be to enable/disable the behavior,
instead of adding it.

Alex.


Artur W. wrote:
> 
> Btw, is is possible to add Behavior to the component in the
> AjaxRequestTarget?
> For exampel I want to add AjaxSelfUpdatingTimerBehavior but when the user
> click on the button.
> 
> 
> Thanks,
> Artur
> 

-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14387677.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: How to refresh component many times in one ajax request

Posted by "Artur W." <a_...@gazeta.pl>.

Gerolf Seitz wrote:
> 
> On Dec 17, 2007 10:33 PM, Artur W. <a_...@gazeta.pl> wrote:
> 
>>
>> The component doesn't refresh. Only for the last time. How to make it
>> work?
>>
> 
> that's because the response is sent *AFTER* loop is over, not when
> target.addComponent(..) is called.
> what you need is a AjaxSelfUpdatingTimerBehavior, override
> onPostProcessTarget and do counter = counter +1; in there.
> 

Thanks for reply Guys! My business case is much more complicated. This was
just simple example.

I did like you said. It was interested that I had to start new thread to
process the loop.
Without it the label didn't refresh the counter value.

Here is my code, maybe it would be helpful for someone else:

		this.counter = 100

		final Label counterLabel = new Label("counter", new PropertyModel(this,
"counter"));
		counterLabel.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(3)) {
			@Override
			protected void onPostProcessTarget(AjaxRequestTarget target) {
				if (counter == 0) {
					
					//close the window after countdown :)
					window.close(target);
				}
			}
		});
		counterLabel.setOutputMarkupId(true);
		add(counterLabel);

		Form form = new Form("form");
		add(form);

		form.add(new IndicatingAjaxButton("confirmButton", form) {
			@Override
			protected void onSubmit(final AjaxRequestTarget target, Form form) {

				setEnabled(false);
				setOutputMarkupId(true);
				target.addComponent(this);

				new Thread() {

					public void run() {
						for (int i = 0; i &lt; 100; i++ ) {
							synchronized (this) {
								
								//do business here ;)
								try { wait(300); } catch (InterruptedException e) {
e.printStackTrace(); }

								counter--;
							}
						}
					}
				}.start();

			}
		});


Btw, is is possible to add Behavior to the component in the
AjaxRequestTarget?
For exampel I want to add AjaxSelfUpdatingTimerBehavior but when the user
click on the button.


Thanks,
Artur
-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14387672.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: How to refresh component many times in one ajax request

Posted by Gerolf Seitz <ge...@gmail.com>.
On Dec 17, 2007 10:33 PM, Artur W. <a_...@gazeta.pl> wrote:

>
> The component doesn't refresh. Only for the last time. How to make it
> work?
>

that's because the response is sent *AFTER* loop is over, not when
target.addComponent(..) is called.
what you need is a AjaxSelfUpdatingTimerBehavior, override
onPostProcessTarget and do counter = counter +1; in there.

hth,
  Gerolf


>
>
> Thanks,
> Artur
>
> --
> View this message in context:
> http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14374053.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: How to refresh component many times in one ajax request

Posted by Michael Sparer <mi...@gmx.at>.
What you're doing right now is to send a request to the server, iterate a
hundred times, replace a component a hundred times, even before the response
from the server reaches the client again.

That what you want to achieve would involve 100 ajax requests and responses,
it IS feasable, but better try a javascript-solution, because 100 roundtrips
is just too much if it's just for changing the label to a fixed value.
But if it's more than that, AjaxSelfUpdatingBehavior might be your friend

Artur W. wrote:
> 
> Hi!
> 
> I try to do something like this
> 
> 		final Label counterLabel = new Label("counter", new PropertyModel(this,
> "counter"));
> 		counterLabel.setOutputMarkupId(true);
> 		add(counterLabel);
> 
> 		Form form = new Form("form");
> 		add(form);
> 
> 		form.add(new AjaxButton("confirmButton") {
> 			@Override
> 			protected void onSubmit(AjaxRequestTarget target, Form form) {
> 				for (int i=0; i &lt;100; i++ ) {
> 					synchronized (this) {
> 						try {
> 							wait(1000);
> 						} catch (InterruptedException e) {
> 							e.printStackTrace();
> 						}
> 						counter += i;
> 						target.addComponent(counterLabel);
> 					}
> 				}
> 			}
> 		});
> 
> 
> The component doesn't refresh. Only for the last time. How to make it
> work?
> 
> 
> Thanks,
> Artur
> 
> 


-----
Michael Sparer
http://talk-on-tech.blogspot.com
-- 
View this message in context: http://www.nabble.com/How-to-refresh-component-many-times-in-one-ajax-request-tp14374053p14384523.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