You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Bastian Voigt <po...@bastian-voigt.de> on 2007/01/26 14:48:35 UTC

Passing Array to JavaScript ?

Hi,

I have a simply question: is it possible to pass an array to a javascript 
included with @Script? How can it be done?

I have an array of Point objects (which consist of a latitude and a longitude 
value) and I want to pass them to my script, so that the line of points can 
be displayed on a map.

Thanks for your time!


-- 
Bastian Voigt
Neumünstersche Straße 4
20251 Hamburg
telefon 040/67957171
mobil   0179/4826359

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


Re: Passing Array to JavaScript ?

Posted by Andrea Chiumenti <ki...@gmail.com>.
and you can also provide a method do your component/page java class, say
String getPointsJsArray()

and provide it as input-symbol to your script.

kiuma

On 1/29/07, Matt Brock <br...@gmail.com> wrote:
>
>
>
> Bastian Voigt wrote:
> >
> > I have a simply question: is it possible to pass an array to a
> javascript
> > included with @Script? How can it be done?
> >
> > I have an array of Point objects (which consist of a latitude and a
> > longitude
> > value) and I want to pass them to my script, so that the line of points
> > can
> > be displayed on a map.
> >
>
> Yes, this is fairly easy using the FOREACH declaration inside the SCRIPT
> definition.  It works much like the FOREACH component.
>
> So, for example:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE script PUBLIC "-//Apache Software Foundation//Tapestry Script
> Specification 3.0//EN"
> "http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">
> <script>
>   <input-symbol key="pointsArray" class="java.util.ArrayList"
> required="yes"/>
>   <body>
>     var Points = {
>       elements: [],
>       pointsObj: function(latitude, longitude) {
>         this.latitude = latitude;
>         this.longitude = longitude;
>       },
>       add: function(id, latitude, longitude) {
>         Points.elements[id] = new Points.pointsObj(latitude, longitude);
>       },
>       remove: function(id) {
>         delete Points.elements[id];
>       }
>     };
>   </body>
>   <initialization>
>     <foreach expression="pointsArray" key="keyPoint">
>       Points.add(${keyPoint.id},${keyPoint.latitude},${keyPoint.longitude
> });
>     </foreach>
>   </initialization>
> </script>
>
> The body is just creating a simple JavaScript object to hold your points
> for
> easy access, plus a couple of convenience methods.  The real meat is in
> the
> initialization part.
> --
> View this message in context:
> http://www.nabble.com/Passing-Array-to-JavaScript---tf3122349.html#a8684293
> 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: Passing Array to JavaScript ?

Posted by Matt Brock <br...@gmail.com>.

Bastian Voigt wrote:
> 
> I have a simply question: is it possible to pass an array to a javascript 
> included with @Script? How can it be done?
> 
> I have an array of Point objects (which consist of a latitude and a
> longitude 
> value) and I want to pass them to my script, so that the line of points
> can 
> be displayed on a map.
> 

Yes, this is fairly easy using the FOREACH declaration inside the SCRIPT
definition.  It works much like the FOREACH component.

So, for example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script PUBLIC "-//Apache Software Foundation//Tapestry Script
Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">
<script>
  <input-symbol key="pointsArray" class="java.util.ArrayList"
required="yes"/>
  <body>
    var Points = {
      elements: [],
      pointsObj: function(latitude, longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
      },
      add: function(id, latitude, longitude) {
        Points.elements[id] = new Points.pointsObj(latitude, longitude);
      },
      remove: function(id) {
        delete Points.elements[id];
      }
    };
  </body>
  <initialization>
    <foreach expression="pointsArray" key="keyPoint">
      Points.add(${keyPoint.id},${keyPoint.latitude},${keyPoint.longitude});
    </foreach>
  </initialization>
</script>

The body is just creating a simple JavaScript object to hold your points for
easy access, plus a couple of convenience methods.  The real meat is in the
initialization part.
-- 
View this message in context: http://www.nabble.com/Passing-Array-to-JavaScript---tf3122349.html#a8684293
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