WTF is "Super Ajax?"

Ok there's really no such thing. However, there's a pretty nifty feature in NinJa's Ajax framework that allows you to create referenced calls to a web service that can be reused and provided full Intellisense support! Look at the sample below, looky looky looky:


Ok so what? This is where the magic begins! Now when you type WebService, you will get Intellisense to AddCall:

  • AddCall(functionName, returnType, params...)  - This will generate a call that can be made with the given functionName and indicates the function on the webservice returns a type of returnType and will take the following named parameters.

So lets look at an ASMX web service we have set up:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for service
/// </summary>
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
public class service : System.Web.Services.WebService {

    public service () {

        //Uncomment the following line if using designed components 

    public string Encode(string text)
        return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));

    public string Decode(string text)
        return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(text));

If we want to set up calls to this service without having to recreate a WebServiceRequest every single time, we'd use the following code:

service.AddCall("Encode", "String", "text");
service.AddCall("Decode", "String", "text");

alert(service.Decode(service.Encode("Hello world!")));

The first line creates a reference. The next two lines add calls to the reference, Encode and Decode. Now when we type "service.", we will see Intellisense show two new functions, Encode and Decode. The other parameters tell what the functions will return and accept. More importantly, the "text" provided here starts the named parameters. When we type "service.Encode(", Intellisense will bring up the list of parameters and whatever is provided will be named as such. Like with the WebServiceRequest, the names of these parameters must match the names found on the web service implemented. But this way we can generate the call once and let it be reused in our entire web site!

Also, these calls come in two flavors, synchronouse and asynchronous. Here service.Encode and service.Decode both default to synchronous calls. That's where the returnType provided before comes in. These calls will return whatever the server returns directly, in this case, strings. The Intellisense will know that these are strings and provide full Intellisense support!

The asynchronous methods would be service.EncodeAsync and service.DecodeAsync. These functions accept the parameters as before, but the last two parameters will always be functions to call onSuccess and onFail of the request. These functions will be provided an AjaxEventArgs just like the standard AjaxRequest/WebServiceRequest objects.

Now unfortunately, you do lose a bit of flexibility with this feature. You no longer have a reference to the actual request being created and can no longer cancel requests with the Abort method and all requests generated by this method follow the defaults defined on the static WebServiceRequest object. However, this provides a way to write a service call once and let it be reused through out an entire web app and get fully Intellisense support!

Last edited Jun 15, 2012 at 5:32 PM by dahrkdaiz, version 1


No comments yet.