[Proto-Scripty] Re: JSON and $H() to get size of returned JSON data
I suggest you read my previous comments about $A, and reponseJSON. $H is not made to iterate over arrays. To iterate over arrays, just use each Like so: [1, 2, 3].each(function(e) { console.log(e) }); Best, Tobie On Nov 22, 3:14 am, laf <[EMAIL PROTECTED]> wrote: > Thanks All, > > I managed to get it work like so; > > onFormSuccess : function(transport) > { > var json = transport.responseText.evalJSON(true); > > if(json.errors.length == 0) { > this.form.submit(); > } else { > var errorHash = $H(json.errors); > this.form.down('.error').show(); > errorHash.each(function(pair) { > this.showError(pair.key, pair.value); > }.bind(this)); > } > > } > > I still found I needed to use $H in the case that there were errors I > needed to show the user, so that I could iterate over the array. Let > me know if it can be improved. > > Regards, > laf --~--~-~--~~~---~--~~ 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: JSON and $H() to get size of returned JSON data
Thanks All, I managed to get it work like so; onFormSuccess : function(transport) { var json = transport.responseText.evalJSON(true); if(json.errors.length == 0) { this.form.submit(); } else { var errorHash = $H(json.errors); this.form.down('.error').show(); errorHash.each(function(pair) { this.showError(pair.key, pair.value); }.bind(this)); } } I still found I needed to use $H in the case that there were errors I needed to show the user, so that I could iterate over the array. Let me know if it can be improved. Regards, laf --~--~-~--~~~---~--~~ 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: JSON and $H() to get size of returned JSON data
if json.errors is an array, you don't need $A. $A is for iterables only (like dom node collections, for example). Best, Tobie On Nov 19, 5:01 am, "Jerod Venema" <[EMAIL PROTECTED]> wrote: > It looks like json.errors should be an array, right? > > var json = {"errors":[]} > > json.errors is an array...so $H(json.errors) is probably not what you want. > > Try $A(json.errors); > > You'll have to adjust your each as well... > > $A(json.errors).each(function(err){ > console.log(err.anError); //outputs "aMessage" > > }); > > Jerod Venemahttp://www.frozenmountain.com/ > > > > On Tue, Nov 18, 2008 at 6:48 PM, laf <[EMAIL PROTECTED]> wrote: > > > So I have this function. It toggles error messages after an AJAX call > > for validation. It works fine in prototype 1.5, > > but an upgrade to 1.6 has broken it and I have not been able to work > > out why. > > > 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(); > > } > > } > > > When the JSON received looks like {"errors":[{"anError": > > "aMessage"}]}, errors.size() works which means the errors are > > displayed as they are supposed to, but when the json received looks > > like {"errors":[]}, then errors.size() falls over as errors is an > > undefined object. > > > Basically, what is the correct way to get the size of the returned > > JSON data reliably with Prototype 1.6? $H() does not work when errors > > is empty. I know the hash was one of the biggest changes moving to > > 1.6.. I'd appreciate any light someone can shed on this. > > > I have tried checking if its null, or its length with no success. > > > Regards, > > laf. > > -- > Jerod Venema --~--~-~--~~~---~--~~ 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: JSON and $H() to get size of returned JSON data
It looks like json.errors should be an array, right? var json = {"errors":[]} json.errors is an array...so $H(json.errors) is probably not what you want. Try $A(json.errors); You'll have to adjust your each as well... $A(json.errors).each(function(err){ console.log(err.anError); //outputs "aMessage" }); Jerod Venema http://www.frozenmountain.com/ On Tue, Nov 18, 2008 at 6:48 PM, laf <[EMAIL PROTECTED]> wrote: > > So I have this function. It toggles error messages after an AJAX call > for validation. It works fine in prototype 1.5, > but an upgrade to 1.6 has broken it and I have not been able to work > out why. > >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(); >} >} > > When the JSON received looks like {"errors":[{"anError": > "aMessage"}]}, errors.size() works which means the errors are > displayed as they are supposed to, but when the json received looks > like {"errors":[]}, then errors.size() falls over as errors is an > undefined object. > > Basically, what is the correct way to get the size of the returned > JSON data reliably with Prototype 1.6? $H() does not work when errors > is empty. I know the hash was one of the biggest changes moving to > 1.6.. I'd appreciate any light someone can shed on this. > > I have tried checking if its null, or its length with no success. > > Regards, > laf. > > > > -- Jerod Venema --~--~-~--~~~---~--~~ 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: JSON and $H() to get size of returned JSON data
FWIW, you should be using response.responseJSON and setting the sanitizeJSON option of your ajax request to true. Would avoid evaluating the json object twice! Best, Tobie note: you'll need to set the mime-type of the response to 'application/ json' or the evalJSON option to 'force' On Nov 19, 2:05 am, kangax <[EMAIL PROTECTED]> wrote: > On Nov 18, 6:48 pm, laf <[EMAIL PROTECTED]> wrote: > > > > > So I have this function. It toggles error messages after an AJAX call > > for validation. It works fine in prototype 1.5, > > but an upgrade to 1.6 has broken it and I have not been able to work > > out why. > > > 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(); > > } > > } > > > When the JSON received looks like {"errors":[{"anError": > > "aMessage"}]}, errors.size() works which means the errors are > > displayed as they are supposed to, but when the json received looks > > like {"errors":[]}, then errors.size() falls over as errors is an > > undefined object. > > The problem is that iterating (via `each`) over a hash created from an > array yields bunch of prototype's `Array.prototype` extras. Wouldn't > `var errors = json.errors;` be enough? You can keep errors in a format > of `{ name: '...', message: '...' }`: > > onFormSuccess : function(transport) { > var json = transport.responseText.evalJSON(true); > var errors = json.errors; > > if (errors.length) { > this.form.down('.error').show(); > errors.each(function(error) { > this.showError(error.name, error.message); > }, this); > } > else { > this.form.submit(); > } > > } > > [...] > > > Regards, > > laf. > > -- > 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 -~--~~~~--~~--~--~---
[Proto-Scripty] Re: JSON and $H() to get size of returned JSON data
On Nov 18, 6:48 pm, laf <[EMAIL PROTECTED]> wrote: > So I have this function. It toggles error messages after an AJAX call > for validation. It works fine in prototype 1.5, > but an upgrade to 1.6 has broken it and I have not been able to work > out why. > > 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(); > } > } > > When the JSON received looks like {"errors":[{"anError": > "aMessage"}]}, errors.size() works which means the errors are > displayed as they are supposed to, but when the json received looks > like {"errors":[]}, then errors.size() falls over as errors is an > undefined object. The problem is that iterating (via `each`) over a hash created from an array yields bunch of prototype's `Array.prototype` extras. Wouldn't `var errors = json.errors;` be enough? You can keep errors in a format of `{ name: '...', message: '...' }`: onFormSuccess : function(transport) { var json = transport.responseText.evalJSON(true); var errors = json.errors; if (errors.length) { this.form.down('.error').show(); errors.each(function(error) { this.showError(error.name, error.message); }, this); } else { this.form.submit(); } } [...] > Regards, > laf. -- 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 -~--~~~~--~~--~--~---