[dev] [commits] Horde-Hatchery branch master updated.

Michael M Slusarz slusarz at horde.org
Fri Feb 20 18:20:12 UTC 2009


Quoting Jan Schneider <jan at horde.org>:

>     Associative PHP arrays are serialized as JSON arrays when empty,  
> instead of
>     objects. $H() only works with objects though.
>     Michael, have you seen this earlier and solved it differently?

At one point, I thought I was casting all arrays to stdClass objects,  
but I don't see that code anymore.

The better solution in general is simply to check for empty-ness in  
the server code and if the value is empty, don't set the value at all  
in the to-be-sent JSON object.  Then you can just do existence  
checking in the JS code.  This has the advantage of making JSON  
encoding/decoding a bit faster and reduces the size of the data object  
sent to the browser in the AJAX response.  Same goes for false-ish  
values - there is simply no need to set these variables if false.  An  
example from imp/lib/Views/ListMessages.php:

         if ($sortpref['limit']) {
             $md->sortlimit = 1;
         }

If there is no limit, there is no need to even set sortlimit.

For example, in your _monthCallback function, don't set 'events' if  
empty and simply add existence checking:

if (r.response.events) {
     $H(r.response.events).each(function(date) {
         ...

michael

-- 
___________________________________
Michael Slusarz [slusarz at horde.org]



More information about the dev mailing list