Skip to content

Commit

Permalink
Fix for #4 - subscribeGo failing when return key pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroHibler committed Jul 3, 2014
1 parent a714b79 commit 2963bd0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ api.call( 'campaigns', 'template-content', { cid: '/* CAMPAIGN ID */' }, functio

## Changelog

### v0.3.0
* Enable submit with return key

### v0.2.0
* On client, MailChimp.call() now reads API Key from session variable 'MailChimpOptions.apiKey' as well

Expand Down
32 changes: 21 additions & 11 deletions lib/client/views/subscribe/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
var subscribeTitle,
subscribeEmail,
subscribeButton,
subscribeMessage = 'Get on the mailing list:',
subscribeInvalidEmail = 'Invalid email address :(',
subscribeSubscribing = 'Subscribing...',
subscribeSuccess = 'Check your inbox! :)',
subscribeMessage = 'Get on the mailing list:',
subscribeInvalidEmail = 'Invalid email address :(',
subscribeSubscribing = 'Subscribing...',
subscribeSuccess = 'Check your inbox! :)',

showMessage = function ( message ) {
if ( subscribeTitle ) {
subscribeTitle.innerHTML = message;
}
},

isValidEmailAddress = function ( emailAddress ) {
// http://stackoverflow.com/a/46181/11236
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test( emailAddress );
},

validateEmailAddress = function ( updateMessage ) {
if ( subscribeEmail.value !== '' && isValidEmailAddress( subscribeEmail.value ) ) {
subscribeButton.disabled = false;
Expand All @@ -30,6 +33,7 @@ var subscribeTitle,
}
}
},

mailChimpListSubscribe = function ( email, list_id ) {
var mailChimp = new MailChimp(/* apiKey, options */);

Expand All @@ -40,6 +44,7 @@ var subscribeTitle,
email: email
}
},

function ( error, result ) {
if ( error ) {
switch ( error.error ) {
Expand All @@ -60,24 +65,26 @@ var subscribeTitle,
console.log( JSON.stringify( result ) );
showMessage( subscribeSuccess );
}

subscribeEmail.disabled = false;
validateEmailAddress( false );
}
);
},

subscribeGo = function ( eventBubbling ) {
subscribeEmail.disabled = true;
subscribeButton.disabled = true;
subscribeEmail.disabled = true;
subscribeButton.disabled = true;
showMessage( subscribeSubscribing );
mailChimpListSubscribe( subscribeEmail.value );
// Prevent Event Bubbling
return eventBubbling;
};

Template.MailChimpListSubscribe.rendered = function () {
subscribeTitle = this.find( '.message' );
subscribeEmail = this.find( '.email' );
subscribeButton = this.find( '.subscribe' );
subscribeTitle = this.find( '.message' );
subscribeEmail = this.find( '.email' );
subscribeButton = this.find( '.subscribe' );
subscribeButton.disabled = ( subscribeEmail.value === '' );
};

Expand All @@ -93,18 +100,20 @@ Template.MailChimpListSubscribe.events({
validateEmailAddress( true );
}, 0);
},

'keyup .email': function ( e ) {
var key = e.which || e.keyCode || e.charCode;
if ( key === 8 || // [Backspace]
key === 46 ) { // [Delete]
setTimeout(function( key ) {
setTimeout(function() {
validateEmailAddress( true );
}, 0);
}
},

'keypress .email': function ( e ) {
var key = e.which || e.keyCode || e.charCode;
setTimeout(function( key ) {
setTimeout(function() {
validateEmailAddress( true );
if ( isValidEmailAddress( subscribeEmail.value ) ) {
if ( key === 13 ) { // [Return]
Expand All @@ -113,6 +122,7 @@ Template.MailChimpListSubscribe.events({
}
}, 0);
},

'click .subscribe': function ( e ) {
validateEmailAddress( true );
if ( isValidEmailAddress( subscribeEmail.value ) ) {
Expand Down
2 changes: 1 addition & 1 deletion smart.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description" : "A Meteor wrapper for the MailChimp API",
"homepage" : "https://github.com/MiroHibler/meteor-mailchimp",
"author" : "Miroslav Hibler (http://miro.hibler.me)",
"version" : "0.2.0",
"version" : "0.3.0",
"git" : "https://github.com/MiroHibler/meteor-mailchimp.git",
"packages" : {}
}

0 comments on commit 2963bd0

Please sign in to comment.