Hi greg,

try this:

Object.extend(Form.Methods,{
unserialize: function(element, source) {
  if (!(element = $(element)))
    throw new Error('DOMElement is required');

  source = Object.isString(source)
    ? source.toQueryParams()
    : source;

  element.getElements().each(function(element) {
    for (var name in source) {
      if (name == element.name)
        element.setValue(source[name]);
    }
  })
  return element;
}
});


as you extend the Form.method, this method could not be used with the
$ utility, but with the $F utility, so call it with:
$F("form1").unserialize("form1",data)

verify the syntax, this is not tested.
If trouble, repost, I could do some test to set the correct syntax :))

-

david

On 13 nov, 00:31, greg <g...@reservation-net.com> wrote:
> Some kind soul out there has created a form unserialize method (below)
> that I would like to use.  Being a newbie I can't figure out how to do
> that.
>
> My first kick at the can was
> Object.extend(Form.Methods, Form.Methods.unserialize);
> and to use it was:
> $("form1").unserialize("form1",data)
>
> but that didn't work.  I've since tried various permutations and
> combinations of this, to no avail.
>
> I did get it to work by simply making it a function, but a method
> would be nicer.
>
> Can anyone point me in the right direction?
>
> Thank you!
>
> /**
> * Form#unserialize(@element, source) -> Element
> * - @element(FormElement): Form element to fill with values
> * - source(Object | String): Source object where field values are
> taken from
> *
> * Fills form with values of a given object `source`.
> * Each propertie name of `source` is compared to name attribute of a
> form element.
> *
> * $('myForm').unserialize()
> *
> **/
> Form.Methods.unserialize = function(element, source) {
>   if (!(element = $(element)))
>     throw new Error('DOMElement is required');
>
>   source = Object.isString(source)
>     ? source.toQueryParams()
>     : source;
>
>   element.getElements().each(function(element) {
>     for (var name in source) {
>       if (name == element.name)
>         element.setValue(source[name]);
>     }
>   })
>   return element;
>
> };
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to