mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-09 20:12:14 +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
|
* Redirect to a URL, but reload instead if the target is the same as the current page
|
||||||
*
|
*
|
||||||
* @param string url
|
* @param string url
|
||||||
|
* @param int delay
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
Rhymix.redirectToUrl = function(url) {
|
Rhymix.redirectToUrl = function(url, delay) {
|
||||||
if (this.isCurrentUrl(url)) {
|
const callback = function() {
|
||||||
window.location.href = url;
|
if (Rhymix.isCurrentUrl(url)) {
|
||||||
window.location.reload();
|
window.location.href = url;
|
||||||
|
window.location.reload();
|
||||||
|
} else {
|
||||||
|
window.location.href = url;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (delay) {
|
||||||
|
this.pendingRedirect = setTimeout(callback, delay);
|
||||||
} else {
|
} 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.
|
// If the response contains a redirect URL, follow the redirect.
|
||||||
|
// This can be canceled by Rhymix.cancelPendingRedirect() within 100 milliseconds.
|
||||||
if (data.redirect_url) {
|
if (data.redirect_url) {
|
||||||
Rhymix.redirectToUrl(data.redirect_url.replace(/&/g, '&'));
|
Rhymix.redirectToUrl(data.redirect_url.replace(/&/g, '&'), 100);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue