summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/prado/ajax3.js
diff options
context:
space:
mode:
authorwei <>2006-05-02 08:28:17 +0000
committerwei <>2006-05-02 08:28:17 +0000
commitd255f4d0e332740b3984e21ce3f7a4a4f1968ba3 (patch)
tree84bf54643e3a188330f6b7b5043efeed1aa44b6e /framework/Web/Javascripts/prado/ajax3.js
parentc5983c0440913cd67f3744c7dda3c3bfc7eee0ed (diff)
Adding more work on ActiveControls.
Diffstat (limited to 'framework/Web/Javascripts/prado/ajax3.js')
-rw-r--r--framework/Web/Javascripts/prado/ajax3.js213
1 files changed, 171 insertions, 42 deletions
diff --git a/framework/Web/Javascripts/prado/ajax3.js b/framework/Web/Javascripts/prado/ajax3.js
index ded63104..1cffbd73 100644
--- a/framework/Web/Javascripts/prado/ajax3.js
+++ b/framework/Web/Javascripts/prado/ajax3.js
@@ -1,27 +1,131 @@
/**
+ * Override Prototype's response implementation.
+ */
+Object.extend(Ajax.Request.prototype,
+{
+ /**
+ * Customize the response, dispatch onXXX response code events, and
+ * tries to execute response actions (javascript statements).
+ */
+ respondToReadyState : function(readyState)
+ {
+ var event = Ajax.Request.Events[readyState];
+ var transport = this.transport, json = this.getHeaderData(Prado.CallbackRequest.DATA_HEADER);
+
+ if (event == 'Complete')
+ {
+ Ajax.Responders.dispatch('on' + transport.status, this, transport, json);
+ Prado.CallbackRequest.dispatchActions(this.getHeaderData(Prado.CallbackRequest.ACTION_HEADER));
+
+ try {
+ (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);
+ }
+
+ /* Avoid memory leak in MSIE: clean up the oncomplete event handler */
+ if (event == 'Complete')
+ this.transport.onreadystatechange = Prototype.emptyFunction;
+ },
+
+ /**
+ * Gets header data assuming JSON encoding.
+ * @param string header name
+ * @return object header data as javascript structures.
+ */
+ getHeaderData : function(name)
+ {
+ try
+ {
+ var json = this.header(name);
+ return eval('(' + json + ')');
+ }
+ catch (e)
+ {
+ if(typeof(json) == "string")
+ {
+ Logger.info("using json")
+ return Prado.CallbackRequest.decode(json);
+ }
+ }
+ }
+});
+
+/**
* Prado Callback client-side request handler.
*/
-Prado.Callback = Class.create();
+Prado.CallbackRequest = Class.create();
/**
* Static definitions.
*/
-Object.extend(Prado.Callback,
+Object.extend(Prado.CallbackRequest,
{
/**
* Callback request target POST field name.
*/
FIELD_CALLBACK_TARGET : 'PRADO_CALLBACK_TARGET',
-
/**
* Callback request parameter POST field name.
*/
FIELD_CALLBACK_PARAMETER : 'PRADO_CALLBACK_PARAMETER',
-
/**
* List of form fields that will be collected during callback.
*/
PostDataLoaders : ['PRADO_PAGESTATE'],
+ /**
+ * Response data header name.
+ */
+ DATA_HEADER : 'X-PRADO-DATA',
+ /**
+ * Response javascript execution statement header name.
+ */
+ ACTION_HEADER : 'X-PRADO-ACTIONS',
+ /**
+ * Response errors/exceptions header name.
+ */
+ ERROR_HEADER : 'X-PRADO-ERROR',
+
+ /**
+ * Dispatch callback response actions.
+ */
+ dispatchActions : function(actions)
+ {
+ actions.each(this.__run);
+ },
+
+ /**
+ * Prase and evaluate a Callback clien-side action
+ */
+ __run : function(command)
+ {
+ for(var method in command)
+ {
+ if(command[method][0])
+ {
+ var id = command[method][0];
+ if($(id) || id.indexOf("[]") > -1)
+ method.toFunction().apply(this,command[method]);
+ else if(typeof(Logger) != "undefined")
+ {
+ Logger.error("Error in executing callback response:",
+ "Unable to find HTML element with ID '"+id+"' before executing "+method+"().");
+ }
+ }
+ }
+ },
/**
* Respond to Prado Callback request exceptions.
@@ -32,27 +136,40 @@ Object.extend(Prado.Callback,
* Server returns 505 exception. Just log it.
*/
"on505" : function(request, transport, data)
- {
- var msg = 'HTTP '+transport.status+" with response";
- Logger.error(msg, transport.responseText);
- this.logException(data);
+ {
+ var e = request.getHeaderData(Prado.CallbackRequest.ERROR_HEADER);
+ Logger.error("Callback Server Error "+e.code, this.formatException(e));
},
/**
* Callback OnComplete event,logs reponse and data to console.
*/
- onComplete : function(request, transport, data)
+ 'on200' : function(request, transport, data)
{
- if(transport.status != 505)
+ if(transport.status < 500)
{
var msg = 'HTTP '+transport.status+" with response : \n";
msg += transport.responseText + "\n";
- msg += "Data : \n"+inspect(data);
+ msg += "Data : \n"+inspect(data)+"\n";
+ msg += "Actions : \n";
+ request.getHeaderData(Prado.CallbackRequest.ACTION_HEADER).each(function(action)
+ {
+ msg += inspect(action)+"\n";
+ })
+
Logger.warn(msg);
}
},
/**
+ * Uncaught exceptions during callback response.
+ */
+ onException : function(e)
+ {
+ Logger.error('Uncaught Callback Client Exception:', e);
+ },
+
+ /**
* Formats the exception message for display in console.
*/
formatException : function(e)
@@ -67,15 +184,8 @@ Object.extend(Prado.Callback,
msg += "("+trace[i].line+"): ";
msg += trace[i]["class"]+"->"+trace[i]["function"]+"()"+"\n";
}
+ msg += e.version+" "+e.time+"\n";
return msg;
- },
-
- /**
- * Log Callback response exceptions to console.
- */
- logException : function(e)
- {
- Logger.error("Callback Request Error "+e.code, this.formatException(e));
}
},
@@ -84,7 +194,7 @@ Object.extend(Prado.Callback,
*/
encode : function(data)
{
- Prado.JSON.stringify(data);
+ return Prado.JSON.stringify(data);
},
/**
@@ -100,13 +210,13 @@ Object.extend(Prado.Callback,
Event.OnLoad(function()
{
if(typeof Logger != "undefined")
- Ajax.Responders.register(Prado.Callback.Exception);
+ Ajax.Responders.register(Prado.CallbackRequest.Exception);
});
/**
* Create and prepare a new callback request.
*/
-Prado.Callback.prototype =
+Prado.CallbackRequest.prototype =
{
/**
* Callback URL, same url as the current page.
@@ -124,27 +234,31 @@ Prado.Callback.prototype =
id : null,
/**
- * Callback parameters.
+ * Current callback request.
*/
- parameters : null,
+ request : null,
/**
* Prepare and inititate a callback request.
*/
- initialize : function(id, parameters, onSuccess, options)
+ initialize : function(id, options)
{
this.options = options || {};
this.id = id;
- this.parameters = parameters;
var request =
{
postBody : this._getPostData(),
- onSuccess : this._onSuccess.bind(this)
+ parameters : ''
}
Object.extend(this.options || {},request);
-
- new Ajax.Request(this.url, this.options);
+ if(this.options.CausesValidation != false && typeof(Prado.Validation) != "undefined")
+ {
+ var form = this.options.Form || Prado.Validation.getForm();
+ if(Prado.Validation.validate(form,this.options.ValidationGroup,this) == false)
+ return;
+ }
+ this.request = new Ajax.Request(this.url, this.options);
},
/**
@@ -156,7 +270,7 @@ Prado.Callback.prototype =
{
var data = {};
- Prado.Callback.PostDataLoaders.each(function(name)
+ Prado.CallbackRequest.PostDataLoaders.each(function(name)
{
$A(document.getElementsByName(name)).each(function(element)
{
@@ -165,17 +279,32 @@ Prado.Callback.prototype =
data[name] = value;
})
})
- if(typeof(this.parameters) != "undefined")
- data[Prado.Callback.FIELD_CALLBACK_PARAMETER] = Prado.Callback.encode(this.parameters);
- data[Prado.Callback.FIELD_CALLBACK_TARGET] = this.id;
+ if(typeof(this.options.params) != "undefined")
+ data[Prado.CallbackRequest.FIELD_CALLBACK_PARAMETER] = Prado.CallbackRequest.encode(this.options.params);
+ data[Prado.CallbackRequest.FIELD_CALLBACK_TARGET] = this.id;
return $H(data).toQueryString();
- },
-
- /**
- * Dispatch a successfull response to the appropriate responders.
- */
- _onSuccess : function(response, transport, json)
- {
- //Logger.info("asd");
}
-} \ No newline at end of file
+}
+
+/**
+ * Create a new callback request using default settings.
+ * @param string callback handler unique ID.
+ * @param mixed parameter to pass to callback handler on the server side.
+ * @param function client side onSuccess event handler.
+ * @param object additional request options.
+ * @return boolean always false.
+ */
+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;
+}