用戶可以編輯他們的個人資料信息。如果他們試圖在存在更改時離開頁面,則所需的功能應該是它們帶有確認框。當我使用Durandal的canDeactivate時,只有當我嘗試導航到另一個Durandal頁面時纔會觸發它。當我使用window.onbeforeunload它只是當我硬刷新或鍵入一個新的URL時觸發等。Durandal canDeactivate vs window.onbeforeunload可能的替代方案
是否有任何通用的解決方案(統一的外觀和感覺),可以捕獲這兩類事件,以便防止用戶立即導航離開頁面?
我的兩個方法都顯示如下:
迪朗達爾canDeactivate
canDeactivate: function() {
if ($("#saveButtonsBottom").css('visibility') === 'visible') {
var title = 'Warning';
var msg = 'Do you want to leave this page and lose all of your edits to this form?';
return app.showMessage(msg, title, ['Yes', 'No'])
.then(function (selectedOption) {
return selectedOption === 'Yes';
});
}
return false;
}
window.onbeforeunload
window.onbeforeunload = function() {
if ($("#saveButtonsBottom").css('visibility') === 'visible') {
var title = 'Warning';
var msg = 'Do you want to leave this page and lose all of your edits to this form?';
return app.showMessage(msg, title, ['Yes', 'No'])
.then(function (selectedOption) {
return selectedOption === 'Yes';
});
}
return true;
};