// vim: et:ts=4:sw=4:sts=4:ft=javascript 'use strict'; /*@ Ox.API bind remote api to object options Options object timeout request timeout url request url callback called once api discover is done @*/ Ox.API = function(options, callback) { var self = { options: Ox.extend({ timeout: 60000, type: 'POST', url: '/api/' }, options || {}), time: new Date() }, that = { api: function(callback) { 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 }); /*@ api bakcend API [action] all api requests available on backend cancel cancel a request options get or set options @*/ 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) { if (Ox.isObject(argument)) { data = argument; } else if (Ox.isNumber(argument)) { age = argument; } else if (Ox.isFunction(argument)) { 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(); }); return that; };