From f8da7fa1fb35d08f08271cb0adf2d60f95d176a5 Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 28 Jul 2013 01:07:42 +0200 Subject: [PATCH] refactor request handling --- source/Ox.UI/js/Core/Request.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/source/Ox.UI/js/Core/Request.js b/source/Ox.UI/js/Core/Request.js index 69374641..b7c12174 100644 --- a/source/Ox.UI/js/Core/Request.js +++ b/source/Ox.UI/js/Core/Request.js @@ -129,22 +129,21 @@ Ox.Request = (function() { options.age == -1 || options.age > +new Date() - cache[req].time )) { - var data = cache[req].data; setTimeout(function() { - callback && callback(data); + callback(cache[req].data, true); }, 0); } else { pending[options.id] = true; $.ajax({ - beforeSend: function (request) { + beforeSend: function(request) { var csrftoken = Ox.Cookies('csrftoken'); if (csrftoken) { - request.setRequestHeader("X-CSRFToken", csrftoken); + request.setRequestHeader('X-CSRFToken', csrftoken); } }, complete: complete, data: options.data, - //dataType: 'json', + // dataType: 'json', timeout: options.timeout, type: options.type, url: options.url @@ -155,14 +154,18 @@ Ox.Request = (function() { }); } - function callback(data) { + function callback(data, success) { if (requests[options.id]) { delete requests[options.id]; - options.callback && options.callback(data); $element && $element.triggerEvent('request', { requests: Ox.len(requests) }); } + if (success) { + options.callback && options.callback(data); + } else { + $element && $element.triggerEvent('error', data); + } } function complete(request) { @@ -193,9 +196,9 @@ Ox.Request = (function() { data: data, time: Ox.getTime() }; - callback(data); + callback(data, true); } else { - $element && $element.triggerEvent('error', data); + callback(data, false); } pending[options.id] = false; }