The new script loader object – part IV

In part three, we saw that we even have intellisense for requiring on-demand download of the necessary files for a specific feature or script. Even though that will probably be its most common use, you can also use the script loader object for downloading scripts which haven’t previously been “defined” through the Sys.loader.defineScripts method. In these scenarios, the loadScripts method is your friend!

Once again, we’ll build in the previous example and we’ll update it so that we use this method for ensuring proper loading of the required scripts:

<head>
    <title></title>
    <script src="Scripts/MicrosoftAjax/start.debug.js" type="text/javascript"></script>
</head>
<body>
    <input type="button" value="invoke B" id="bt" />
</body>
<script type="text/javascript">
    Sys.onReady(function() {

        function loadAndCallB() {

                Sys.loader.loadScripts( ["a.js", "b.js"],
                                        function(){ B(); } );
        }
        var bt = Sys.get("#bt");
        if (bt.attachEvent) {
            bt.attachEvent("onclick", loadAndCallB);
        }
        else {
            bt.addEventListener("click", loadAndCallB, false);
        }
    });
</script>

The code is similar, but there are some interesting differences:

  • in this case, you need to pay attention to the loading order of the scripts. Since you haven’t previously defined them (through the defineScripts method),the loader object can’t really understand that it needs to load script A before running the callback method on B. Even if you modify script b.js so that its registerScript method specifies the dependency in A,you’ll still need to have previously defined (ie, called the defineScripts method for) A so that it understand the execution dependency between A and B;
  • the loadScripts method expects an array of urls, a callback function (will be invoked when all the scripts have been downloaded) and a custom context which will be passed to the callback function (we didn’t use this in the previous sample);
  • the loadScripts method is smart enough for not doing duplicated downloads. That means that the second click on the button will simply redirect to the callback function (without downloading the scripts again).

And I guess this sums it up for now. We’ve already seen so many features introduced by preview 6 and there are still a ton of them to talk about. I guess this will keep me occupied in the next couple of days. Stay tuned for more on MS AJAX.

Advertisements

~ by Luis Abreu on October 16, 2009.

3 Responses to “The new script loader object – part IV”

  1. Is it possible to use CDN for Preview 6?

    I was using :

    But it is not recognizing the sys.create.dataView object.

  2. you”re using the wrong ulr…

    for preview 6, the path ishttp://ajax.microsoft.com/ajax/beta/0910/…

  3. One admits that life is high priced, nevertheless some people require cash for various issues and not every one earns enough money. Thus to receive fast loans and just commercial loan should be a proper way out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: