From a19578af35a642048af8553b2051da2dca8e4432 Mon Sep 17 00:00:00 2001 From: Rolux Date: Wed, 27 Jan 2010 18:00:00 +0530 Subject: [PATCH] request controller --- build/js/ox.js | 37 +++++ build/js/ox.ui.js | 395 ++++++++++++++++++++++++++++++++++---------- demos/test/app.html | 24 +++ 3 files changed, 369 insertions(+), 87 deletions(-) create mode 100644 demos/test/app.html diff --git a/build/js/ox.js b/build/js/ox.js index cf62dc89..0d65d03e 100644 --- a/build/js/ox.js +++ b/build/js/ox.js @@ -11,6 +11,39 @@ Core functions ================================================================================ */ +Ox.getset = function(obj, args, callback, context) { + /* + generic getter and setter function + Ox.getset(obj) returns obj + Ox.getset(obj, str) returns obj.str + Ox.getset(obj, {key: val, ...}, callback, context) sets obj.key to val, + calls callback(key, val), + returns context + */ + var args = args || {}, + callback = callback || function() {}, + context = context || {}, + length = args.length, + ret; + if (length == 0) { + // getset() + ret = obj; + } else if (length == 1 && typeof arguments[0] == "string") { + // getset(str) + ret = obj[args[0]] + } else { + // getset(str, val) or getset({str: val, ...}) + // translate (str, val) to ({str: val}) + args = Ox.makeObject(args); + obj = $.extend(obj, args); + $.each(args, function(k, v) { + callback(k, v); + }); + ret = context; + } + return ret; +} + Ox.print = function() { /* */ @@ -523,6 +556,10 @@ Ox.getISOYear = function(date) { return date_.getFullYear(); }; +Ox.getTime = function() { + return +new Date(); +} + Ox.getTimezoneOffsetString = function(date) { /* Time zone offset string (-1200 - +1200) diff --git a/build/js/ox.ui.js b/build/js/ox.ui.js index b84692df..904c340d 100644 --- a/build/js/ox.ui.js +++ b/build/js/ox.ui.js @@ -70,12 +70,55 @@ requires ---------------------------------------------------------------------------- */ + Ox.App = function() { + /* + options: + requestTimeout + requestType + requestURL + */ + return function(options) { + + options = options || {}; + var self = {}, + that = this; + + self.options = $.extend({ + requestTimeout: oxui.requestTimeout, + requestType: oxui.requestType, + requestURL: oxui.requestURL + }, options); + + self.change = function() { + + }; + + that.launch = function() { + $.ajaxSetup({ + timeout: self.options.requestTimeout, + type: self.options.requestType, + url: self.options.requestURL + }); + }; + + that.options = function() { + return Ox.getset(self.options, Array.slice.call(arguments), self.change, that); + }; + + return that; + + }; + + }(); + /* ---------------------------------------------------------------------------- Ox.Cache ---------------------------------------------------------------------------- */ + // currently part of Ox.Request + /* ---------------------------------------------------------------------------- Ox.Event @@ -331,6 +374,163 @@ requires ---------------------------------------------------------------------------- */ + /* + ---------------------------------------------------------------------------- + Ox.Request + ---------------------------------------------------------------------------- + */ + + Ox.Request = function() { + + var cache = {}, + pending = {}, + requests = {}, + self = { + options: { + timeout: 15000, + type: "POST", + url: "api" + } + }; + + return { + + cancel: function() { + var index; + if (arguments.length == 0) { + requests = {}; + } else if (Ox.isFunction(arguments[0])) { + // cancel with function + $.each(requests, function(id, req) { + if (arguments[0](req)) { + delete requests[id]; + } + }) + } else { + // cancel by id + delete requests[arguments[0]] + } + }, + + options: function(options) { + return Ox.getset(self.options, options, $.noop(), this) + }, + + send: function(options) { + + options = $.extend({ + age: -1, + callback: function() {}, + id: Ox.uid(), + timeout: self.options.timeout, + type: self.options.type, + url: self.options.url + }, options); + + var req = JSON.stringify({ + url: options.url, + data: options.data + }); + + function callback(data, callback) { + delete requests[options.id]; + callback(data); + } + + function error(request, status, error) { + var $dialog = new Ox.Dialog({ + title: "Error: Remote request failed.", + buttons: [ + new Ox.Button({ + value: "Details", + click: function() { + var $iframe = $("