TechnoBuzz

A Techno Blog, mainly about Java

Speak Dojo

what is dojo?

Dojo is an Open Source JavaScript UI toolkit. It makes writing JavaScript easier, building great interfaces faster, and deploying dynamic UIs at scale much easier. The foundation of Dojo is “Dojo Base”, a single tiny library which contains Ajax, event handling, effects, blazing fast CSS queries, language utilities, and a lot more. On top of this Base, the rest of Dojo Core adds high-quality facilities for Drag and Drop, extended forms of Ajax and I/O, JSON-RPC, internationalization, and back-button handling.

You can use the div tag to define widget locations and Dojo will place the widget there either during page load or in response to events.

dojo book 0.4APIreference documentationDojo jot wikidojo toolkit home

JavaPassion PDF has 86 pages of info.

What is JSON?

(Using Dojo and JSON to Build Ajax Applications article ) – JSON is a Java library that helps convert Java objects into a string representation. This string, when eval()ed in JavaScript, produces an array that contains all of the information that the Java object contained. JSONObject class , JSONArray class “- Dojo provides an abstraction layer for invoking JSON-RPC requests”

json over xml?

IBM Paper: “JSON is built on two structures:

  1. A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence

JSON-RPC is a lightweight remote procedure call protocol in which JSON serializes requests and responses

The request is a single object with three properties:

  • method – A string containing the name of the method to be invoked.
  • params – An array of objects to pass as arguments to the method.
  • id – The request ID. This can be of any type. It is used to match the response with the request that it is replying to.

When the method invocation completes, the service must reply with a response. The response is a single object with three properties:

  • result – The object that was returned by the invoked method. This must be null, in case there was an error invoking the method.
  • error – An error object if there was an error invoking the method. It must be null, if there was no error.
  • id – This must be the same ID as the request it is responding to.

A notification is a special request that does not have a response. It has the same properties as the request object with one exception:

id – Must be null

Using Dojo and JSON to Build Ajax Applications article: Dojo libraries are organized in packages just like Java code. For this example, we will need to import two packages.

The dojo.io package contains classes that allow us to make HTTP requests using protocols such as XMLHTTPTransport.

The dojo.event package is designed to provide a unified event system for DOM and programmatic events.

The dojo.event.connect() method allows you to associate a handler for the onclick event for myButton:

function onLoad() {

   var buttonObj = document.getElementById("myButton");

   dojo.event.connect(buttonObj, "onclick",

          this, "onclick_myButton");

  }  function onclick_myButton() {

   var bindArgs = {

    url: "welcome.jsp",

    error: function(type, data, evt){

     alert("An error occurred.");

    },

    load: function(type, data, evt){

     alert(data);

    },

    mimetype: "text/plain",

    formNode: document.getElementById("myForm")

   };

   dojo.io.bind(bindArgs);

  }

The magical dojo.io.bind() function is where the power lies. It takes as argument bindArgs, an array of name/value pairs. In this example, we specify five pairs:

  1. url: The URL to make the request to.
  2. mimetype: The response type expected.
  3. load: Code to execute upon success.
  4. error: Code to execute upon error.
  5. formNode: The ID of the form whose fields to submit as parameters to the URL.

Once the call to dojo.io.bind(bindArgs) is made,depending on whether the request encountered any errors, either the load or error code is executed. Both load and error take three arguments:
type
: The type of function; it will always be set to load for load() and error for error().
data
: The response received. If mimetype is specified as text/plain, data contains the raw response. If, however, text/json is used, data contains the value of eval('(' + responseReceived + ')'), where responseReceived is what the call returned.

evt: The event object.

Advertisements

July 21, 2007 - Posted by | AJAX

No comments yet.

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: