Last active
February 14, 2019 08:01
-
-
Save patorash/ccfef86bb04c03f81ead161659554ccc to your computer and use it in GitHub Desktop.
knockout.jsとreCAPTCHAを連携させる
This file contains hidden or 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
ko.bindingHandlers.recaptcha = { | |
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { | |
var propWriters = allBindings()['_ko_property_writers']; | |
var value = valueAccessor(); | |
document.addEventListener('createCaptcha', function(event, theme) { | |
var site_key = document.querySelector('.g-recaptcha').dataset.sitekey | |
var callback = allBindings.get('recaptchaCallback') || function () { | |
if (!value) { | |
if (ko.isObservable(value)) { | |
value = true | |
} else { | |
propWriters.recaptcha(true) | |
} | |
} | |
}; | |
window.widgetId = grecaptcha.render('recaptcha', { | |
sitekey: site_key, | |
theme: theme, | |
callback: callback, | |
'expired-callback': function() { | |
grecaptcha.reset(widgetId); | |
if (ko.isObservable(value)) { | |
value = false | |
} else { | |
propWriters.recaptcha(false) | |
} | |
} | |
}) | |
}); | |
} | |
}; | |
ko.expressionRewriting._twoWayBindings['recaptcha'] = true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment