Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #58 from PressAssociation/destroy
Browse files Browse the repository at this point in the history
Tidy up the validity state on $destroy.
  • Loading branch information
iambrosi committed Aug 26, 2015
2 parents 69529e8 + d762c13 commit 324efc2
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

scope.widgetId = null;

var sessionTimeout;
var removeCreationListener = scope.$watch('key', function (key) {
if (!key) {
return;
Expand All @@ -51,7 +52,7 @@
});

// captcha session lasts 2 mins after set.
$timeout(function (){
sessionTimeout = $timeout(function (){
if(ctrl){
ctrl.$setValidity('recaptcha',false);
}
Expand All @@ -75,14 +76,27 @@
scope.widgetId = widgetId;
scope.onCreate({widgetId: widgetId});

scope.$on('$destroy', cleanup);
scope.$on('$destroy', destroy);

});

// Remove this listener to avoid creating the widget more than once.
removeCreationListener();
});

function destroy() {
if (ctrl) {
// reset the validity of the form if we were removed
ctrl.$setValidity('recaptcha', null);
}
if (sessionTimeout) {
// don't trigger the session timeout if we are no longer active
$timeout.cancel(sessionTimeout);
sessionTimeout = null;
}
cleanup();
}

function cleanup(){
// removes elements reCaptcha added.
angular.element($document[0].querySelectorAll('.pls-container')).parent().remove();
Expand Down

0 comments on commit 324efc2

Please sign in to comment.