Skip to content

Instantly share code, notes, and snippets.

@yosemsweet
Last active August 29, 2015 14:18
Show Gist options
  • Save yosemsweet/0aaaf016b227c8e4c39a to your computer and use it in GitHub Desktop.
Save yosemsweet/0aaaf016b227c8e4c39a to your computer and use it in GitHub Desktop.
Submit form from Rooster
window.jQuery(function() {
var $ = window.jQuery;
var submittable = $('[data-submittable="true"]');
submittable.find('[data-submit-state]:not([data-submit-state="unsubmitted"])').hide();
submittable.find('[data-submit-state="unsubmitted"]').show();
function watchForSubscriptionSuccess(submittable) {
_GT.active_context.on('change', function(changes) {
if(changes.success) {
submittable.find('[data-submit-state]:not([data-submit-state="submitted"])').hide();
submittable.find('[data-submit-state="submitted"]').show();
} else if (changes.hasOwnProperty('success')) {
submittable.find('[data-submit-state]:not([data-submit-state="error"])').hide();
submittable.find('[data-submit-state="error"]').show();
}
});
}
function submitEngagement(email) {
_GT.active_context.add({'email': email});
var host_url = new _GT.QueryString(window.location.search).get_value('url') || '*';
_GT.host_origin = _GT.getOrigin(host_url);
var host_wirepoint = new _GT.WirePoint(window.parent || window, {origin: _GT.host_origin});
host_wirepoint.post_message('gt_engagement_popup', host_wirepoint.origin());
watchForSubscriptionSuccess(submittable);
}
submittable.find('button').bind('click', function (e) {
var email = submittable.find('[data-submit-state]:visible input[type="email"]').val();
submitEngagement(email);
submittable.find('[data-submit-state]:not([data-submit-state="submitting"])').hide();
submittable.find('[data-submit-state="submitting"]').show();
});
});
window.jQuery && window.jQuery(function() {
var $ = window.jQuery;
_GT.on('engagement', function(popup) {
var email = _GT.active_context.properties.email;
jQuery.post('/subscribe',
{email: email},
function(data) {
if('ok' == data) {
_GT.active_context.add({'success':'true'});
}
}
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment