You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Andy Pahne <an...@gmail.com> on 2009/07/31 11:52:38 UTC

AjaxFormLoop: property changes have no effect

I am using an AjaxFormLoop component. There is no database involved, so 
the code should be simple.

The add operation in onAddRowFromVehicles() works. I can see that 
because the list of vehicles has alwas the correct size. But Properties 
of Vehicle (like vehicle.model) are never updated, no matter what I type 
into the for fields.

Any hints? I have no idea and think that it should work because my list 
of vehicles is persisted.


Here is a snippet of my relevant parts:


class Page{

    @Property @Persist
    private List<Vehicle> vehicles;
   
    @Property
    private Vehicle vehicle;

        Object onAddRowFromVehicles() {
        Vehicle vehicle = new Vehicle();
        vehicle.setType(VehicleType.PKW);
        this.vehicles.add(vehicle);
        return vehicle;
       
    }


    public ValueEncoder<Vehicle> getVehicleEncoder(){
        return new VehicleValueEncoder();
    }
   

  public class VehicleValueEncoder implements ValueEncoder<Vehicle>{

        private static final String SEP = "|";

        @Override
        public String toClient(Vehicle value) {
            StringBuffer result = new StringBuffer();
            result.append("vehicle");
            result.append(SEP);
            result.append(value.getType().name());
            result.append(SEP);
            result.append(value.getModel());
            result.append(SEP);
            result.append(value.getLicenseNumber());
            result.append(SEP);
            result.append(value.getLengthCentimeter());
            result.append(SEP);
            result.append(value.getHeightCentimeter());
            result.append(SEP);
            return result.toString();
        }

       
        @Override
        public Vehicle toValue(String clientValue) {
           
            String[] splitted = clientValue.split("\\" + SEP);  // use a 
regex
           
            Vehicle vehicle = new Vehicle();
            vehicle.setType(VehicleType.valueOf(splitted[1]));
            vehicle.setModel(splitted[2]);
            vehicle.setLicenseNumber(splitted[3]);
            vehicle.setLengthCentimeter(Integer.valueOf(splitted[4]));
            vehicle.setHeightCentimeter(Integer.valueOf(splitted[5]));
           
            return vehicle;
           
        }
       
    }
   
   

   
   

}


TML-File
---------------

...
    <form t:type="Form" 
          t:id="request"
          t:clientValidation="true"
          t:autofocus="false">
...
            <fieldset class="column">
                <legend>${message:firstName-label}</legend>
                <input type="text"
                       t:type="TextField"
                       t:value="firstName"
                       t:id="firstName"
                       name="first_name"/>
            </fieldset>

   <t:AjaxFormLoop t:id="vehicles" t:source="vehicles" t:value="vehicle" 
encoder="vehicleEncoder">
        
                  <fieldset class="column first" style="width:110px;">
                    <legend>${message:vehicle-type-label}</legend>
                    <select name="vehicleType"
                            t:id="vehicleType"
                            t:type="Select"
                            t:value="vehicle.type"
                            t:blankOption="NEVER"
                            style="width:110px;"></select>
                  </fieldset>
                 
                 
                  <fieldset class="column small left" 
style="position:relative; left:-10px;">
                        <legend>${message:vehicle-model-label}</legend>
                        <input type="text"
                               t:type="TextField"
                               t:value="vehicle.model"
                               t:id="vehicleModel"
                                style="width:95px;"/>
                  </fieldset>
                 
                  <fieldset class="column small right" 
style="position:relative; left:-10px; width=95px;">
                        <legend>${message:vehicle-license-label}</legend>
                        <input type="text"
                               t:type="TextField"
                               t:value="vehicle.licenseNumber"
                               t:id="vehicleLicenseNumber"
                               style="width:95px;"/>
                  </fieldset>
                 

                 
                  <fieldset class="column last small left" 
style="position:relative; left:+10px;">
                        <legend>${message:vehicle-length-label}</legend>
                        <input type="text"
                               t:type="TextField"
                               t:value="vehicle.lengthCentimeter"
                               size="4"
                               t:id="vehicleLength"/>
                  </fieldset>
                 
                  <fieldset class="column last small left" 
