From 47d05516b1d1c465217c59732bf8442ab0cfd497 Mon Sep 17 00:00:00 2001 From: wei <> Date: Fri, 5 May 2006 06:58:04 +0000 Subject: Added prioritize callback and enabled viewstate update on callback return. --- framework/Web/Javascripts/js/ajax.js | 44 ++++++----- framework/Web/Javascripts/prado/ajax3.js | 130 ++++++++++++++++++------------- 2 files changed, 98 insertions(+), 76 deletions(-) (limited to 'framework/Web/Javascripts') diff --git a/framework/Web/Javascripts/js/ajax.js b/framework/Web/Javascripts/js/ajax.js index 6ab3d18d..5deebd1b 100644 --- a/framework/Web/Javascripts/js/ajax.js +++ b/framework/Web/Javascripts/js/ajax.js @@ -23,7 +23,7 @@ setTimeout(this.onComplete.bind(this),10);}}});Ajax.PeriodicalUpdater=Class.crea this.timer=setTimeout(this.onTimerEvent.bind(this),this.decay*this.frequency*1000);},onTimerEvent:function(){this.updater=new Ajax.Updater(this.container,this.url,this.options);}});Object.extend(Ajax.Request.prototype,{respondToReadyState:function(readyState) {var event=Ajax.Request.Events[readyState];var transport=this.transport,json=this.getHeaderData(Prado.CallbackRequest.DATA_HEADER);if(event=='Complete') {try -{Ajax.Responders.dispatch('on'+transport.status,this,transport,json);Prado.CallbackRequest.dispatchActions(transport,this.getHeaderData(Prado.CallbackRequest.ACTION_HEADER));(this.options['on'+this.transport.status]||this.options['on'+(this.responseIsSuccess()?'Success':'Failure')]||Prototype.emptyFunction)(transport,json);}catch(e){this.dispatchException(e);} +{Prado.CallbackRequest.updatePageState(this,transport);Ajax.Responders.dispatch('on'+transport.status,this,transport,json);Prado.CallbackRequest.dispatchActions(transport,this.getHeaderData(Prado.CallbackRequest.ACTION_HEADER));(this.options['on'+this.transport.status]||this.options['on'+(this.responseIsSuccess()?'Success':'Failure')]||Prototype.emptyFunction)(transport,json);}catch(e){this.dispatchException(e);} if((this.header('Content-type')||'').match(/^text\/javascript/i)) this.evalResponse();} try{(this.options['on'+event]||Prototype.emptyFunction)(transport,json);Ajax.Responders.dispatch('on'+event,this,transport,json);}catch(e){this.dispatchException(e);} @@ -34,7 +34,7 @@ this.transport.onreadystatechange=Prototype.emptyFunction;},getHeaderData:functi catch(e) {if(typeof(json)=="string") {Logger.info("using json") -return Prado.CallbackRequest.decode(json);}}}});Prado.CallbackRequest=Class.create();Object.extend(Prado.CallbackRequest,{FIELD_CALLBACK_TARGET:'PRADO_CALLBACK_TARGET',FIELD_CALLBACK_PARAMETER:'PRADO_CALLBACK_PARAMETER',FIELD_CALLBACK_PAGESTATE:'PRADO_PAGESTATE',PostDataLoaders:[],DATA_HEADER:'X-PRADO-DATA',ACTION_HEADER:'X-PRADO-ACTIONS',ERROR_HEADER:'X-PRADO-ERROR',Queque:[],InProgress:[],dispatchActions:function(transport,actions) +return Prado.CallbackRequest.decode(json);}}}});Prado.CallbackRequest=Class.create();Object.extend(Prado.CallbackRequest,{FIELD_CALLBACK_TARGET:'PRADO_CALLBACK_TARGET',FIELD_CALLBACK_PARAMETER:'PRADO_CALLBACK_PARAMETER',FIELD_CALLBACK_PAGESTATE:'PRADO_PAGESTATE',PostDataLoaders:[],DATA_HEADER:'X-PRADO-DATA',ACTION_HEADER:'X-PRADO-ACTIONS',ERROR_HEADER:'X-PRADO-ERROR',PAGESTATE_HEADER:'X-PRADO-PAGESTATE',requestInProgress:null,dispatchActions:function(transport,actions) {if(actions&&actions.length>0) actions.each(this.__run.bind(this,transport));},__run:function(transport,command) {for(var method in command) @@ -54,27 +54,29 @@ msg+=e.version+" "+e.time+"\n";return msg;}},encode:function(data) {return Prado.JSON.stringify(data);},decode:function(data) {if(typeof(data)=="string"&&data.trim().length>0) return Prado.JSON.parse(data);else -return null;},dispatchQuequedRequest:function() -{requests=Prado.CallbackRequest;if(requests.InProgress.length==0&&requests.Queque.length>0) -{var item=requests.Queque.pop();item.callback=new Ajax.Request(item.url,item.options);item.callback.callbackID=item.id;item.timeout=setTimeout(function() -{requests.removeInProgress(item.callback);},item.options.TimeOut);requests.InProgress.push(item);}},removeInProgress:function(request) -{if(request&&request.callbackID) -{var stillInProgress=[];requests=Prado.CallbackRequest;requests.InProgress.each(function(item) -{if(item.callback.callbackID==request.callbackID) -{item.callback.transport.abort();request.transport.abort();clearTimeout(item.timeout);} -else -{stillInProgress.push(item);}}) -requests.InProgress=stillInProgress;} -requests.dispatchQuequedRequest();}}) -Ajax.Responders.register({onComplete:Prado.CallbackRequest.removeInProgress});Event.OnLoad(function() +return null;},dispatchPriorityRequest:function(callback) +{Logger.info("priority request "+callback.id) +this.abortRequestInProgress();callback.request=new Ajax.Request(callback.url,callback.options);callback.timeout=setTimeout(function() +{Logger.warn("priority timeout");Prado.CallbackRequest.abortRequestInProgress();},callback.options.RequestTimeOut);this.requestInProgress=callback;Logger.info("dispatched "+this.requestInProgress)},dispatchNormalRequest:function(callback) +{Logger.info("dispatching normal request");new Ajax.Request(callback.url,callback.options);},abortRequestInProgress:function() +{inProgress=Prado.CallbackRequest.requestInProgress;if(inProgress) +{Logger.warn("aborted "+inProgress.id) +inProgress.request.transport.abort();clearTimeout(inProgress.timeout);Prado.CallbackRequest.requestInProgress=null;}},updatePageState:function(request,transport) +{pagestate=$(this.FIELD_CALLBACK_PAGESTATE);if(request.options.EnablePageStateUpdate&&request.options.HasPriority&&pagestate) +{Logger.warn("updating page state");pagestate.value=request.header(this.PAGESTATE_HEADER);}}}) +Ajax.Responders.register({onComplete:function(request) +{if(request.options.HasPriority) +Prado.CallbackRequest.abortRequestInProgress();}});Event.OnLoad(function() {if(typeof Logger!="undefined") -Ajax.Responders.register(Prado.CallbackRequest.Exception);});Prado.CallbackRequest.prototype={url:window.location.href,options:{TimeOut:30000},id:null,callback:null,initialize:function(id,options) -{Object.extend(this.options,options||{});this.id=id;var request={postBody:this._getPostData(),parameters:''} -Object.extend(this.options,request);if(this.options.CausesValidation!=false&&typeof(Prado.Validation)!="undefined") +Ajax.Responders.register(Prado.CallbackRequest.Exception);});Prado.CallbackRequest.prototype={url:window.location.href,options:{},id:null,request:null,initialize:function(id,options) +{this.id=id;this.options={RequestTimeOut:30000,EnablePageStateUpdate:true,HasPriority:true,CausesValidation:true,ValidationGroup:null,PostInputs:true,postBody:this._getPostData(),parameters:''} +Object.extend(this.options,options||{});if(this.options.CausesValidation&&typeof(Prado.Validation)!="undefined") {var form=this.options.Form||Prado.Validation.getForm();if(Prado.Validation.validate(form,this.options.ValidationGroup,this)==false) return;} -Prado.CallbackRequest.Queque.push(this);Prado.CallbackRequest.dispatchQuequedRequest();},_getPostData:function() -{var data={};var callback=Prado.CallbackRequest;if(this.options.PostState!=false) +if(this.options.HasPriority) +Prado.CallbackRequest.dispatchPriorityRequest(this);else +Prado.CallbackRequest.dispatchNormalRequest(this);},_getPostData:function() +{var data={};var callback=Prado.CallbackRequest;if(this.options.PostInputs!=false) {callback.PostDataLoaders.each(function(name) {$A(document.getElementsByName(name)).each(function(element) {var value=$F(element);if(typeof(value)!="undefined") @@ -83,7 +85,7 @@ if(typeof(this.options.params)!="undefined") data[callback.FIELD_CALLBACK_PARAMETER]=callback.encode(this.options.params);var pageState=$F(callback.FIELD_CALLBACK_PAGESTATE);if(typeof(pageState)!="undefined") data[callback.FIELD_CALLBACK_PAGESTATE]=pageState;data[callback.FIELD_CALLBACK_TARGET]=this.id;return $H(data).toQueryString();}} Prado.Callback=function(UniqueID,parameter,onSuccess,options) -{var callback={'params':parameter||'','onSuccess':onSuccess||Prototype.emptyFunction,'CausesValidation':true};Object.extend(callback,options||{});new Prado.CallbackRequest(UniqueID,callback);return false;} +{var callback={'params':parameter||'','onSuccess':onSuccess||Prototype.emptyFunction};Object.extend(callback,options||{});new Prado.CallbackRequest(UniqueID,callback);return false;} Array.prototype.______array='______array';Prado.JSON={org:'http://www.JSON.org',copyright:'(c)2005 JSON.org',license:'http://www.crockford.com/JSON/license.html',stringify:function(arg){var c,i,l,s='',v;switch(typeof arg){case'object':if(arg){if(arg.______array=='______array'){for(i=0;i 0) + Logger.info("priority request "+callback.id) + this.abortRequestInProgress(); + + callback.request = new Ajax.Request(callback.url, callback.options); + callback.timeout = setTimeout(function() { - var item = requests.Queque.pop(); - item.callback = new Ajax.Request(item.url, item.options); - item.callback.callbackID = item.id; - item.timeout = setTimeout(function() - { - requests.removeInProgress(item.callback); - }, item.options.TimeOut); - requests.InProgress.push(item); + Logger.warn("priority timeout"); + Prado.CallbackRequest.abortRequestInProgress(); + },callback.options.RequestTimeOut); + + this.requestInProgress = callback; + Logger.info("dispatched "+this.requestInProgress) + }, + + /** + * Dispatch a normal request, no timeouts or aborting of requests. + */ + dispatchNormalRequest : function(callback) + { + Logger.info("dispatching normal request"); + new Ajax.Request(callback.url, callback.options); + }, + + /** + * Abort the current priority request in progress. + */ + abortRequestInProgress : function() + { + inProgress = Prado.CallbackRequest.requestInProgress; + if(inProgress) + { + Logger.warn("aborted "+inProgress.id) + inProgress.request.transport.abort(); + clearTimeout(inProgress.timeout); + Prado.CallbackRequest.requestInProgress = null; } }, /** - * Remove a request currently in progress and call dispatchQuequedRequest. + * Updates the page state. It will update only if EnablePageStateUpdate and + * HasPriority options are both true. */ - removeInProgress : function(request) + updatePageState : function(request, transport) { - if(request && request.callbackID) + pagestate = $(this.FIELD_CALLBACK_PAGESTATE); + if(request.options.EnablePageStateUpdate && request.options.HasPriority && pagestate) { - var stillInProgress = []; - requests = Prado.CallbackRequest; - requests.InProgress.each(function(item) - { - if(item.callback.callbackID == request.callbackID) - { - item.callback.transport.abort(); - request.transport.abort(); - clearTimeout(item.timeout); - } - else - { - stillInProgress.push(item); - } - }) - requests.InProgress = stillInProgress; + Logger.warn("updating page state"); + pagestate.value = request.header(this.PAGESTATE_HEADER); } - requests.dispatchQuequedRequest(); } }) -Ajax.Responders.register({onComplete : Prado.CallbackRequest.removeInProgress}); +/** + * Automatically aborts the current request when a priority request has returned. + */ +Ajax.Responders.register({onComplete : function(request) +{ + if(request.options.HasPriority) + Prado.CallbackRequest.abortRequestInProgress(); +}}); //Add HTTP exception respones when logger is enabled. Event.OnLoad(function() @@ -291,10 +310,7 @@ Prado.CallbackRequest.prototype = /** * Callback options, including onXXX events. */ - options : - { - TimeOut : 30000 // 30 second timeout. - }, + options : { }, /** * Callback target ID. E.g. $control->getUniqueID(); @@ -304,34 +320,39 @@ Prado.CallbackRequest.prototype = /** * Current callback request. */ - callback : null, + request : null, /** * Prepare and inititate a callback request. */ initialize : function(id, options) { - Object.extend(this.options, options || {}); - this.id = id; - - var request = + + this.options = { + RequestTimeOut : 30000, // 30 second timeout. + EnablePageStateUpdate : true, + HasPriority : true, + CausesValidation : true, + ValidationGroup : null, + PostInputs : true, postBody : this._getPostData(), parameters : '' } - - Object.extend(this.options, request); - - if(this.options.CausesValidation != false && typeof(Prado.Validation) != "undefined") + Object.extend(this.options, options || {}); + + if(this.options.CausesValidation && typeof(Prado.Validation) != "undefined") { var form = this.options.Form || Prado.Validation.getForm(); if(Prado.Validation.validate(form,this.options.ValidationGroup,this) == false) return; } - Prado.CallbackRequest.Queque.push(this); - Prado.CallbackRequest.dispatchQuequedRequest(); + if(this.options.HasPriority) + Prado.CallbackRequest.dispatchPriorityRequest(this); + else + Prado.CallbackRequest.dispatchNormalRequest(this); }, /** @@ -343,7 +364,7 @@ Prado.CallbackRequest.prototype = { var data = {}; var callback = Prado.CallbackRequest; - if(this.options.PostState != false) + if(this.options.PostInputs != false) { callback.PostDataLoaders.each(function(name) { @@ -378,8 +399,7 @@ Prado.Callback = function(UniqueID, parameter, onSuccess, options) var callback = { 'params' : parameter || '', - 'onSuccess' : onSuccess || Prototype.emptyFunction, - 'CausesValidation' : true + 'onSuccess' : onSuccess || Prototype.emptyFunction }; Object.extend(callback, options || {}); -- cgit v1.2.3