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);});
Regards,
Richard Quadling.
--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
hab da was fuer Dich.
Ist fuer Dich bestimmt von Interesse.
http://www.heise.de/newsticker/Webbrowser-ohne-Installation-testen--/meldung/133454
MfG
Manfred
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
That depends on the backend of course.
For me, this would be rarely (if ever) a problem, cause I am trying
to keep my returned types consistent.
If I return an associative array from my backend, then it should be
always associative array. And I would always expect plain Object in
json response. I this case that workaround works.
If I return numerically indexed array, then it should always be so,
and then I always expect native Array in the response. In this case I
would use $A instead of $H anyway, so no workaround is needed.
Of course there might be persons who mix those types and return array
of objects when found many, and just the object, when found one. I
have no idea how to distinguish such cases... But this would require
extra js logic anyway, so I think it would not be a problem.
I agree with Richard, that there should be a warning for php users in
the docs, and a sample snippet of js code providing a way to avoid
accidental passing of empty Array to $H.
I'll try to write some info covering this on proto-scripty.wikidot.com
tonight. It might be of some help to many.
If you have any better idea how to avoid this problem,
let us know.
Regards,
SWilk