Skip to content

Instantly share code, notes, and snippets.

@klamping
Last active August 29, 2015 14:02
Show Gist options
  • Save klamping/caecc1409e3eee82b197 to your computer and use it in GitHub Desktop.
Save klamping/caecc1409e3eee82b197 to your computer and use it in GitHub Desktop.
ng-diaper
angular.directive('ngDiaper', function ($rootScope, $timeout, wipesSvc) {
return {
replace: true,
priority: 1000,
restrict: 'E',
// TODO for some reason, even though we're trying to isolate the scope, properties inside this directive keep leaking out
scope: true,
template: '<form name="diaper" ng-submit="changeDiaper()"><input type="submit" id="plumbing"></form>',
link: function (scope, el) {
scope.changeDiaper = function () {
var wipe = wipesSvc.grab();
wipe.clean(el);
// the fresh air on the skin always causes a reaction
scope.$emit('pee');
// grab a new wipe
// TODO check for error thrown from svc just in case wipes run out (shouldn't happen though)
wipe = wipesSvc.grab();
wipe.clean(el);
scope.diaper.$setPristine();
// start a timeout to immediately dirty the diaper after finished changing it
$timeout(function () {
scope.diaper.$setDirty();
}, 0);
};
// dirty the diaper after every $digest
$rootScope.$watch(function(){
scope.diaper.$setDirty();
});
}
};
});
@kentcdodds
Copy link

+1

@mpgilbertusa
Copy link

Kevin, I see that you're no longer writing the clean code that I recall from the past. That's a shame.

@klamping
Copy link
Author

klamping commented Jun 9, 2014

Michael, mind mentioning what 'code smells' you found with this?

💩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment