mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Allow redirects to be canceled by Promise.then() after AJAX call
This commit is contained in:
parent
2c5b3e072f
commit
f09cce5d1a
1 changed files with 30 additions and 6 deletions
|
|
@ -262,14 +262,37 @@ Rhymix.isSameHost = function(url) {
|
|||
* Redirect to a URL, but reload instead if the target is the same as the current page
|
||||
*
|
||||
* @param string url
|
||||
* @param int delay
|
||||
* @return void
|
||||
*/
|
||||
Rhymix.redirectToUrl = function(url) {
|
||||
if (this.isCurrentUrl(url)) {
|
||||
window.location.href = url;
|
||||
window.location.reload();
|
||||
Rhymix.redirectToUrl = function(url, delay) {
|
||||
const callback = function() {
|
||||
if (Rhymix.isCurrentUrl(url)) {
|
||||
window.location.href = url;
|
||||
window.location.reload();
|
||||
} else {
|
||||
window.location.href = url;
|
||||
}
|
||||
};
|
||||
if (delay) {
|
||||
this.pendingRedirect = setTimeout(callback, delay);
|
||||
} else {
|
||||
window.location.href = url;
|
||||
callback();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Cancel any pending redirect
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
Rhymix.cancelPendingRedirect = function() {
|
||||
if (this.pendingRedirect) {
|
||||
clearTimeout(this.pendingRedirect);
|
||||
this.pendingRedirect = null;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -481,8 +504,9 @@ Rhymix.ajax = function(action, params, callback_success, callback_error) {
|
|||
}
|
||||
|
||||
// If the response contains a redirect URL, follow the redirect.
|
||||
// This can be canceled by Rhymix.cancelPendingRedirect() within 100 milliseconds.
|
||||
if (data.redirect_url) {
|
||||
Rhymix.redirectToUrl(data.redirect_url.replace(/&/g, '&'));
|
||||
Rhymix.redirectToUrl(data.redirect_url.replace(/&/g, '&'), 100);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue