You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Mario Rabe <ma...@googlemail.com> on 2009/08/08 20:26:45 UTC
redirect from ajax request
Hi,
how can I do a redirect from a XHR-request? Here an example to make clear
what I want to do:
I've got a couter-component which shows a count and 2 links (up and down).
If a link is clicked, the count is changed inPlace. A "ValueChanged"-Event
is triggered on every change.
[Pseudo-Code. won't compile, just to show the intention]
class Counter{
@Property private int count;
EventCallback callback = new EventCallback(){
boolean handleEvent(...){
...
}
};
onUp(){
count++;
resources.triggerEvent("valueChanged", new Object[]{count},
callback);
}
onDown(){
count--;
resources.triggerEvent("valueChanged", new Object[]{count},
callback);
}
}
That counter can be used on pages and the page can do something if user
changes value.
class CounterPage{
Object onValueChangeFromMyCounter(int count){
if(count > Oracle.MYSTICAL_VALUE){
System.secretOperation();
}
}
}
Now my question is how to do a page-redirect in the EventListener on the
page? I can return an URL from it but to do with it in the callback? Which
service is responsible? And does tapestry.js handle redirects in
XHR-requests as W3C-XHR doesn't?
Greetings
Mario
Re: redirect from ajax request
Posted by Mario Rabe <ma...@googlemail.com>.
Actually its was pretty easy (like most things I try to do with tapestry) as
soon as I understand the concept. Simply getting the
current ComponentEventResultProcessor from the Environment, wrap it in case
of exceptions and let it process the return-value. It is really as easy as
this, EXCEPT you use the processor AND return a value after that.
This way I managed it to have 2 json-objects in the response which made
tapestry.js really mad =D
{"redirectURL":"/TapTestReal/about"}{"content":"And another counter: 11"}
public class Counter {
@InjectComponent
private Zone countzone;
@Inject
private ComponentResources componentResources;
@Environmental
private ComponentEventResultProcessor componentEventResultProcessor;
@Property
@Persist
private int count;
private Object fireValueChanged() throws IOException {
ComponentResultProcessorWrapper callback = new
ComponentResultProcessorWrapper(componentEventResultProcessor);
componentResources.triggerEvent("valueChanged", new Object[]{new
Integer(count)}, callback);
if (!callback.isAborted()) {
return countzone.getBody();
}
return null;
}
public Object onUp() throws IOException {
count++;
return fireValueChanged();
}
public Object onDown() throws IOException {
count--;
return fireValueChanged();
}
public Object onReset() throws IOException {
count = 0;
return fireValueChanged();
}
public String getZoneId(){
return countzone.getClientId(); //with this I can use this
component multiple times on a single page even with ajax enabled
}
}
I cutted some pieces of code which implement features not used in this
context, hopefully it still compiles. That component can be used on a page
like:
Object onValueChangedFromEggCounter(int value){
if(value > 10)
return ToMuchEggs.class;
return null;
}
and everything works like expected (if counter reaches 11 you are redirected
to another page).
- Mario
2009/8/10 Howard Lewis Ship <hl...@gmail.com>
> To be specific, when during an Ajax request the component even returns
> a page class or page instance, Tapestry builds and returns a Link as
> part of the JSON response, the the client-side loads that Link's URL
> into the browser address, performing a redirect.
>
> There are two implementations of ComponentEventResultProcessor, one
> for @Traditional and one for @Ajax ... just bear that in mind if you
> want to read the existing code, or extend the current behavior. They
> are both based on a mapped configuration from return type to a
> handler.
>
> On Sun, Aug 9, 2009 at 5:34 PM, Thiago H. de Paula
> Figueiredo<th...@gmail.com> wrote:
> > Em Sat, 08 Aug 2009 15:26:45 -0300, Mario Rabe <
> mario.rabe@googlemail.com>
> > escreveu:
> >
> >> Hi,
> >
> > Hi!
> >
> >> how can I do a redirect from a XHR-request? Here an example to make
> clear
> >> what I want to do:
> >
> > If it was a Tapestry-provided event, you could just return a page
> instance,
> > a page class, an URL or a page name in an event handler method.
> > Looking at the Form component sources, it uses this callback instance in
> its
> > VALIDATE_FORM event:
> >
> > ComponentResultProcessorWrapper callback = new
> > ComponentResultProcessorWrapper(componentEventResultProcessor);
> >
> > componentEventResultProcessor comes from an environmental service:
> >
> > @Environmental
> > private ComponentEventResultProcessor componentEventResultProcessor;
> >
> > Try using the above callback and please post the results. :)
> >
> > --
> > Thiago H. de Paula Figueiredo
> > Independent Java consultant, developer, and instructor
> > http://www.arsmachina.com.br/thiago
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>
>
>
> --
> Howard M. Lewis Ship
>
> Creator of Apache Tapestry
> Director of Open Source Technology at Formos
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
Re: redirect from ajax request
Posted by Howard Lewis Ship <hl...@gmail.com>.
To be specific, when during an Ajax request the component even returns
a page class or page instance, Tapestry builds and returns a Link as
part of the JSON response, the the client-side loads that Link's URL
into the browser address, performing a redirect.
There are two implementations of ComponentEventResultProcessor, one
for @Traditional and one for @Ajax ... just bear that in mind if you
want to read the existing code, or extend the current behavior. They
are both based on a mapped configuration from return type to a
handler.
On Sun, Aug 9, 2009 at 5:34 PM, Thiago H. de Paula
Figueiredo<th...@gmail.com> wrote:
> Em Sat, 08 Aug 2009 15:26:45 -0300, Mario Rabe <ma...@googlemail.com>
> escreveu:
>
>> Hi,
>
> Hi!
>
>> how can I do a redirect from a XHR-request? Here an example to make clear
>> what I want to do:
>
> If it was a Tapestry-provided event, you could just return a page instance,
> a page class, an URL or a page name in an event handler method.
> Looking at the Form component sources, it uses this callback instance in its
> VALIDATE_FORM event:
>
> ComponentResultProcessorWrapper callback = new
> ComponentResultProcessorWrapper(componentEventResultProcessor);
>
> componentEventResultProcessor comes from an environmental service:
>
> @Environmental
> private ComponentEventResultProcessor componentEventResultProcessor;
>
> Try using the above callback and please post the results. :)
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java consultant, developer, and instructor
> http://www.arsmachina.com.br/thiago
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
--
Howard M. Lewis Ship
Creator of Apache Tapestry
Director of Open Source Technology at Formos
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: redirect from ajax request
Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Sat, 08 Aug 2009 15:26:45 -0300, Mario Rabe <ma...@googlemail.com>
escreveu:
> Hi,
Hi!
> how can I do a redirect from a XHR-request? Here an example to make clear
> what I want to do:
If it was a Tapestry-provided event, you could just return a page
instance, a page class, an URL or a page name in an event handler method.
Looking at the Form component sources, it uses this callback instance in
its VALIDATE_FORM event:
ComponentResultProcessorWrapper callback = new
ComponentResultProcessorWrapper(componentEventResultProcessor);
componentEventResultProcessor comes from an environmental service:
@Environmental
private ComponentEventResultProcessor componentEventResultProcessor;
Try using the above callback and please post the results. :)
--
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
http://www.arsmachina.com.br/thiago
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org