Last active
August 29, 2015 13:56
-
-
Save nitishn/9126089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setupSweepsMethods: function() { | |
var that = this; | |
// autofill button does a couple of things | |
// 1. Login to facebook if they already aren't and grab permissions for email and birthday | |
// 2. Extract as much data as we can for the sweepstakes form | |
// 3. Populate the sweepstakes with new data | |
$('.autofill_button').click( function(event) { | |
event.preventDefault(); | |
var defferedObject = new $.Deferred(); | |
// kick off the dance party by logging into Facebook | |
that.facebookLogin(defferedObject) | |
// once Facebook returns data, parse it out and | |
defferedObject.done(function(){ | |
//console.log(that.app_data.facebook_data); | |
that.populateSweepstakes(); | |
}); | |
}); | |
}, | |
populateSweepstakes: function() { | |
var that = this; | |
// check if we even have facebook data | |
if( that.app_data.facebook_data ) { | |
// grab first name | |
var first_name = that.app_data.facebook_data.first_name; | |
// grab last name | |
var last_name = that.app_data.facebook_data.last_name; | |
// grab email | |
var email = that.app_data.facebook_data.email; | |
// grab birthday | |
var birthdate = that.app_data.facebook_data.birthday; | |
// insert data back into sweepstakes | |
$('[name="entry[name][first_name]"]').val( first_name ); | |
$('[name="entry[name][last_name]"]').val( last_name ); | |
// check for FB proxy email address which we are not allowed to use | |
var emailCheck = email.split('@'); | |
if ( emailCheck[1] != 'proxymail.facebook.com' ) { | |
$('.engagements_field_email').find('input').val( email ); | |
} | |
// split up birthday and inject it into sweepstakes | |
birthdate = birthdate.split('/'); | |
$('[name="entry[birthday][year]"]').val( birthdate[2] ); | |
$('[name="entry[birthday][month]"]').val( birthdate[1].replace(/^0+/, '') ); | |
$('[name="entry[birthday][day]"]').val( birthdate[0].replace(/^0+/, '') ); | |
} | |
}, | |
facebookLogin: function(defferedObject) { | |
var that = this; | |
// login to facebook | |
FB.login(function(response) { | |
if (response.authResponse) { | |
// request information from FB | |
FB.api('/me', function(response) { | |
that.app_data.facebook_data = response; | |
// FB finished call and so resolve it | |
defferedObject.resolve(); | |
}); | |
} else { | |
// something went wrong | |
console.warn( 'Failed to authenticate' ); | |
} | |
}, { scope: that.app_data.request_perms } ); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment