Created
January 7, 2016 11:49
-
-
Save jrencz/8e6cb494ebf4d8138370 to your computer and use it in GitHub Desktop.
Quick sketch of Angular1 attribute directive that calls a function after a promise returned by ngClick handler is resolved or rejected
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
(function () { | |
'use strict'; | |
angular | |
.module('finally', []) | |
.directive('finally', function ( | |
$parse | |
) { | |
return { | |
restrict: 'A', | |
priority: 1, | |
terminal: true, | |
link(scope, element, attr) { | |
const clickHandler = angular.isFunction(attr.ngClick) ? | |
attr.ngClick : | |
$parse(attr.ngClick); | |
element.on('click', $event => { | |
scope.$apply(() => { | |
const promise = clickHandler(scope, {$event}); | |
if (promise && 'finally' in promise) { | |
promise.finally(() => { | |
scope.$eval(attr.finally); | |
}); | |
} | |
}); | |
}); | |
}, | |
}; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment