[Proto-Scripty] Re: Reopen thread: Creating hash with $H differs from 1.5 to 1.6

2008-09-24 Thread amagondes

hi, did you try adding a temporary item to the errors array in php
just before sending the json back and then removing it in your js code
before checking errors.size(). for example

$json = array('errors' => array_merge(array('REMOVE' => ''), $fp-
>getErrors());
$this->sendJson($json);

then in your js do something like this:

errors.unset('REMOVE');

if(errors.size() > 0) {
   // do your stuff
}

this is really just a hack but it might help



On Sep 16, 9:38 am, MichaelS <[EMAIL PROTECTED]> wrote:
> json.errors isn't an array, but an undefined object. You can't use it
> like an array and casting it to $A doesn't help either. Since the
> errors coming back originally were an array, you'd think this would
> work, but it doesn't.
>
> The frustration is that the original code works when json.errors has
> values, but doesn't when it's empty.
>
> The code that creates the json:
>             $json = array('errors' => $fp->getErrors());
>             $this->sendJson($json);
>
> Any thoughts on how we could evaluate json.errors when it's an empty
> representation( "{"errors":[]}")?
>
> MS
>
> on July 17, kangax   said:
>
>
>
> >Looks like you're creating a hash from an array (json.errors). Hash
> >actually doesn't know that it's an array and happily iterates over its
> >properties.
> >You then end up with a hash that has properties/methods from
> >Array.prototype (since for/in shows prototype.js-defined
> >Array.prototype methods).
> >Remove hash and use a plain array (when iterating over json.errors) or
> >represent your errors as an object (which is the best way to hold key/
> >value pairs anyway)
> >><[EMAIL PROTECTED]> wrote:
> >> Hi,
> >> I have this snippet of code that uses the $H to create hash out of my
> >> form errors. This works perfectly fine in prototype version 1.5 but
> >> when I upgraded to version 1.6 it failes to work when no errors occur.
> >> When I have no errors my reponse text is this "{"errors":[]}" but
> >> errors.size() is always greater than 0. SHould I be doing something
> >> different?
> >>         onFormSuccess : function(transport)
> >>         {
> >>                 var json = transport.responseText.evalJSON(true);
> >>                 var errors = $H(json.errors);
> >>                 if (errors.size() > 0) {
> >>                         this.form.down('.error').show();
> >>                         errors.each(function(pair){
> >>                                 this.showError(pair.key, pair.value);
> >>                         }.bind(this));
> >>                 }
> >>                 else{
> >>                         this.form.submit();
> >>                 }
> >>         }- Hide quoted text -
>
> - Show quoted text -

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Reopen thread: Creating hash with $H differs from 1.5 to 1.6

2008-09-16 Thread kangax

On Sep 16, 9:38 am, MichaelS <[EMAIL PROTECTED]> wrote:
> json.errors isn't an array, but an undefined object. You can't use it

What exactly is an "undefined object"?

> like an array and casting it to $A doesn't help either. Since the
> errors coming back originally were an array, you'd think this would
> work, but it doesn't.
>
> The frustration is that the original code works when json.errors has
> values, but doesn't when it's empty.

It shouldn't matter if `errors` is represented as an empty array
(rather than `undefined`, if I understand you correctly)

>
> The code that creates the json:
>             $json = array('errors' => $fp->getErrors());
>             $this->sendJson($json);
>
> Any thoughts on how we could evaluate json.errors when it's an empty
> representation( "{"errors":[]}")?

'{"errors":[]}'.evalJSON().errors; // [];
'{"errors":[]}'.evalJSON().errors.length; // 0;

I'm not following what the issue is.

>
> MS

--
kangax
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---