Book Image

Microsoft AJAX Library Essentials: Client-side ASP.NET AJAX 1.0 Explained

Book Image

Microsoft AJAX Library Essentials: Client-side ASP.NET AJAX 1.0 Explained

Overview of this book

Microsoft AJAX Library Essentials is a practical reference for the client-side library of the ASP.NET AJAX Framework 1.0, and a tutorial for the underlying technologies and techniques required to use the library at its full potential. The main goal of this book is to get you comfortable with the Microsoft AJAX Library, a huge set of functions that can be used for developing powerful client-side functionality.Beginning with a hands-on tour of the basic technologies associated with AJAX, JavaScript, XMLHttpRequest, JSON, and the DOM, you'll move on to a crash course in the Microsoft AJAX tools. You will learn, through numerous step-by-step exercises, how to create basic AJAX applications, how the object-based programming model of JavaScript works, and how Microsoft AJAX Library extends this model. You'll understand the architecture of the Microsoft AJAX components, how they all fit together, and exactly what they can do for you. Then you will learn how to use the Microsoft AJAX Library in your web projects, and a detailed case study will walk you through creating your own customized client components. At every stage of your journey, you'll be able to try out examples to illuminate the theory, and consolidate your understanding. In addition to learning about the client and server controls, you'll also see how to handle errors and debug your AJAX applications.To complement your new found skills, the book ends with a visual reference of the Microsoft AJAX Library namespaces and classes, including diagrams and quick explanations for all the classes mentioned in the book, providing an invaluable reference you will turn to again and again.
Table of Contents (14 chapters)
Copyright
Credits
About the Authors
About the Reviewers
Preface

Conventions


In this book, you were first introduced to class diagrams in Chapter 5. There you learned that unlike in C#, several conventions need to be used in order to implement typical OOP concepts using JavaScript code. Class diagrams use different conventions for the different types of items they describe, such as class fields, properties, methods, and events.

Fields are variables of primitive types and can be accessed directly using a class instance. Properties are mechanisms where the values of class fields can be accessed or altered only through getter and setter functions. This restriction is by convention only; the fields that store property values can be accessed directly, but the convention requires using the getter and setter methods.

The property’s field that internally store its value is declared with a leading underscore (_). The getter and setter methods are prefixed with “get_” and “set_” followed by the property name. For example, a property called name would be implemented using JavaScript code like this:

this._name = myvalue;
...
get_name: function()
{
   return this._name;
},

set_name: function(value)
{
 .  this._name = value;
}

Methods are functions inside a class. The term comes from the OOP world, but in the case of JavaScript, method and function can often be used interchangeably.

Events represent notifications that an action has occurred. The implementation for events in JavaScript is very similar to the one chosen on the server-side .NET platform.

The JavaScript implementation for an event named change looks like this:

this._events = new Sys.EventHandlerList();
...


// register a change event handler
add_change: function(handler) {
  this.get_events().addHandler("change", handler);
},

// unregister a change event handler
remove_change: function(handler) {
  this.get_events().removeHandler("change", handler);
}

When an event is raised, one or more functions, called handlers, can be invoked in response. Handlers can be registered and deregistered from an event. Each event handler would typically have a signature like the following:

function MyHandler(source, eventArgs)

Here, source represents the object that raised the event, and eventArgs contains an object derived from Sys.EventArgs which contains the parameters sent to our event. This strongly resembles the .NET style of event handlers.

The diagrams in this book have been created using the Class Diagram feature of Visual Studio 2005 Team System Edition. Note, however, that this feature doesn’t actually work with JavaScript—to create class diagrams, we created C# classes that correspond to the Microsoft AJAX Library functions. The mentioned conventions were used to port the JavaScript code to C#. Here are a few examples:

  • Unlike public methods, private (by convention) JavaScript methods are named with a leading underscore. See Figure A-1.

    Figure A-1 Public and private methods

  • JavaScript properties (by convention) are declared as C# properties (see Figure A-2); read-only JavaScript properties are represented as C# properties having only a getter (see Figure A-3).

    Figure A-2 Read-only property

    Figure A-3 Read-only property

  • Private (by convention) JavaScript fields are represented as C# fields with leading underscore(s)—see Figure A-4; all public Javascript fields are mapped to public C# fields (see Figure A-5).

    Figure A-4 Private fields

    Figure A-5 Public field

  • All the events (by convention) exposed by JavaScript objects are mapped to C# events; the classic EventHandler in C# is replaced by convention by Function (see Figure A-6).

    Figure A-6 Event