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

Function Class


The Function class (Figure A-7) provides basic features for functions and it is used throughout the library.

Figure A-7 Function

emptyMethod() Method

Static method that represents an empty function.

_validateParams() Method

Static method that validates a list of parameters against of list of JSON objects representing the validation rules.

Parameters

arguments – Array of parameters to be validated.

expectedParams – Array of JSON objects describing the validation rules for the arguments.

Returns

The method return an Error message if the arguments do not validate.

Remarks

Each JSON object contains a validation rule for a parameter. The JSON object is a dictionary of keys and values.

The list of keys that can be used is given in the following table:

Key

Description

name

The name of the parameter

type

The allowed type for this parameter (examples: String Array, Function, Sys.Component, etc.)

mayBeNull

Boolean value indicating whether this parameter can be passed as null or not

domElement

Boolean value indicating whether this parameter is a DOM element or not

integer

Boolean value indicating whether this parameter should have an integer value or not

optional

Boolean value indicating whether this parameter is optional or not

parameterArray

Boolean value indicating whether this parameter should be an Array or not

elementType

The allowed type for each element of an array (type must be Array)

elementMayBeNull

Boolean value indicating whether an array element could have null or not (type must be Array)

elementDomElement

Boolean value indicating whether each element of an array is a DOM element (type must be Array)

elementInteger

Boolean value indicating whether each element of an array should have an integer value (type must be Array)

The function returns an Error message if the parameters don’t validate and this error is typically thrown as shown below.

if (e) throw e;

This error could be caught and the appropriate measures can be taken programmatically. If not caught, the error will pop up in the debugging console of the browser.

Example

function Sys$_Debug$fail(message) {
/// <param name="message" type="String" mayBeNull="true"></param>
  var e = Function._validateParams(arguments, [
{name: "message", type: String, mayBeNull: true}
]);
    if (e) throw e;

The above example is extracted from the debug version of the library.

createDelegate() Method

Static method that creates a delegate for a given function and instance object.

Parameters

instance – the referenced object inside the method.

method – the method for which the delegate is created.

Returns

The method returns a delegate function.

Remarks

When the handler is an instance method and uses the this word in its body, we need to attach it as an event handler. We can use this method so that in the returned function this means the same thing as in the original instance context.

Example

myObject = function(){
  var handler = Function.createDelegate(this,this.myMethod);
}


myObject.prototype = {
  myMethod: function() { ...
  }
}

createCallback() Method

Static method that creates a callback function for a given method and an optional context.

Parameters

method – the method for which the callback is created.

context – the arbitrary context for calling the callback function.

Returns

The method returns a callback function.

Remarks

The context parameter is optional, but if it is omitted the callback function simply represents the original method with an additional level of indirection.

Even if the callback function is called without any parameters, the initial context will still be remembered.

Example

var myCallback = Function.createCallback(myMethod, "test");
function myMethod(message){
   alert(message);
}
myCallback();

The output of the above mentioned code snipped is an alert message with the test message.

Type Class

The Type class (Figure A-8) provides useful static methods for type-handling and type reflection, which helps implementing OOP features with JavaScript. It represent an alias for Function.

Figure A-8 Type