AjaxPro and the jQuery JavaScript Provider

Tags: AJAX, Ajax.NET, ASP.NET, JavaScript, jQuery

Because jQuery is a often used JavaScript framework I will have a provider available in the next release of Ajax.NET Professional that will render only the wrapper JavaScript files in the jQuery JavaScript code.

 

The JavaScript output for jQuery

MyAjaxClass_class = function() {};
MyAjaxClass_class.prototype.url = '/WebSite1/ajaxpro/Default,App_Code.ashx';

MyAjaxClass_class.prototype.MyMethod = function(onsuccess, onerror) {
   return $.ajax({
      type: "POST",
      url: this.url,
      data: "{}",
      beforeSend: function(xhr) {
         xhr.setRequestHeader("X-AjaxPro-Method", "MyMethod");
         xhr.setRequestHeader("X-AjaxPro-Token", AjaxPro.token);
      },
      success: function(s) {
         var o = null;
         eval("o = " + s + ";");
         if(o != null) {
            if(typeof o.value != "undefined" && typeof onsuccess == "function")
               onsuccess(o.value);
            else if(typeof o.error != "undefined" && typeof onerror == "function")
               onerror(o.error);
         }
         if(typeof onerror == "function") {
            onerror({"Message":"Failed."});
         }
      }
   }); 
};
var MyAjaxClass = new MyAjaxClass_class();

Maybe this will change until the release version, but it is working similar to the AjaxPro way of invoking AjaxMethods. One change here is that it will return the XmlHttpRequest object and you could cancel the request at your own.

 

The jQuery JavaScript Provider

The .NET C# code that will render this output looks like this:

public class jQueryTypeJavaScriptProvider : TypeJavaScriptProvider
{
   public override void RenderClassBegin()
   {
      string clientNS = GetClientNamespace();

      sb.Append(clientNS);
      sb.Append("_class = function() {};\r\n");
         
      sb.Append(clientNS);
      sb.Append("_class.prototype.url = '" + m_URL + "';\r\n");
   }

   public override void RenderClassEnd()
   {
      string clientNS = GetClientNamespace();

      sb.Append("var ");
      sb.Append(clientNS);
      sb.Append(" = new ");
      sb.Append(clientNS);
      sb.Append("_class();\r\n\r\n");
   }

   public override void RenderMethods(List<MethodInfo> methods)
   {
      string clientNS = GetClientNamespace();

      for (int i = 0; i < methods.Count; i++)
      {
         MethodInfo method = methods[i];
         string methodName = GetClientMethodName(method);
         ParameterInfo[] pi = method.GetParameters();

         sb.Append(clientNS + "_class.prototype.");
         sb.Append(methodName);
         sb.Append(" = function(");

         for (int p = 0; p < pi.Length; p++)
         {
            sb.Append(pi[p].Name);
         }

         sb.Append("callback) {\r\n");

         sb.Append(@"
   return $.ajax({
      type: ""POST"",
      url: this.url,
      data: ""{}"",
      beforeSend: function(xhr) {
         xhr.setRequestHeader(""X-AjaxPro-Method"", """ + methodName + @""");
         xhr.setRequestHeader(""X-AjaxPro-Token"", AjaxPro.token);
      },
      success: function(s) {
         var o = null;
         eval(""o = "" + s + "";"");
         if(o != null) {
            if(typeof o.value != "undefined" && 
typeof onsuccess == "
function") onsuccess(o.value); else if(typeof o.error != "undefined" &&
typeof onerror == "
function") onerror(o.error); } if(typeof onerror == "function") { onerror({"Message":"Failed."}); } } }); "); sb.Append("};\r\n"); //if (i < methods.Count - 1) // sb.Append(","); } } }

There will be a new setting in web.config where you can configure which provider you want to use, one of the built-in providers or your own. With the scriptReplacements settings you can disable the core, prototype and converter JavaScript files, and then you are ready to use it without any JavaScript code built by AjaxPro.

I hope that this will help developers to use AjaxPro with any JavaScript framework.

3 Comments

Comments have been disabled for this content.