1 APE.Request = new Class({
2 initialize: function(ape) {
4 this.stack = new APE.Request.Stack(ape);
5 this.cycledStack = new APE.Request.CycledStack(ape);
7 this.callbackChl = new $H;
9 //Fix presto bug (see send method)
10 if (Browser.Engine.presto){
15 this.requestObserver.periodical(10, this);
19 send: function(cmd, params, options, noWatch) {
20 if (this.ape.requestDisabled) return;
22 if (Browser.Engine.presto && !noWatch) {
23 this.requestVar.updated = true;
24 this.requestVar.args.push([cmd, params, options]);
29 if (!options) options = {};
31 opt.event = options.event || true;
32 opt.requestCallback = options.requestCallback || null;
33 opt.callback = options.callback;
34 var ret = this.ape.transport.send(this.parseCmd(cmd, params, opt), opt);
36 $clear(this.ape.pollerObserver);
37 this.ape.pollerObserver = this.ape.poller.delay(this.ape.options.pollTime, this.ape);
42 parseCmd: function(cmd, params, options) {
46 if ($type(cmd) == 'array') {
48 for (var i = 0; i < cmd.length; i++) {
54 if (!tmp.options) tmp.options = {};
56 tmp.params ? o.params = tmp.params : null;
57 evParams = $extend({}, o.params);
60 if (!$defined(tmp.options.sessid) || tmp.options.sessid !== false) o.sessid = this.ape.sessid;
63 var ev = 'cmd_' + tmp.cmd.toLowerCase();
65 if (tmp.options.callback) this.callbackChl.set(o.chl, tmp.options.callback);
66 if (tmp.options.requestCallback) options.requestCallback = tmp.options.requestCallback;
68 //Request is on a pipe, fire the event on the pipe
69 if (o.params && o.params.pipe) {
70 var pipe = this.ape.getPipe(o.params.pipe);
71 if (pipe) evParams = [evParams, pipe];
74 this.ape.fireEvent('onCmd', [tmp.cmd, evParams]);
76 if (pipe) pipe.fireEvent(ev, evParams);
78 this.ape.fireEvent(ev, evParams);
85 params ? o.params = params : null;
86 var evParams = $extend({}, params);
89 if (!$defined(options.sessid) || options.sessid !== false) o.sessid = this.ape.sessid;
92 var ev = 'cmd_' + cmd.toLowerCase();
93 if (options.callback) this.callbackChl.set(o.chl, options.callback);
96 //Request is on a pipe, fire the event on the pipe
97 if (params && params.pipe) {
98 var pipe = this.ape.getPipe(params.pipe);
99 if (pipe) evParams = [evParams, pipe];
101 this.ape.fireEvent('onCmd', [cmd, evParams]);
103 if (pipe) pipe.fireEvent(ev, evParams);
105 this.ape.fireEvent(ev, evParams);
109 var transport = this.ape.options.transport;
110 return JSON.stringify(a, function(key, value) {
111 if (typeof(value) == 'string') {
112 value = encodeURIComponent(value);
113 //In case of JSONP data have to be escaped two times
114 if (transport == 2) value = encodeURIComponent(value);
121 * This method is only used by opera.
122 * Opera have a bug, when request are sent trought user action (ex : a click), opera throw a security violation when trying to make a XHR.
123 * The only way is to set a class var and watch when this var change
125 requestObserver: function(){
126 if (this.requestVar.updated) {
127 var args = this.requestVar.args.shift();
128 this.requestVar.updated = (this.requestVar.args.length>0) ? true : false;
129 args[3] = true; //Set noWatch argument to true
130 this.send.run(args, this);