/// <summary> /// Generates the javascript proxy function. /// </summary> /// <param name="url">The URL.</param> /// <param name="functionName">Name of the function.</param> /// <param name="httpRequestMethod">The HTTP request method.</param> /// <param name="scriptFunctionParameters">The script function parameters.</param> /// <returns></returns> protected override string GenerateJavascriptFunction(string url, string functionName, string httpRequestMethod, ScriptFunctionParameter[] scriptFunctionParameters) { var function = new StringBuilder(); function.AppendFormat("\t{0}: function(", functionName); var ajaxCallParameters = new StringBuilder("_="); for(var i = 0; i < scriptFunctionParameters.Length; i++) { var scriptFunctionParameter = scriptFunctionParameters[i]; var clientSideName = scriptFunctionParameter.ClientSideParameterName; var serverSideName = scriptFunctionParameter.ServerSideParameterName; // append to the function parameters function.AppendFormat("{0}{1}", clientSideName, i < scriptFunctionParameters.Length - 1 ? ", " : string.Empty); // append to the ajax call parameters ajaxCallParameters.Append("&") .Append(serverSideName) .Append("='+") // I don't know the equivalent of Object.toJSON in JQuery, so if you need it.. this is the place to insert it :) //.Append(scriptFunctionParameter.NeedsJSONEncoding ? "Object.toJSON(" + clientSideName + ")" : clientSideName) .Append(clientSideName) .Append("+'"); // close the last parameter with an extra "," for the callback parameter function.Append(i == scriptFunctionParameters.Length - 1 ? ", " : string.Empty); } function.Append("callback)").Append(Environment.NewLine).Append("\t{").Append(Environment.NewLine); function.AppendFormat("\t\t" + "var r=$.ajax({{type: '{0}'," + "url: '{1}'," + "data: '{2}'," + "async: !!callback," + "complete: callback}});" + Environment.NewLine + "\t\t" + "if(!callback) return r.responseText;", httpRequestMethod, url, ajaxCallParameters); function.Append("\r\n\t}\r\n"); return function.ToString(); }
public static ScriptFunction Create(Delegate @delegate, Type[] types, bool isAsync = false) { var parameters = @delegate.Method.GetParameters(); if (parameters.Length != types.Length) { WrongLength(@delegate.Method, types.Length, parameters.Length); return(null); } var scriptFunctionParameters = new ScriptFunctionParameter[types.Length]; for (int i = 0, length = types.Length; i < length; i++) { var type = types[i]; if (typeof(IBaseObject).IsAssignableFrom(type)) { if (type.IsAssignableFrom(parameters[i].ParameterType)) { scriptFunctionParameters[i] = new ScriptFunctionParameter(true, type); continue; } WrongType(@delegate.Method, type, parameters[i].ParameterType); return(null); } if (type == parameters[i].ParameterType) { scriptFunctionParameters[i] = new ScriptFunctionParameter(false, type); continue; } WrongType(@delegate.Method, type, parameters[i].ParameterType); return(null); } /*if (isAsync && typeof(Task).IsAssignableFrom(@delegate.Method.ReturnType)) * { * WrongReturnType(@delegate.Method, typeof(Task), @delegate.Method.ReturnType); * return null; * }*/ return(new ScriptFunction(@delegate, scriptFunctionParameters)); }
/// <summary> /// Generates the javascript proxy function. /// </summary> /// <param name="url">The URL.</param> /// <param name="functionName">Name of the function.</param> /// <param name="httpRequestMethod">The HTTP request method.</param> /// <param name="scriptFunctionParameters">The script function parameters.</param> /// <returns></returns> /// <remarks> /// if we have a [JSONBinder] mark on the parameter, we can serialize the parameter using prototype's Object.toJSON(). /// This requires Prototype 1.5.1. Users of [JSONBinder] should be aware of that. /// </remarks> protected override string GenerateJavascriptFunction(string url, string functionName, string httpRequestMethod, ScriptFunctionParameter[] scriptFunctionParameters) { var function = new StringBuilder(); function.AppendFormat("\t{0}: function(", functionName); var ajaxCallParameters = new StringBuilder("_="); for(var i = 0; i < scriptFunctionParameters.Length; i++) { var scriptFunctionParameter = scriptFunctionParameters[i]; var clientSideName = scriptFunctionParameter.ClientSideParameterName; var serverSideName = scriptFunctionParameter.ServerSideParameterName; // append parameters to the function definition function.AppendFormat("{0}{1}", clientSideName, i < scriptFunctionParameters.Length - 1 ? ", " : string.Empty); // append to the ajax call parameters ajaxCallParameters.Append("&") .Append(serverSideName) .Append("='+") .Append(scriptFunctionParameter.NeedsJSONEncoding ? "Object.toJSON(" + clientSideName + ")" : clientSideName) .Append("+'"); // close the last parameter with an extra "," for the callback parameter function.Append(i == scriptFunctionParameters.Length - 1 ? ", " : string.Empty); } function.Append("callback)").Append(Environment.NewLine).Append("\t{").Append(Environment.NewLine); function.AppendFormat("\t\tvar r=new Ajax.Request('{0}', " + "{{method: '{1}', asynchronous: !!callback, onComplete: callback, parameters: '{2}'}}); " + Environment.NewLine + "\t\tif(!callback) return r.transport.responseText;", url, httpRequestMethod, ajaxCallParameters); function.Append("\r\n\t}\r\n"); return function.ToString(); }