You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Edward <st...@sby.dnet.net.id> on 2003/07/23 16:04:49 UTC

JavaScript's Array

Hi,
Is there any way to store/catch/take javaScript 's Array to List type variable?

I have a table which is editable that contains records, edited dynamically 
on client side, so it's bunch of record, not one by one.
then after user has done with it, the button will work with take all 
arrays  then store into database.

Thanks for help

Regards

Ed.


RE: JavaScript's Array

Posted by Edward <st...@sby.dnet.net.id>.
thanks for help
I really appreciate this. i'll try your solutions.
Point #2 is exactly my prob

regards

Ed.

At 13:54 24/07/2003 +0200, you wrote:
>Hey up,
>
>I've had this problem, too.  There are two solutions, I think, depending
>on the exact details of your problem.  Here are the distinctions between
>the two problems and then afterwards I discuss the solutions:


RE: JavaScript's Array

Posted by Hugh Leather <hu...@hotmail.com>.
Hey up,

I've had this problem, too.  There are two solutions, I think, depending
on the exact details of your problem.  Here are the distinctions between
the two problems and then afterwards I discuss the solutions:

BTW, I am by no means a guru, so it might be wise to get other opinions,
too :-)

Problem 1
=========
You're doing MVC in DHTML (to reduce page refresh).  Here the client's
browser has a javascript data model which drives the UI.  The actual
form elements do not represent all the data in the model.  Therefore if
you submit the form, you will be missing data.  You consider creating
enough form elements to adequately represent the data a bad solution
(code complexity and having lots gui elements can murder a browser).  

Problem 2
=========
You're just trying to reduce page refresh but the form elements contain
all the data.  A typical example is that you give the user 'add row' and
'delete row' buttons but want those to not cause refresh.

For simplicity we will not want to remember which of the original rows
are still there.  i.e. we won't be merging the resulting list back in,
just replacing it.

Solution to Problem 1
=====================
This problem arises a lot I think when doing things like MVC DHTML so
that page refreshes are reduced.  The approach suffers from the
inability to serialize and deserialize javascript.  I am in the process
of creating an open source project to do just that (but I don't have a
lot of time to do it in, so it might be a while).  I'll post here when I
release the first version.

In the meantime, you might try something simple like this: 

Suppose that you have an array of objects, root, and each element only
has simple fields (i.e. we don't have much of an object graph, just a
simple tree - depth 2).

function serialize( root ) {
  // Acumulate the serialization in an array - much faster than adding
to a  
  // string.
  var chunks = new Array(); 

  for( var i = 0; i < root.length; ++i ) {
    var obj = root[ i ];
    
    chunks.push( "obj:" + getConstructorName( obj ));
    
    for( var j in obj ) {
      switch( typeof obj[ j ] ) {
      
      case "object" : 
        // stuff.
	  break;

	case "string" : 
	  chunks.push( j + "=s" + serialEscape( obj[ j ] ));
        break;
        
      case "number" :
        chunks.push( j + "=n" + obj[ j ] );
        break;

      case "function" :
        // Do not serialize functions!
        break;

      // Other cases 
    }
  }

  return chunks.join( separator );
}

Obviously, I've left out lots of details, special cases etc.  If this is
close to your problem, mail me and we could discuss it more if you like.

Then you put the result into a hidden field, submit and reverse the
process on the server (during the rewind phase).  In fact as long as you
don't end up having to this very often, it's quite simple.  Having to do
this more than once is very annoying and that's why I going to open
source a solution to the problem.

Solution to Problem 2
=====================
This is pretty straight forward, if a little clunky, but there are a
couple of gatchas.  

Our javascript for the add and delete buttons is as expected, either
writing out the html into the page or modifying the DOM.  

It will represent each row as a set of input elements like this:

<input name="{component-id}.{col-name}.{row-index}" value="..."
type="..."/>

We will, for simplicity, also have a hidden field that contains the
number of rows to expect:

<input name="{component-id}.num-rows" value="..." type="hidden"/>

And I think you can see how this is going to go.  In the rewind phase of
our table component we need to examine the request parameters.  We
create an array of {component-id}.num-rows elements to store our
results.  Then we find any request parameter that of the form
{component-id}.{col-name}.{row-index} and set the appropriate property.

Prety much done.  Again, this isn't something you'd want to do very
often, but the code for the above could probably be nicely generalized.

The two gotchas are:

1. you can't enumerate request parameters (at least when using file
upload).
This means that you have to work out what they are.  But, you will
notice that in this case, if we know the column names and the number of
rows then we have all the information we need to know the request
parameter names.
BTW, there is a bug in bugzilla to try and get parameters to be
enumerable.

