-
Notifications
You must be signed in to change notification settings - Fork 132
Update jquery.ajaxchimp.js to add support for error messages containing custom information #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,12 +30,13 @@ For e.g. 'http://blahblah.us1.list-manage.com/subscribe/post-json?u=5afsdhfuhdsi | |
|
|
||
| $.ajaxChimp = { | ||
| responses: { | ||
| 'We have sent you a confirmation email' : 0, | ||
| 'Please enter a value' : 1, | ||
| 'An email address must contain a single @' : 2, | ||
| 'The domain portion of the email address is invalid (the portion after the @: )' : 3, | ||
| 'The username portion of the email address is invalid (the portion before the @: )' : 4, | ||
| 'This email address looks fake or invalid. Please enter a real email address' : 5 | ||
| 0: 'We have sent you a confirmation email', | ||
| 1: 'Please enter a value', | ||
| 2: 'An email address must contain a single @', | ||
| 3: 'The domain portion of the email address is invalid', | ||
| 4: 'The username portion of the email address is invalid', | ||
| 5: 'This email address looks fake or invalid. Please enter a real email address', | ||
| 6: 'is already subscribed to list' | ||
| }, | ||
| translations: { | ||
| 'en': null | ||
|
|
@@ -93,15 +94,31 @@ For e.g. 'http://blahblah.us1.list-manage.com/subscribe/post-json?u=5afsdhfuhdsi | |
| } | ||
| } | ||
|
|
||
|
|
||
| // Translate and display message | ||
| var length = 0, msgnr; | ||
|
|
||
| if(Object.keys){ | ||
| length = Object.keys($.ajaxChimp.responses).length | ||
| } | ||
| else { | ||
| for (var key in $.ajaxChimp.responses){ | ||
| if ($.ajaxChimp.responses.hasOwnProperty(key)) { | ||
| length++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| while(length--) { | ||
| if (msg.indexOf($.ajaxChimp.responses[length])!==-1) { | ||
| msgnr = length; | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| settings.language !== 'en' | ||
| && $.ajaxChimp.responses[msg] !== undefined | ||
| && $.ajaxChimp.translations | ||
| && $.ajaxChimp.translations[settings.language] | ||
| && $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]] | ||
| settings.language !== 'en' && msgnr > -1 && $.ajaxChimp.translations && $.ajaxChimp.translations[settings.language] && $.ajaxChimp.translations[settings.language][msgnr] | ||
| ) { | ||
| msg = $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]]; | ||
| msg = $.ajaxChimp.translations[settings.language][msgnr]; | ||
| } | ||
| label.html(msg); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add |
||
|
|
||
|
|
@@ -123,19 +140,16 @@ For e.g. 'http://blahblah.us1.list-manage.com/subscribe/post-json?u=5afsdhfuhdsi | |
| success: successCallback, | ||
| dataType: 'jsonp', | ||
| error: function (resp, text) { | ||
| console.log('mailchimp ajax submit error: ' + text); | ||
| window.console.log('mailchimp ajax submit error: ' + text); | ||
| } | ||
| }); | ||
|
|
||
| // Translate and display submit message | ||
| var submitMsg = 'Submitting...'; | ||
| if( | ||
| settings.language !== 'en' | ||
| && $.ajaxChimp.translations | ||
| && $.ajaxChimp.translations[settings.language] | ||
| && $.ajaxChimp.translations[settings.language]['submit'] | ||
| settings.language !== 'en' && $.ajaxChimp.translations && $.ajaxChimp.translations[settings.language] && $.ajaxChimp.translations[settings.language].submit | ||
| ) { | ||
| submitMsg = $.ajaxChimp.translations[settings.language]['submit']; | ||
| submitMsg = $.ajaxChimp.translations[settings.language].submit; | ||
| } | ||
| label.html(submitMsg).show(2000); | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a similar fix for this locally, but I didn't change the responses structure.
My fix looks like:
This way we only do partial matches if the string doesn't exist in the hash map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I think the fix needs to be a lot better. Meaning, there are cases where the response is like:
{"result":"error","msg":"[email protected] is already subscribed to list My Newsletter. <a href=\"http:\/\/address.list-manage1.com\/subscribe\/send-email?u=uuu&id=ddd&e=xxx==\">Click here to update your profile.<\/a>"})I believe the best fix would be not to do partial matches (like I did also) but rather regular expressions to extract the information we need.
BTW, any luck on requesting Mailchimp to return these errors already translated from the server? Basically the translations exist and can be tweaked from the Mailchimp webpage. I still don't understand why they don't translate this before sending the response. Perhaps we could send a language param to get the response from them directly?
The reason why I'm saying this is because I might have customers that want to translate the same errors in different ways and that way I wouldn't have to change the translations per customer, and rather just tell them how to do on Mailchimp interface.