MSDN Magazine - December 2007 - (Page 35) Inside the Microsoft AJAX Library DINO ESPOSITO T here’s an unavoidable connection between AJAX and JavaScript that is implied by the AJAX acronym, which stands for Asynchronous JavaScript and XML. JavaScript, though, is a simple programming language whose foundation was laid out quite a few years ago when object-oriented programming was considered overkill for Web pages. At the same time, principles of dynamic languages were considered too academic to be fully implemented in a broadly used programming language. As a result, JavaScript contains a bit of everything: it supports some object-oriented semantics in a similar fashion as more fully object-oriented language like C#, and it has a number of dynamic binding features like today’s Ruby or Python. JavaScript is, however, the language that zillions of Web pages use to make displayed pages more interactive rather than a static composition of text, tables, and graphics. What is familiarly called JavaScript is actually the browser’s implementation of the standardized ECMAScript language. You use the language primarily to embed functions in HTML pages that glue together a user’s actions with elements in the page. The elements in the page—buttons, textboxes, and so on —are modeled after the schema defined in the Document Object Model (DOM). Using JavaScript, you can programmatically rename, edit, add, or delete elements in the displayed document and handle any events fired by such elements. In addition, you can perform any browser-specific action, such as opening or popping up a new window or—what makes AJAX so special—invoke the popular XMLHttpRequest object and place asynchronous calls to a remote URL. The key fact to bear in mind is that JavaScript and the DOM, by themselves, no longer provide developers with the full programming power needed in the current age of the Web. They represent a kind of minimal toolbox to work with. To craft good AJAX pages, more is needed, such as a rich client-side JavaScript library. A big benefit of AJAX is the ability to run more of a site’s code on the client, thus enabling the browser to respond quickly to user actions. Typical AJAX actions, though, go far beyond simply switching images when the mouse rolls over an element or displaying a pop-up window with a menu and toolbar. AJAX actions include client-side data validation, advanced user interface features such as floating menus, drag and drop, pop-up panels, and sophisticated input. AJAX actions also include dispatching requests to the server in an out-of-band way that bypasses the classic model of replacing the entire page. Whatever AJAX platform you want to consider must be backed by a rich JavaScript library. The library typically contains at least a ready-to-use set of client-side user interface gadgets and an application model with events that define a lifecycle for each loaded document. In ASP.NET AJAX 1.0 and in the AJAX platform integrated with ASP.NET 3.5, the JavaScript library of choice is the Microsoft® AJAX Library. Introducing the Microsoft AJAX Library The Microsoft AJAX Library consists of two related parts: JavaScript language extensions and a sort of base class library to provide predefined services and tools to developers. Although the notion of an object is pervasive, JavaScript by itself can’t be presented as an object-oriented language, as it fails to fully and natively implement all of the three pillars of object-oriented programming: inheritance, polymorphism, and encapsulation. There’s a bit of inheritance you can get through object prototypes and there’s a bit of encapsulation you obtain via closures. So the Microsoft AJAX Library first endows the language with more powThe closure model delivers erful tools and then proan object that has all of its ceeds with the definition of new classes and programmembers entirely defined ming tools. in the same overall context, The Microsoft AJAX much like a C# class. Library itself is self-contained, written in JavaScript, and stored in a couple of .js files. This means that any programming environment that accepts JavaScript can successfully use the Microsoft AJAX Library provided that the constituent files are properly referenced. There are two main files involved in the Microsoft AJAX Library: MicrosoftAjax.js and MicrosoftAjaxWebForms.js. MicrosoftAjax.js defines language extensions supported by the Microsoft AJAX Library including namespaces, interfaces, enums, and inheritance. MicrosoftAjaxWebForms.js defines the partial rendering engine and the whole network stack. In an ASP.NET AJAX application, you don’t need to reference or embed any of these two files directly. This is one of the tasks that the ScriptManager control accomplishes for you: This column is based on a prerelease version of ASP.NET 3.5 and the Microsoft AJAX Library. All information herein is subject to change. december2007 35 http://ASP.NET http://ASP.NET http://ASP.NET http://ASP.NET
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.