You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Taha Hafeez <ta...@gmail.com> on 2011/01/23 06:42:09 UTC
@XHR : isXHR shortcut
Hi
This is a simple example of using ComponentClassTransformWorker and also may
be used for graceful degradation for ajax in case javascript is disabled
All it does is convert a call
@XHR
Object onClick(){
//Processing ...
return zone.getBody();
}
to
Object onClick(){
//Processing
if(!request.isXHR()){
return null;
}else {
return zone.getBody();
}
}
Here is the code
Annotation
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface XHR {
}
Worker
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.ComponentClassTransformWorker;
import org.apache.tapestry5.services.ComponentMethodAdvice;
import org.apache.tapestry5.services.ComponentMethodInvocation;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.TransformMethod;
import org.apache.tapestry5.services.TransformMethodSignature;
import com.google.code.tapestryaddons.XHR;
public class XHRWorker implements ComponentClassTransformWorker {
private Request request;
public XHRWorker(Request request){
this.request = request;
}
public void transform(ClassTransformation transformation,
MutableComponentModel model) {
for(final TransformMethod method:
transformation.matchMethodsWithAnnotation(XHR.class)){
TransformMethodSignature signature = method.getSignature();
if(!"void".equals(signature.getReturnType())){
method.addAdvice(new ComponentMethodAdvice(){
public void advise(ComponentMethodInvocation invocation) {
invocation.proceed();
Object result = invocation.getResult();
if(!request.isXHR()){
result = null;
}
invocation.overrideResult(result);
}
});
}
}
}
}
and obviously the contribution
public void contributeComponentClassTransformWorker(
OrderedConfiguration<ComponentClassTransformWorker> configuration){
configuration.addInstance("XHRWorker", XHRWorker.class);
}
A simple example
Template File
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:t='http://tapestry.apache.org/schema/tapestry_5_1_0.xsd'>
<t:eventLink t:zone='zone' t:id='click'>Ajax Link</t:eventLink>
<br/>
<t:zone t:id='zone'>Clicked ${count} times</t:zone>
</html>
and java file
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.corelib.components.Zone;
import com.google.code.tapestryaddons.XHR;
public class XHRDemo {
@Persist
private int count;
@InjectComponent
private Zone zone;
@XHR
Object onClick(){
count++;
return zone.getBody();
}
public int getCount(){
return count;
}
}
RE: @XHR : isXHR shortcut
Posted by "Poder, Jacob" <ja...@thermofisher.com>.
Hmm, apparently the code was filtered out but is included here:
http://tapestry.1045711.n5.nabble.com/XHR-isXHR-shortcut-tp3353416p5713607.html
I'll try posting it again:
// Code begin
public class ZoneWorker implements ComponentClassTransformWorker2 {
protected Request request;
public ZoneWorker(Request request) {
this.request = request;
}
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
// Find Zone's getBody method
PlasticMethod getBody = null;
if (plasticClass.getClassName().equals(Zone.class.getName())) {
for (PlasticMethod method : plasticClass.getMethods()) {
if (method.getDescription().methodName.toLowerCase().equals("getbody")) {
getBody = method;
break;
}
}
getBody.addAdvice(new MethodAdvice() {
public void advise(MethodInvocation invocation) {
if(request.isXHR()) {
// Proceed to Zone's getBody method
invocation.proceed();
} else {
// Override to return null and trigger full page render
invocation.setReturnValue(null);
}
}
});
}
}
}
// Code end
Best regards,
Jacob
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: @XHR : isXHR shortcut
Posted by jpoder <ja...@thermofisher.com>.
Hi,
I'm looking for a general solution to avoid constructs like this in all my
event handlers:
Object onMyAjaxEventHandler(){
do_some_processing();
if(request.isXHR()){
return zone.getBody();
}
return null;
}
Taha's @XHR annotation is a step in the right direction, but finding myself
adding that to every single method that returns zone.getBody() makes me
think that getBody() could make the distinction internally.
Now, I realize that this is quite intrusive to the Zone component, but it
does keep my event handler code a lot cleaner!
I experimentally created the component transformer shown below, which does
the work for me.
Would you advice against and/or could you suggest a better abstraction for
doing this?
Also, should I call invocation.proceed() even in the non-XHR case to ensure
other method advices be run?
Thanks for your input!
Best regards,
Jacob
--
View this message in context: http://tapestry.1045711.n5.nabble.com/XHR-isXHR-shortcut-tp3353416p5713607.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: @XHR : isXHR shortcut
Posted by Taha Hafeez <ta...@gmail.com>.
Hi
The reason the body is invoked is because there may be some logic which must
be executed even if the call is not ajax. The worker only ensures that if
javascript is disabled the call may still go through.
Regarding the return value, i modified the code to include cases you had
pointed out.
So this is the new worker
package com.google.code.tapestryaddons.internal;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.ComponentClassTransformWorker;
import org.apache.tapestry5.services.ComponentMethodAdvice;
import org.apache.tapestry5.services.ComponentMethodInvocation;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.TransformMethod;
import org.apache.tapestry5.services.TransformMethodSignature;
import com.google.code.tapestryaddons.XHR;
public class XHRWorker implements ComponentClassTransformWorker {
private Request request;
public XHRWorker(Request request){
this.request = request;
}
public void transform(ClassTransformation transformation,
MutableComponentModel model) {
for(final TransformMethod method:
transformation.matchMethodsWithAnnotation(XHR.class)){
TransformMethodSignature signature = method.getSignature();
if(!"void".equals(signature.getReturnType())){
method.addAdvice(new ComponentMethodAdvice(){
public void advise(ComponentMethodInvocation invocation) {
invocation.proceed();
Object result = invocation.getResult();
if(!request.isXHR()){
result = defaultForReturnType(result.getClass());
}
invocation.overrideResult(result);
}
});
}else {
throw new RuntimeException("XHR can be applied to non-void event
handlers only");
}
}
}
private Object defaultForReturnType(Class<?> returnType){
if (!returnType.isPrimitive()) return null;
if (returnType.equals(boolean.class)) return false;
return 0;
}
}
I copied the method "defaultForReturnType()" from ChainBuilderImpl
regards
Taha
On Mon, Jan 24, 2011 at 12:24 PM, Howard Lewis Ship <hl...@gmail.com>wrote:
> I suppose this is useful, but what it does is to invoke the method
> regardless of whether XHR is true or not, and then throw away the result.
>
> It probably should be:
>
> if (request.isXHR()) {
> invocation.proceed();
> }
>
> I'm not sure what the else should be ... probably return null/0/false
> (depending on the return type of the annotated method).
>
>
> On Sun, Jan 23, 2011 at 4:42 AM, Thiago H. de Paula Figueiredo <
> thiagohp@gmail.com> wrote:
>
> > On Sun, 23 Jan 2011 10:01:17 -0200, Taha Hafeez <
> tawus.tapestry@gmail.com>
> > wrote:
> >
> > Thanks Thiago
> >>
> >
> > :)
> >
> > You are absolutely right, in case of void it can throw an exception, i
> >> will modify it and put it in my examples at
> >> http://code.google.com/p/tapestry-addons/
> >>
> >
> > Why not Apache Extras (http://apache-extras.org/)? More info here:
> > https://blogs.apache.org/comdev/entry/why_apache_extras.
> >
> > --
> > Thiago H. de Paula Figueiredo
> > Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> > and instructor
> > Owner, Ars Machina Tecnologia da Informação Ltda.
> > http://www.arsmachina.com.br
> >
> > ---------------------------------------------------------------------
> > 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
>
> The source for Tapestry training, mentoring and support. Contact me to
> learn
> how I can get you up and productive in Tapestry fast!
>
> (971) 678-5210
> http://howardlewisship.com
>
Re: @XHR : isXHR shortcut
Posted by Howard Lewis Ship <hl...@gmail.com>.
I suppose this is useful, but what it does is to invoke the method
regardless of whether XHR is true or not, and then throw away the result.
It probably should be:
if (request.isXHR()) {
invocation.proceed();
}
I'm not sure what the else should be ... probably return null/0/false
(depending on the return type of the annotated method).
On Sun, Jan 23, 2011 at 4:42 AM, Thiago H. de Paula Figueiredo <
thiagohp@gmail.com> wrote:
> On Sun, 23 Jan 2011 10:01:17 -0200, Taha Hafeez <ta...@gmail.com>
> wrote:
>
> Thanks Thiago
>>
>
> :)
>
> You are absolutely right, in case of void it can throw an exception, i
>> will modify it and put it in my examples at
>> http://code.google.com/p/tapestry-addons/
>>
>
> Why not Apache Extras (http://apache-extras.org/)? More info here:
> https://blogs.apache.org/comdev/entry/why_apache_extras.
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> 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
The source for Tapestry training, mentoring and support. Contact me to learn
how I can get you up and productive in Tapestry fast!
(971) 678-5210
http://howardlewisship.com
Re: @XHR : isXHR shortcut
Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sun, 23 Jan 2011 10:01:17 -0200, Taha Hafeez <ta...@gmail.com>
wrote:
> Thanks Thiago
:)
> You are absolutely right, in case of void it can throw an exception, i
> will modify it and put it in my examples at
> http://code.google.com/p/tapestry-addons/
Why not Apache Extras (http://apache-extras.org/)? More info here:
https://blogs.apache.org/comdev/entry/why_apache_extras.
--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: @XHR : isXHR shortcut
Posted by Taha Hafeez <ta...@gmail.com>.
Thanks Thiago
You are absolutely right, in case of void it can throw an exception, i will
modify it and put it in my examples at
http://code.google.com/p/tapestry-addons/
regards
Taha
On Sun, Jan 23, 2011 at 5:24 PM, Thiago H. de Paula Figueiredo <
thiagohp@gmail.com> wrote:
> On Sun, 23 Jan 2011 03:42:09 -0200, Taha Hafeez <ta...@gmail.com>
> wrote:
>
> Hi
>>
>
> Hi, Taha!
>
>
> This is a simple example of using ComponentClassTransformWorker and also
>> may be used for graceful degradation for ajax in case javascript is disabled
>> All it does is convert a call
>> @XHR
>> Object onClick(){
>> //Processing ...
>> return zone.getBody();
>> }
>>
>> to
>>
>> Object onClick(){
>> //Processing
>> if(!request.isXHR()){
>> return null;
>> }else {
>> return zone.getBody();
>> }
>> }
>>
>
> This is a very cool example! Thanks for posting it! :)
>
> Just one suggestion: instead of ignoring @XHR on method that return void,
> I'd throw an exception, so the developer is warned that the annotation won't
> work. ;)
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
Re: @XHR : isXHR shortcut
Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sun, 23 Jan 2011 03:42:09 -0200, Taha Hafeez <ta...@gmail.com>
wrote:
> Hi
Hi, Taha!
> This is a simple example of using ComponentClassTransformWorker and also
> may be used for graceful degradation for ajax in case javascript is
> disabled
> All it does is convert a call
> @XHR
> Object onClick(){
> //Processing ...
> return zone.getBody();
> }
>
> to
>
> Object onClick(){
> //Processing
> if(!request.isXHR()){
> return null;
> }else {
> return zone.getBody();
> }
> }
This is a very cool example! Thanks for posting it! :)
Just one suggestion: instead of ignoring @XHR on method that return void,
I'd throw an exception, so the developer is warned that the annotation
won't work. ;)
--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org