2009/2/25 SWilk <[email protected]>: > > kangax wrote: >> On Feb 24, 8:43 am, Richard Quadling <[email protected]> wrote: >>> Hi. >>> >>> I'm using JSONP to pass data from the server to the client. >>> >>> Normally the call is like ... >>> >>> tabRA_UpdateReportIDs({"13":"Richard Quadling's demo report (Richard's >>> complicated report)"}); >>> >>> A hash. >>> >>> In some instances, there is no data and the call looks like this ... >>> >>> tabRA_UpdateReportIDs([]); >>> >>> An array. >>> >>> As I understand things, this is not incorrect. I'm using PHP and I've >>> placed a request to allow for an option to force empty arrays to be an >>> empty hash for the json_encode() function. On the json_decode() >>> function, there is an option allowing the user to decide between an >>> object or an associative array (object or hash), so having a similar >>> option on the json_encode() side would, on the surface, seem useful. >>> (http://bugs.php.net/bug.php?id=47493) >>> >>> My point in contacting this group is to see if ... >>> >>> $H([]) >>> >>> should work like ... >>> >>> $H({}) >>> >>> If you run these commands in console you get quite different output ... >>> >>> $H({}).each(function(h){console.debug(h);}); >>> >>> vs .. >>> >>> $H([]).each(function(h){console.debug(h);}); >> >> `$H([])` produces something that it was never intended to produce - an >> object extended with Array.prototype.* methods. You probably don't >> ever want to use it. >> > > It's not that Richard does want to use it. It's that the PHP > json_encode() function produces inconsistent output for empty arrays. > > If you do > json_encode(array('key' => 'value'); > you will get: > { key: value }; > but > json_encode(array()); > returns: > []; > > So it is possible, that the PHP backend returns unexpectedly empty > array instead of object. The problem clearly is on the PHP side, and > Richard propsed a patch for this issue. > > Anyway, until this is fixed, and until PHP is updated on the servers > used by prototype users, a workaround is *needed*. > > I think we should propose a pattern which would be used to avoid this > behaviour. > Do you have an idea how to reliably avoid that? > > I think in most cases: > var _a=[]; > _a = (Object.isArray(_a) && !_a.length) ? {} : _a; > var hash = $H(_a); > > ...would be sufficient, as not empty arrays should not be passed to > $H(). What do you think? > > -- > Regards, > SWilk > > > >
The JS workaround is fine for me. I don't have any ability to force the patch in PHP, so from my POV, fixing the client side code is my only option. Admittedly, this is an edge case, so if prototype doesn't take this on board directly, can I recommend adding a note or something to the documentation to cover this issue. The JS workaround that has been suggested should also be present. Regards, Richard. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] 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 -~----------~----~----~----~------~----~------~--~---