style="position:relative; left:+25px;">
                        <legend>${message:vehicle-height-label}</legend>
                        <input type="text"
                               t:type="TextField"
                               size="4"
                               t:value="vehicle.heightCentimeter"
                               t:id="vehicleHeight"/>
                  </fieldset>
                 
                  <fieldset class="clear">
                      <legend class="hidden">clear</legend>
                  </fieldset>

         </t:AjaxFormLoop>







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


Re: AjaxFormLoop: property changes have no effect

Posted by davidb82 <da...@thisisnumero.com>.

Andy Pahne-7 wrote:
> 
> 
> I am using an AjaxFormLoop component. There is no database involved, so 
> the code should be simple.
> 
> The add operation in onAddRowFromVehicles() works. I can see that 
> because the list of vehicles has alwas the correct size. But Properties 
> of Vehicle (like vehicle.model) are never updated, no matter what I type 
> into the for fields.
> 
> Any hints? I have no idea and think that it should work because my list 
> of vehicles is persisted.
> 
> 
> Here is a snippet of my relevant parts:
> 
> 
> class Page{
> 
>     @Property @Persist
>     private List<Vehicle> vehicles;
>    
>     @Property
>     private Vehicle vehicle;
> 
>         Object onAddRowFromVehicles() {
>         Vehicle vehicle = new Vehicle();
>         vehicle.setType(VehicleType.PKW);
>         this.vehicles.add(vehicle);
>         return vehicle;
>        
>     }
> 
> 
>     public ValueEncoder<Vehicle> getVehicleEncoder(){
>         return new VehicleValueEncoder();
>     }
>    
> 
>   public class VehicleValueEncoder implements ValueEncoder<Vehicle>{
> 
>         private static final String SEP = "|";
> 
>         @Override
>         public String toClient(Vehicle value) {
>             StringBuffer result = new StringBuffer();
>             result.append("vehicle");
>             result.append(SEP);
>             result.append(value.getType().name());
>             result.append(SEP);
>             result.append(value.getModel());
>             result.append(SEP);
>             result.append(value.getLicenseNumber());
>             result.append(SEP);
>             result.append(value.getLengthCentimeter());
>             result.append(SEP);
>             result.append(value.getHeightCentimeter());
>             result.append(SEP);
>             return result.toString();
>         }
> 
>        
>         @Override
>         public Vehicle toValue(String clientValue) {
>            
>             String[] splitted = clientValue.split("\\" + SEP);  // use a 
> regex
>            
>             Vehicle vehicle = new Vehicle();
>             vehicle.setType(VehicleType.valueOf(splitted[1]));
>             vehicle.setModel(splitted[2]);
>             vehicle.setLicenseNumber(splitted[3]);
>             vehicle.setLengthCentimeter(Integer.valueOf(splitted[4]));
>             vehicle.setHeightCentimeter(Integer.valueOf(splitted[5]));
>            
>             return vehicle;
>            
>         }
>        
>     }   
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

I think the problem is with your ValueEncoder - it just instantiates a new
Vehicle which doesn't get stored back in your list. 

Your code would probably work better if you did something like:

public class VehicleValueEncoder implements ValueEncoder<Integer>{

        @Override
        public Integer toClient(Vehicle value) {
                   
               return vehicles.indexOf(value);          
        }

       
        @Override
        public Vehicle toValue(Integer clientValue) {

               return vehicles.get(clientValue);
        }       
    }   

Or something like that anyway... 
-- 
View this message in context: http://www.nabble.com/AjaxFormLoop%3A-property-changes-have-no-effect-tp24754470p24793930.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: AjaxFormLoop: property changes have no effect

Posted by Steve Eynon <st...@alienfactory.co.uk>.
Excellent - I'm really happy I found this email for I to, fell into
the same trap.

I entered the world of ValueEncoders after playing with Field
Translators and TypeCoercers and thought maybe they did the same
thing. I also (wrongly) thought that as I supplied the AjaxFormLoop
with a source List, Tapestry would be able to back track through the
list and set the properties for me (much the same as it read them in
the first place).

Anyhow, it all works now and I just thought I'd keep this thread
relevant in case anyone else has the same problem.

Steve.


On 5 August 2009 07:22, Andy Pahne wrote:
>
> You are right, that was a misunderstanding on my side.
>
> Now I got it. Thanks. For the curious, here is the working code:
>
>   public ValueEncoder getVehicleEncoder(){
>       return new ValueEncoder() {
>
>           @Override public String toClient(Object value) {
>               return String.valueOf(vehicles.indexOf(value));
>           }
>
>           @Override public Object toValue(String clientValue) {
>               return vehicles.get(new Integer(clientValue));
>           }
>                 };
>   }
>
>
>
> Still I had to take a little care that my Vehicle' equal() method returns
> some meaningfull value....
>
>
> Thank you for your feedback.
> Andy

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


Re: AjaxFormLoop: property changes have no effect

Posted by Andy Pahne <an...@gmail.com>.
You are right, that was a misunderstanding on my side.

Now I got it. Thanks. For the curious, here is the working code:

    public ValueEncoder getVehicleEncoder(){
        return new ValueEncoder() {

            @Override public String toClient(Object value) {
                return String.valueOf(vehicles.indexOf(value));
            }

            @Override public Object toValue(String clientValue) {
                return vehicles.get(new Integer(clientValue));
            }
           
        };
    }



Still I had to take a little care that my Vehicle' equal() method 
returns some meaningfull value....


Thank you for your feedback.
Andy






Daniel Jones schrieb:
> Hi Andy,
>
> I think you are a bit confused regarding the ValueEncoder.
>
> Your toClient method should provide a unique string identifier which can be
> used to
> retrieve that particular vehicle from your list.
>
> Your toValue method should accept the unique string identifier provided by
> the toClient method and use this to retrieve the SAME object from your list.
>
> Currently, in your toValue method, you are just creating a new Vehicle
> object, which is not the same object that is contained in your list (which
> your loop is reading from).  What you want to do in this method is search
> your vehicles list for the correct object.
>
> I hope this helps.
> Regards,
> Daniel
>
>   


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


Re: AjaxFormLoop: property changes have no effect

Posted by Daniel Jones <da...@murieston.com>.
Hi Andy,

I think you are a bit confused regarding the ValueEncoder.

Your toClient method should provide a unique string identifier which can be
used to
retrieve that particular vehicle from your list.

Your toValue method should accept the unique string identifier provided by
the toClient method and use this to retrieve the SAME object from your list.

Currently, in your toValue method, you are just creating a new Vehicle
object, which is not the same object that is contained in your list (which
your loop is reading from).  What you want to do in this method is search
your vehicles list for the correct object.

I hope this helps.
Regards,
Daniel


Andy Pahne-7 wrote:
> 
> 
>   public class VehicleValueEncoder implements ValueEncoder<Vehicle>{
> 
>         private static final String SEP = "|";
> 
>         @Override
>         public String toClient(Vehicle value) {
>             StringBuffer result = new StringBuffer();
>             result.append("vehicle");
>             result.append(SEP);
>             result.append(value.getType().name());
>             result.append(SEP);
>             result.append(value.getModel());
>             result.append(SEP);
>             result.append(value.getLicenseNumber());
>             result.append(SEP);
>             result.append(value.getLengthCentimeter());
>             result.append(SEP);
>             result.append(value.getHeightCentimeter());
>             result.append(SEP);
>             return result.toString();
>         }
> 
>        
>         @Override
>         public Vehicle toValue(String clientValue) {
>            
>             String[] splitted = clientValue.split("\\" + SEP);  // use a 
> regex
>            
>             Vehicle vehicle = new Vehicle();
>             vehicle.setType(VehicleType.valueOf(splitted[1]));
>             vehicle.setModel(splitted[2]);
>             vehicle.setLicenseNumber(splitted[3]);
>             vehicle.setLengthCentimeter(Integer.valueOf(splitted[4]));
>             vehicle.setHeightCentimeter(Integer.valueOf(splitted[5]));
>            
>             return vehicle;
>            
>         }
>        
>     }
> 

-- 
View this message in context: http://www.nabble.com/AjaxFormLoop%3A-property-changes-have-no-effect-tp24754470p24812971.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