2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
2012-05-21 10:38:18 +00:00
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
2012-07-04 11:29:18 +00:00
|
|
|
Ox.Request <o> Basic request controller
|
|
|
|
# FIXME: options is not a property, just documenting defaults
|
|
|
|
# options <o> Options object
|
|
|
|
# timeout <n|60000> request timeout
|
|
|
|
# type <s|"POST"> request type, possible values POST, GET, PUT, DELETE
|
|
|
|
# url <s> request url
|
2011-05-16 08:24:46 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-11-30 14:49:11 +00:00
|
|
|
Ox.Request = (function() {
|
2011-04-22 22:03:10 +00:00
|
|
|
|
|
|
|
var cache = {},
|
|
|
|
pending = {},
|
|
|
|
requests = {},
|
|
|
|
self = {
|
2011-11-30 14:49:11 +00:00
|
|
|
options: {
|
2011-04-22 22:03:10 +00:00
|
|
|
timeout: 60000,
|
|
|
|
type: 'POST',
|
|
|
|
url: '/api/'
|
2011-11-30 14:49:11 +00:00
|
|
|
}
|
2013-07-15 09:10:08 +00:00
|
|
|
},
|
|
|
|
$element;
|
2011-04-22 22:03:10 +00:00
|
|
|
|
|
|
|
return {
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
cancel <f> cancel pending requests
|
|
|
|
() -> <u> cancel all requests
|
2011-09-05 23:34:29 +00:00
|
|
|
(fn) -> <u> cancel all requests where function returns true
|
|
|
|
(id) -> <u> cancel request by id
|
2011-05-16 08:24:46 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
cancel: function() {
|
|
|
|
if (arguments.length == 0) {
|
|
|
|
// cancel all requests
|
|
|
|
requests = {};
|
|
|
|
} else if (Ox.isFunction(arguments[0])) {
|
|
|
|
// cancel with function
|
|
|
|
Ox.forEach(requests, function(req, id) {
|
|
|
|
if (arguments[0](req)) {
|
|
|
|
delete requests[id];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// cancel by id
|
|
|
|
delete requests[arguments[0]];
|
|
|
|
}
|
2012-01-27 14:29:11 +00:00
|
|
|
$element && $element.triggerEvent('request', {
|
|
|
|
requests: Ox.len(requests)
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
},
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
clearCache <f> clear cached results
|
2011-09-05 23:34:29 +00:00
|
|
|
() -> <u> ...
|
2011-05-16 08:24:46 +00:00
|
|
|
@*/
|
2011-09-19 13:34:17 +00:00
|
|
|
clearCache: function(query) {
|
|
|
|
if (!query) {
|
|
|
|
cache = {};
|
|
|
|
} else {
|
|
|
|
cache = Ox.filter(cache, function(val, key) {
|
|
|
|
return key.indexOf(query) == -1;
|
|
|
|
});
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
},
|
2012-01-27 14:29:11 +00:00
|
|
|
|
2012-01-14 10:48:26 +00:00
|
|
|
/*@
|
2013-07-15 09:10:08 +00:00
|
|
|
bindEvent <f> Bind event
|
2012-01-14 10:48:26 +00:00
|
|
|
@*/
|
2012-01-19 13:40:35 +00:00
|
|
|
bindEvent: function() {
|
2012-01-27 14:29:11 +00:00
|
|
|
if (!$element) {
|
2012-01-19 13:40:35 +00:00
|
|
|
$element = Ox.Element();
|
|
|
|
}
|
|
|
|
$element.bindEvent.apply(this, arguments);
|
|
|
|
},
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
options <f> get/set options
|
|
|
|
() -> <o> get options
|
|
|
|
(options) -> <o> set options
|
|
|
|
options <o> Options Object
|
|
|
|
@*/
|
2011-11-30 14:49:11 +00:00
|
|
|
options: function() {
|
|
|
|
return Ox.getset(self.options, arguments, function() {}, this);
|
2011-04-22 22:03:10 +00:00
|
|
|
},
|
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
requests <f> pending requests
|
|
|
|
() -> <n> returns number of requests
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
requests: function() {
|
|
|
|
return Ox.len(requests);
|
|
|
|
},
|
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
send <f> send request
|
|
|
|
(options) -> <n> returns request id
|
|
|
|
options <o> Options Object
|
|
|
|
age <n|-1> cache age
|
|
|
|
id <n|Ox.uid()> request id
|
|
|
|
timeout <n|self.options.timeout> overwrite default timeout
|
|
|
|
type <n|self.options.timeout> overwrite default type
|
|
|
|
url <n|self.options.timeout> overwrite default url
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
send: function(options) {
|
|
|
|
|
2011-09-17 18:36:09 +00:00
|
|
|
var options = Ox.extend({
|
2011-04-22 22:03:10 +00:00
|
|
|
age: -1,
|
|
|
|
callback: null,
|
|
|
|
id: Ox.uid(),
|
|
|
|
timeout: self.options.timeout,
|
|
|
|
type: self.options.type,
|
|
|
|
url: self.options.url
|
|
|
|
}, options),
|
|
|
|
req = JSON.stringify({
|
|
|
|
url: options.url,
|
|
|
|
data: options.data
|
|
|
|
});
|
|
|
|
|
|
|
|
if (pending[options.id]) {
|
|
|
|
setTimeout(function() {
|
|
|
|
Ox.Request.send(options);
|
|
|
|
}, 0);
|
|
|
|
} else {
|
|
|
|
requests[options.id] = {
|
|
|
|
url: options.url,
|
|
|
|
data: options.data
|
|
|
|
};
|
2011-10-07 19:29:04 +00:00
|
|
|
if (cache[req] && (
|
|
|
|
options.age == -1
|
|
|
|
|| options.age > +new Date() - cache[req].time
|
|
|
|
)) {
|
2011-04-22 22:03:10 +00:00
|
|
|
setTimeout(function() {
|
2013-07-27 23:07:42 +00:00
|
|
|
callback(cache[req].data, true);
|
2011-04-22 22:03:10 +00:00
|
|
|
}, 0);
|
|
|
|
} else {
|
|
|
|
pending[options.id] = true;
|
|
|
|
$.ajax({
|
2013-07-27 23:07:42 +00:00
|
|
|
beforeSend: function(request) {
|
2013-03-16 11:54:43 +00:00
|
|
|
var csrftoken = Ox.Cookies('csrftoken');
|
|
|
|
if (csrftoken) {
|
2013-07-27 23:07:42 +00:00
|
|
|
request.setRequestHeader('X-CSRFToken', csrftoken);
|
2013-03-16 11:54:43 +00:00
|
|
|
}
|
|
|
|
},
|
2011-10-27 18:50:23 +00:00
|
|
|
complete: complete,
|
2011-04-22 22:03:10 +00:00
|
|
|
data: options.data,
|
2013-07-27 23:07:42 +00:00
|
|
|
// dataType: 'json',
|
2011-04-22 22:03:10 +00:00
|
|
|
timeout: options.timeout,
|
|
|
|
type: options.type,
|
|
|
|
url: options.url
|
|
|
|
});
|
|
|
|
}
|
2012-01-27 14:29:11 +00:00
|
|
|
$element && $element.triggerEvent('request', {
|
|
|
|
requests: Ox.len(requests)
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
2013-07-27 23:07:42 +00:00
|
|
|
function callback(data, success) {
|
2011-10-07 19:29:04 +00:00
|
|
|
if (requests[options.id]) {
|
|
|
|
delete requests[options.id];
|
2012-01-27 14:29:11 +00:00
|
|
|
$element && $element.triggerEvent('request', {
|
|
|
|
requests: Ox.len(requests)
|
|
|
|
});
|
2013-07-28 11:53:21 +00:00
|
|
|
if (success) {
|
|
|
|
options.callback && options.callback(data);
|
|
|
|
} else {
|
|
|
|
$element && $element.triggerEvent('error', data);
|
|
|
|
}
|
2013-07-27 23:07:42 +00:00
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 18:50:23 +00:00
|
|
|
function complete(request) {
|
2012-01-14 10:48:26 +00:00
|
|
|
var $dialog, data;
|
2011-10-27 18:50:23 +00:00
|
|
|
try {
|
|
|
|
data = JSON.parse(request.responseText);
|
2012-05-25 09:44:43 +00:00
|
|
|
} catch (error) {
|
2011-04-22 22:03:10 +00:00
|
|
|
try {
|
2011-10-27 18:50:23 +00:00
|
|
|
data = {
|
|
|
|
status: {
|
|
|
|
code: request.status,
|
|
|
|
text: request.statusText
|
|
|
|
}
|
|
|
|
};
|
2012-05-25 09:44:43 +00:00
|
|
|
} catch (error) {
|
2011-10-27 18:50:23 +00:00
|
|
|
data = {
|
|
|
|
status: {
|
|
|
|
code: '500',
|
|
|
|
text: 'Unknown Error'
|
|
|
|
}
|
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-04 11:05:25 +00:00
|
|
|
if (Ox.contains([200, 404, 409], data.status.code)) {
|
2011-10-31 16:02:00 +00:00
|
|
|
// we have to include not found and conflict
|
|
|
|
// so that handlers can handle these cases
|
2011-10-27 18:50:23 +00:00
|
|
|
cache[req] = {
|
|
|
|
data: data,
|
|
|
|
time: Ox.getTime()
|
|
|
|
};
|
2013-07-27 23:07:42 +00:00
|
|
|
callback(data, true);
|
2011-04-22 22:03:10 +00:00
|
|
|
} else {
|
2013-07-27 23:07:42 +00:00
|
|
|
callback(data, false);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
pending[options.id] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options.id;
|
|
|
|
|
2012-01-19 13:40:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*@
|
2012-01-27 14:29:11 +00:00
|
|
|
unbindEvent <f> Unbind event
|
2012-01-19 13:40:35 +00:00
|
|
|
@*/
|
|
|
|
unbindEvent: function() {
|
|
|
|
$element && $element.unbindEvent.apply(this, arguments);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-11-30 14:49:11 +00:00
|
|
|
}());
|