Sep 23

The script dependency registration of MS AJAX

Posted in Javascript MS AJAX      Comments Off on The script dependency registration of MS AJAX

I’ve finally started looking at the latest release of the ASP.NET AJAX library (preview 5). This version introduces several new features and I’ve thought I should write a couple of posts about it here. Today I’m going to talk about a feature I like to call “script dependency registration” (btw, it has been a long time since I’ve looked at/used the MS AJAX lib, so this feature might have been introduced in previous releases).

The idea is really simple: whenever you create your own script file, you’re supposed to register it and specify its dependencies (if there are any, of course). The best way to understand this is to look at an example. If you look at the MicrosoftAjaxTemplates file, you should see something like this:

["MicrosoftAjaxComponentModel.js", "MicrosoftAjaxSerialization.js"]);

The _register method expects two parameters:

  • a name that identifies the script. Typically, you’ll use the name of the JS file here, though there’s nothing from preventing you to use whatever name you want;
  • an array with a list of dependencies (notice that you’re supposed to pass the name of the dependencies).

What we’re saying in the previous snippet is that we’re registering the current file with the name MicrosoftAjaxTemplates.js and that it requires the MicrosofAjaxComponentModel.js and the MicrosoftAjaxSerialization.js entries for working properly.

If anyone is developing a component or writing code which requires the code available on MicrosoftAjaxTemplates, then you only need to register your new script and pass the “MicrosoftAjaxTemplates.js” value as a dependency:


As you can probably guess by now,the advantage of using this method is that you’ll get an exception if you try to use a script without loading all its dependencies.

If you’re creating scripts which *might* require specific features defined on a third party lib,then you probably will not want to use the Type._registerScript method.

For instance, you might use the DataView control’s ability to load data from a web service. To ensure that, you could always use the _registerScript method to force the loading of the JavaScript file which had the web service code. However, you might want to use only the basic features of the DataView control which don’t rely on some third party JS web service code. Since you’ve used the _registerScript to ensure scenario 1 (which required the web service JS code), you would end up adding unnecessary  code to the page (the web service code JavaScript file).

In these cases, there’s a better option: you should use the _checkDependency method instead of using the _registerScript method. Here’s an example of its usage (taken, again, from the MicrosoftAjaxTemplates.js file):


As you can see, its use is slightly different from the _registerScript method. The _checkDependency method expects two parameters:

  • the name of the required dependency (supposed to be previously registered through the _registerScript method);
  • the name of the member/method that relies on tha dependency.

If the method can’t find the dependency registered, you’ll end up getting an exception which tells you something like “feature X requires script Y to work”. Pretty cool, right? (and way faster than taking a look at the error console and seeing that prop X isn’t defined, right?)

As you can see, you’ve got two options for checking dependencies: you should rely on the _registerScript method to register your script and enforce that all the third party JS lib files that support the basic features of your code are included in the page: on the other hand, you should use the _checkDependency method for ensuring that your component’s *optional* features work properly.

And this concludes the first post on the new MS AJAX lib. Stay tuned for more.