2011-12-21 10:54:17 +00:00
|
|
|
'use strict';
|
2012-05-21 10:38:18 +00:00
|
|
|
|
2011-12-21 10:54:17 +00:00
|
|
|
/*@
|
2012-07-04 11:15:32 +00:00
|
|
|
Ox.API <f> Remote API controller
|
|
|
|
options <o> Options object
|
|
|
|
timeout <n|60000> request timeout
|
|
|
|
url <s> request url
|
|
|
|
callback <f> called once api discover is done
|
|
|
|
([options, ] callback) -> <o> API controller
|
|
|
|
api <f> Remote API discovery (calls the API's `api` method)
|
|
|
|
(callback) -> <n> Request id
|
|
|
|
callback <f> Callback functions
|
|
|
|
.* <f> Remote API method call
|
|
|
|
([data, [age, ]]callback) -> <n> Request id
|
|
|
|
data <o> Request data
|
|
|
|
age <n|-1> Max-age in ms (0: not from cache, -1: from cache)
|
|
|
|
callback <f> Callback function
|
|
|
|
cancel <f> Cancels a request
|
|
|
|
(id) -> <u> undefined
|
|
|
|
id <n> Request id
|
2011-12-21 10:54:17 +00:00
|
|
|
@*/
|
|
|
|
|
2011-12-21 15:42:11 +00:00
|
|
|
Ox.API = function(options, callback) {
|
2011-12-21 10:54:17 +00:00
|
|
|
|
|
|
|
var self = {
|
|
|
|
options: Ox.extend({
|
|
|
|
timeout: 60000,
|
|
|
|
type: 'POST',
|
|
|
|
url: '/api/'
|
|
|
|
}, options || {}),
|
|
|
|
time: new Date()
|
|
|
|
},
|
|
|
|
that = {
|
|
|
|
api: function(callback) {
|
2012-07-04 11:15:32 +00:00
|
|
|
return Ox.Request.send({
|
2011-12-21 10:54:17 +00:00
|
|
|
url: self.options.url,
|
2011-12-30 09:33:01 +00:00
|
|
|
data: {action: 'api'},
|
2011-12-21 10:54:17 +00:00
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
},
|
|
|
|
cancel: function(id) {
|
|
|
|
Ox.Request.cancel(id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ajaxSetup({
|
|
|
|
timeout: self.options.timeout,
|
|
|
|
type: self.options.type,
|
|
|
|
url: self.options.url
|
|
|
|
});
|
|
|
|
|
|
|
|
that.api(function(result) {
|
|
|
|
Ox.forEach(result.data.actions, function(val, key) {
|
|
|
|
that[key] = function(/*data, age, callback*/) {
|
|
|
|
var data = {}, age = -1, callback = null;
|
2012-02-17 10:32:32 +00:00
|
|
|
Ox.forEach(arguments, function(argument) {
|
2012-05-25 17:46:02 +00:00
|
|
|
var type = Ox.typeOf(argument);
|
|
|
|
if (type == 'object') {
|
2011-12-21 10:54:17 +00:00
|
|
|
data = argument;
|
2012-05-25 17:46:02 +00:00
|
|
|
} else if (type == 'number') {
|
2011-12-21 10:54:17 +00:00
|
|
|
age = argument;
|
2012-05-25 17:46:02 +00:00
|
|
|
} else if (type == 'function') {
|
2011-12-21 10:54:17 +00:00
|
|
|
callback = argument;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return Ox.Request.send(Ox.extend({
|
|
|
|
age: age,
|
|
|
|
callback: callback,
|
|
|
|
data: {
|
|
|
|
action: key,
|
|
|
|
data: JSON.stringify(data)
|
|
|
|
},
|
|
|
|
url: self.options.url
|
2012-07-04 11:15:32 +00:00
|
|
|
}, !val.cache ? {age: 0} : {}));
|
2011-12-21 10:54:17 +00:00
|
|
|
};
|
|
|
|
});
|
2013-06-01 11:59:01 +00:00
|
|
|
callback && callback(that);
|
2011-12-21 10:54:17 +00:00
|
|
|
});
|
2012-07-04 11:15:32 +00:00
|
|
|
|
2011-12-21 10:54:17 +00:00
|
|
|
return that;
|
2012-07-04 11:15:32 +00:00
|
|
|
|
2011-12-21 10:54:17 +00:00
|
|
|
};
|