Created
February 19, 2014 00:03
-
-
Save muratcorlu/9083317 to your computer and use it in GitHub Desktop.
Sayfada yarım kalmış bir form varken sayfadan çıkılmak istendiğinde uyarı vermeye yarayan AngularJS directive'i
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
/** | |
* @ngdoc directive | |
* @name safe.directive:incomplete-confirmation | |
* | |
* @element ANY | |
* | |
* @description | |
* | |
* `name` attribute'u olan bütün form elementlerinde çalışarak | |
* üzerinde değişiklik yapılmış ancak kaydedilmemiş bir form olduğunda | |
* sayfa yenilenmek veya başka bir sayfaya geçilmek istenirse | |
* kullanıcıya devam ettiği takdirde forma girilen değerleri kaybedeceği | |
* bilgisi ve vazgeçme şansı verilir. | |
* | |
* | |
*/ | |
angular.module('sahibinden.form', []) | |
.directive('incompleteConfirmation', function ($window) { | |
'use strict'; | |
return { | |
restrict: 'A', | |
require: '?img', | |
link: function (scope, element, attrs) { | |
if (attrs.name) { | |
var confirmationMessage = 'Kaydedilmemiş değişiklikleriniz var. Buna rağmen sayfadan çıkmak istediğinize emin misiniz?'; | |
$window.addEventListener('beforeunload', function (e) { | |
if (scope[attrs.name].$dirty) { | |
(e || window.event).returnValue = confirmationMessage; //Gecko + IE | |
return confirmationMessage; //Webkit, Safari, Chrome etc. | |
} | |
}); | |
scope.$on('$locationChangeStart', function (event) { | |
if (scope[attrs.name].$dirty) { | |
if (!confirm(confirmationMessage)) { | |
event.preventDefault(); | |
} | |
} | |
}); | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment