From 5168f360535dcd871294011906edac230934a5b7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 21 Dec 2011 16:24:17 +0530 Subject: [PATCH] move api discovery into its own function --- source/Ox.UI/js/Core/Ox.Api.js | 75 ++++++++++++++++++++++++++++++++++ source/Ox.UI/js/Core/Ox.App.js | 54 ++---------------------- 2 files changed, 79 insertions(+), 50 deletions(-) create mode 100644 source/Ox.UI/js/Core/Ox.Api.js diff --git a/source/Ox.UI/js/Core/Ox.Api.js b/source/Ox.UI/js/Core/Ox.Api.js new file mode 100644 index 00000000..2f527dbd --- /dev/null +++ b/source/Ox.UI/js/Core/Ox.Api.js @@ -0,0 +1,75 @@ +// vim: et:ts=4:sw=4:sts=4:ft=javascript +'use strict'; +/*@ +Ox.Api bin 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; +}; diff --git a/source/Ox.UI/js/Core/Ox.App.js b/source/Ox.UI/js/Core/Ox.App.js index 2d6a4ee8..5766a1df 100644 --- a/source/Ox.UI/js/Core/Ox.App.js +++ b/source/Ox.UI/js/Core/Ox.App.js @@ -20,63 +20,17 @@ Ox.App = function(options) { options: Ox.extend({ timeout: 60000, type: 'POST', - url: '/api/', + url: '/api/' }, options || {}), time: new Date() }, that = Ox.Element({}, Ox.extend({}, self)); - that.api = { - 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, + that.api = Ox.Api({ type: self.options.type, + timeout: self.options.timeout, 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.api(function(result) { - Ox.forEach(result.data.actions, function(val, key) { - that.api[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}: {})); - }; - }); + }, function() { that.api.init(getUserData(), function(result) { //Ox.UI.hideLoadingScreen(); that.triggerEvent({