Simplify aborting AJAX request

This commit is contained in:
Jakub Vrana 2012-02-29 14:31:35 -08:00
parent 83113cbe67
commit 99a75c3c44

View file

@ -191,7 +191,7 @@ function selectAddRow(field) {
* @uses ajaxRequest * @uses ajaxRequest
*/ */
function ajaxAbort() { function ajaxAbort() {
ajaxRequest.aborted = true; ajaxRequest.onreadystatechange = null;
if (ajaxRequest.abort) { if (ajaxRequest.abort) {
ajaxRequest.abort(); ajaxRequest.abort();
} }
@ -203,7 +203,6 @@ function ajaxAbort() {
* @param KeyboardEvent * @param KeyboardEvent
* @param [string] * @param [string]
* @return boolean * @return boolean
* @uses ajaxRequest
*/ */
function bodyKeydown(event, button) { function bodyKeydown(event, button) {
var target = event.target || event.srcElement; var target = event.target || event.srcElement;
@ -321,7 +320,6 @@ function replaceFavicon(href) {
} }
} }
var ajaxState = 0;
var ajaxRequest = {}; var ajaxRequest = {};
/** Safely load content to #content /** Safely load content to #content
@ -330,14 +328,13 @@ var ajaxRequest = {};
* @param [boolean] * @param [boolean]
* @param [boolean] * @param [boolean]
* @return XMLHttpRequest or false in case of an error * @return XMLHttpRequest or false in case of an error
* @uses ajaxState, ajaxRequest * @uses ajaxRequest
*/ */
function ajaxSend(url, data, popState, noscroll) { function ajaxSend(url, data, popState, noscroll) {
if (!history.pushState) { if (!history.pushState) {
return false; return false;
} }
ajaxAbort(); ajaxAbort();
var currentState = ++ajaxState;
onblur = function () { onblur = function () {
if (!originalFavicon) { if (!originalFavicon) {
originalFavicon = (document.getElementById('favicon') || {}).href; originalFavicon = (document.getElementById('favicon') || {}).href;
@ -346,7 +343,6 @@ function ajaxSend(url, data, popState, noscroll) {
}; };
document.body.className += ' loading'; document.body.className += ' loading';
ajaxRequest = ajax(url, function (request) { ajaxRequest = ajax(url, function (request) {
if (!request.aborted && currentState == ajaxState) {
var title = request.getResponseHeader('X-AJAX-Title'); var title = request.getResponseHeader('X-AJAX-Title');
if (title) { if (title) {
document.title = decodeURIComponent(title); document.title = decodeURIComponent(title);
@ -393,20 +389,19 @@ function ajaxSend(url, data, popState, noscroll) {
if (window.jush) { if (window.jush) {
jush.highlight_tag('code', 0); jush.highlight_tag('code', 0);
} }
}
}, data); }, data);
return ajaxRequest; return ajaxRequest;
} }
/** Revive page from history /** Revive page from history
* @param PopStateEvent|history * @param PopStateEvent|history
* @uses ajaxState * @uses ajaxRequest
*/ */
onpopstate = function (event) { onpopstate = function (event) {
if ((ajaxState || event.state) && !/#/.test(location.href)) { if ((ajaxRequest.send || event.state) && !/#/.test(location.href)) {
ajaxSend(location.href, (event.state && confirm(areYouSure) ? event.state : ''), 1); // 1 - disable pushState ajaxSend(location.href, (event.state && confirm(areYouSure) ? event.state : ''), 1); // 1 - disable pushState
} else { } else {
ajaxState++; ajaxRequest.send = true; // to enable AJAX for next call of this function
} }
}; };