Fixed page exit confirmation

This commit is contained in:
Marcin Kurczewski 2014-09-16 17:29:11 +02:00
parent ac312ccb49
commit d72c6c5d6e
2 changed files with 21 additions and 0 deletions

View file

@ -3,6 +3,7 @@ var App = App || {};
App.Router = function(pathJs, _, jQuery, util, appState, presenterManager) {
var root = '#/';
var previousLocation = window.location.href;
injectRoutes();
@ -57,6 +58,18 @@ App.Router = function(pathJs, _, jQuery, util, appState, presenterManager) {
{previousRoute: pathJs.routes.previous});
presenterManager.switchContentPresenter( presenterName, finalParams);
}).enter(function(e) {
if (util.isExitConfirmationEnabled()) {
if (window.location.href !== previousLocation) {
if (!window.confirm('Are you sure you want to leave this page? Data will be lost.')) {
window.location.href = previousLocation;
return false;
} else {
util.disableExitConfirmation();
}
}
}
previousLocation = window.location.href;
});
}

View file

@ -3,17 +3,24 @@ var App = App || {};
App.Util = function(_, jQuery, promise) {
var templateCache = {};
var exitConfirmationEnabled = false;
function enableExitConfirmation() {
exitConfirmationEnabled = true;
jQuery(window).bind('beforeunload', function(e) {
return 'There are unsaved changes.';
});
}
function disableExitConfirmation() {
exitConfirmationEnabled = false;
jQuery(window).unbind('beforeunload');
}
function isExitConfirmationEnabled() {
return exitConfirmationEnabled;
}
function parseComplexRouteArgs(args) {
var result = {};
args = (args || '').split(/;/);
@ -148,6 +155,7 @@ App.Util = function(_, jQuery, promise) {
formatRelativeTime: formatRelativeTime,
enableExitConfirmation: enableExitConfirmation,
disableExitConfirmation: disableExitConfirmation,
isExitConfirmationEnabled: isExitConfirmationEnabled,
};
};