angular.module("umbraco.directives")
    .directive('umbGenerateAlias', function ($timeout, entityResource) {
        return {
            restrict: 'E',
            templateUrl: 'views/components/umb-generate-alias.html',
            replace: true,
            scope: {
                alias: '=',
                aliasFrom: '=',
                enableLock: '=?'
            },
            link: function (scope, element, attrs, ctrl) {

                var unbindWatcher = function(){};
                var bindWatcher = true;
                var generateAliasTimeout = "";

                scope.locked = true;
                scope.placeholderText = "Enter alias...";

                function generateAlias(value) {

                  if (generateAliasTimeout) {
                    $timeout.cancel(generateAliasTimeout);
                  }

                  if( value !== undefined && value !== "") {

                    scope.alias = "Generating Alias...";

                    generateAliasTimeout = $timeout(function () {
                        entityResource.getSafeAlias(value, true).then(function (safeAlias) {
                        scope.alias = safeAlias.alias;
                      });
                    }, 500);

                  } else {
                    scope.alias = "";
                    scope.placeholderText = "Enter alias...";
                  }

                }

                // if alias gets unlocked - stop watching alias
                scope.$watch('locked', function(newValue, oldValue){
                    if(newValue === false) {
                        unbindWatcher();
                        bindWatcher = false;
                    }
                });

                // validate custom entered alias
                scope.$watch('alias', function(newValue, oldValue){

                  if(scope.alias === "" && bindWatcher === true || scope.alias === null && bindWatcher === true) {
                    // add watcher
                    unbindWatcher = scope.$watch('aliasFrom', function(newValue, oldValue) {
                      if (newValue !== undefined && newValue !== null) {
                        generateAlias(newValue);
                      }
                    });
                  }

                });

            }
        };
    });

@@ -456,36 +456,26 @@
 var 
-unbindWatcher = function()%7B%7D
+eventBindings = %5B%5D
 ;%0A  
@@ -558,16 +558,57 @@
 ut = %22%22;
+%0A                var updateAlias = false;
 %0A%0A      
@@ -929,16 +929,34 @@
 e !== %22%22
+ && value !== null
 ) %7B%0A%0A   
@@ -1072,24 +1072,67 @@
 nction () %7B%0A
+                       updateAlias = true;%0A
             
@@ -1208,24 +1208,75 @@
 afeAlias) %7B%0A
+                           if(updateAlias) %7B%0A      
             
@@ -1318,16 +1318,45 @@
 .alias;%0A
+                           %7D%0A
         
@@ -1430,16 +1430,56 @@
  else %7B%0A
+                    updateAlias = true;%0A
         
@@ -1682,32 +1682,51 @@
                 
+eventBindings.push(
 scope.$watch('lo
@@ -1811,50 +1811,8 @@
 ) %7B%0A
-                        unbindWatcher();%0A 
     
@@ -1883,32 +1883,33 @@
               %7D)
+)
 ;%0A%0A             
@@ -1956,24 +1956,43 @@
             
+eventBindings.push(
 scope.$watch
@@ -2206,24 +2206,27 @@
     
-unbindWatcher = 
+eventBindings.push(
 scop
@@ -2304,59 +2304,29 @@
     
+ 
 if
- (newValue !== undefined && newValue !== null
+(bindWatcher
 ) %7B%0A
+  
     
@@ -2384,32 +2384,33 @@
                 
+ 
 %7D%0A              
@@ -2409,32 +2409,33 @@
               %7D)
+)
 ;%0A              
@@ -2434,32 +2434,270 @@
             %7D%0A%0A 
+              %7D));%0A%0A               // clean up%0A               scope.$on('$destroy', function()%7B%0A                 // unbind watchers%0A                 for(var e in eventBindings) %7B%0A                   eventBindings%5Be%5D();%0A                  %7D%0A
                %7D

clear alias when name is empty