Skip to content

Instantly share code, notes, and snippets.

@muratcorlu
Created February 19, 2014 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muratcorlu/9083317 to your computer and use it in GitHub Desktop.
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
/**
* @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