'use strict'; /*@ Ox.API Remote API controller options Options object timeout request timeout url request url callback called once api discover is done ([options, ] callback) -> API controller api Remote API discovery (calls the API's `api` method) (callback) -> Request id callback Callback functions .* Remote API method call ([data, [age, ]]callback) -> Request id data Request data age Max-age in ms (0: not from cache, -1: from cache) callback Callback function cancel Cancels a request (id) -> undefined id Request id @*/ Ox.API = function(options, callback) { var self = { options: Ox.extend({ timeout: 60000, type: 'POST', url: '/api/' }, options || {}), time: new Date() }, that = { api: function(callback) { return Ox.Request.send({ url: self.options.url, data: {action: 'api'}, 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; Ox.forEach(arguments, function(argument) { var type = Ox.typeOf(argument); if (type == 'object') { data = argument; } else if (type == 'number') { age = argument; } else if (type == 'function') { callback = argument; } }); return Ox.Request.send(Ox.extend({ age: age, callback: callback, data: { action: key, data: JSON.stringify(data) }, url: self.options.url }, !val.cache ? {age: 0} : {})); }; }); callback && callback(that); }); return that; };