2. DO NOT CREATE AN ARRAY BASED ON THE {component-id}.num-rows
PARAMETER!
This is a very easy way to let people kill your site.  All they have to
do is submit a very large number and you start making big arrays, ouch!
Instead, create a map, populate the values, check that all is as
expected and then create the array from the map.  (or limit the size of
the array you can create.)



I hope this helps (and that I haven't completely misunderstood your
problem :-)

	Cheers,

	Hugh.	
   
	
 
      

-----Original Message-----
From: Marilen Corciovei [mailto:len@nemesisit.rdsnet.ro] 
Sent: 24 July 2003 04:08
To: Tapestry users
Cc: stcode@sby.dnet.net.id
Subject: Re: JavaScript's Array

Hi,

I am not sure the javascript relation to your problem. It is posible to
edit multiple records with tapestry (like multiple rows in a database)
using the ListEdit component.

Len.

On Thu, 2003-07-24 at 05:04, Edward wrote:
> Noone could help me? :(
> it's tapestry related problem, if you think it's OOT
> or anyone could help me to direct me to some urls that may help?
> 
> thanks
> 
> At 21:04 23/07/2003 +0700, you wrote:
> >Hi,
> >Is there any way to store/catch/take javaScript 's Array to List type

> >variable?
> >
> >I have a table which is editable that contains records, edited
dynamically 
> >on client side, so it's bunch of record, not one by one.
> >then after user has done with it, the button will work with take all 
> >arrays  then store into database.
> >
> >Thanks for help
> >
> >Regards
> >
> >Ed.
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail:
tapestry-user-help@jakarta.apache.org
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
-- 
Marilen Corciovei <le...@nemesisit.rdsnet.ro>


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


Re: JavaScript's Array

Posted by Marilen Corciovei <le...@nemesisit.rdsnet.ro>.
Hi,

I am not sure the javascript relation to your problem. It is posible to
edit multiple records with tapestry (like multiple rows in a database)
using the ListEdit component.

Len.

On Thu, 2003-07-24 at 05:04, Edward wrote:
> Noone could help me? :(
> it's tapestry related problem, if you think it's OOT
> or anyone could help me to direct me to some urls that may help?
> 
> thanks
> 
> At 21:04 23/07/2003 +0700, you wrote:
> >Hi,
> >Is there any way to store/catch/take javaScript 's Array to List type 
> >variable?
> >
> >I have a table which is editable that contains records, edited dynamically 
> >on client side, so it's bunch of record, not one by one.
> >then after user has done with it, the button will work with take all 
> >arrays  then store into database.
> >
> >Thanks for help
> >
> >Regards
> >
> >Ed.
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
-- 
Marilen Corciovei <le...@nemesisit.rdsnet.ro>


Re: JavaScript's Array

Posted by Edward <st...@sby.dnet.net.id>.
Noone could help me? :(
it's tapestry related problem, if you think it's OOT
or anyone could help me to direct me to some urls that may help?

thanks

At 21:04 23/07/2003 +0700, you wrote:
>Hi,
>Is there any way to store/catch/take javaScript 's Array to List type 
>variable?
>
>I have a table which is editable that contains records, edited dynamically 
>on client side, so it's bunch of record, not one by one.
>then after user has done with it, the button will work with take all 
>arrays  then store into database.
>
>Thanks for help
>
>Regards
>
>Ed.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>