Dojo 1 Ajax
Dojo 1 Ajax
Dojo 1 Ajax
For live Ajax & GWT training, see training courses at http://courses.coreservlets.com/. t htt // l t /
Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial. Available at public JSP, tutorial venues, or customized versions can be held on-site at your organization.
C Courses d developed and t l d d taught b M t H ll ht by Marty Hall Courses developed and taught by coreservlets.com experts (edited by Marty)
Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF 1.x & 2.0, Ajax, GWT, custom mix of topics Ajax courses can concentrate on 1EE Training: http://courses.coreservlets.com/ or survey several Customized Java library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, Google Closure)
Servlets,Hibernate/JPA, EJB3, Ruby/Rails Spring, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Contact [email protected] for details Developed and taught by well-known author and developer. At public venues or onsite at your location.
Introduction
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Overview of Dojo
Core
Utilities for Ajax, CSS querying, DOM manipulation, cross-browser event handling, and general JavaScript programming Very similar to jQuery
Dijit j
Large set of rich GUI components Much more extensive than jQuery UI
D j X DojoX
Charts, graphs, vector graphics, fancy widgets Similar to the jQuery Plugins
7
Core Utilities
dojo.query("css selector")
Returns a NodeList of matching DOM elements.
Almost identical to $("css selector") in jQuery.
Example
dojo.xhrGet({ url: "address", load: responseHandler });
dojo.require("dojo.libraryName")
8
Installation
Unzip release file, creating 3 subfolders: dojo, dijit, dojox
Folders are quite large, but core file (dojo.js) is small. Other files are loaded on-the-fly based on dojo.require statements.
Online documentation
http://dojotoolkit.org/docs
9
Browser Compatibility
Firefox
Core: 1.5 or later (vs 2.0 for jQuery) Dijit: 2.0 or later (same as jQuery)
Internet Explorer
6.0 or later (same as jQuery)
Safari
3.1 or later (vs. 3.0 for jQuery)
Opera
Core: 9.6 or later (vs. 9.0 for jQuery) Dijit: not supported!
Chrome
1.0 or later
10
Industry Usage
11
Approximately 40% of matches to prototype and JavaScript were false positives such as build a prototype with JavaScript. So, discount the Prototype graph by about 40%.
Examples
dojo.query("#some-id")
Return 1-element set (or empty set) of element with id Simplest use and most common for Ajax (note the #!) use, # !) Can also use dojo.byId("some-id") to get single element
dojo.query("p")
Return all p elements
dojo.query(".blah")
Return all elements that have class="blah"
dojo.query("li b span.blah")
13
Return all <span class="blah"> elements that are inside b elements, that in turn are inside li elements
dojo.query("selector").addClass("name")
Adds CSS class name to each. Also removeClass toggleClass each removeClass,
dojo.query("selector").wipeOut().play()
Makes invisible. Also wipeIn, fadeOut, fadeIn, etc.
dojo.query("selector").html("<tag>some html</tag>")
Sets the innerHTML of each element element.
Must use dojo.require("dojo.NodeList-html");
dojo.byId("some-id").value
Returns value of input element. Notice no #. #.
Chaining
14
dojo.query("a").onclick(f1).addClass("name").forEach(f2)
15
16
18
Copied from Dojo download. To start with, you only need to load dojo/dojo.js. Other files brought in via dojo.require statements. j q
Written for next example in this chapter. Written for this example. Written for example in next chapter.
20
21
22
23
dojo.xhrGet: Basics
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Handler function gets response text. Response text is text considered a string unless you use handleAs option.
25
The preventCache option is not required, but is a convenient option when the same URL (including query data) yields different responses. This way, you dont have to send Cache-Control and Pragma headers from server.
This is the response text, but you can declare a second argument (ioArgs) if you want the response object (XmlHttpRequest) and other information. Also note that the latest Firefox does not let you pass native functions here, so you cannot use alert instead , y of showAlert for the success parameter.
28
29
30
dojo.xhrGet : Results
31
Dojo support
dojo.addOnLoad(function() {});
Function runs after the DOM is loaded, but does not wait , for images, as with window.onload Use this approach to set up all event handlers
33
34
35
Ajax Functions
dojo.xhrGet({options})
Make a GET request
dojo.xhrPost({options})
M k a POST request Make
dojo.xhrPut, dojo.xhrDelete
Make a PUT or DELETE request Unsupported on many servers
dojo.rawXhrPost, dojo.rawXhrPut j , j
Sends a POST or PUT request, but lets you provide the raw data for the body of the request
37
Overview
dojo.xhrGet({ url: , load: , content: });
Content value is an object: query string gets built out of property names and URL-encoded property values
O end of URL f ajax.xhrGet; On d f for j hG t in POST body for dojo.xhrPost See later example for building the string automatically using the form option Works identically to parameters option in Prototype or data option in jQuery data
Examples
dojo.xhrGet({ content: { param1: "foo bar!", j ({ p , param2: "baz"}});
38
39
function showParams1() { dojo. dojo.xhrGet({ Get({ url: "show-params.jsp", content: { param1: "foo", p param2: "bar" }, load: showAlert }); }
The preventCache option is not used since the same data always results in the same response.
40
41
42
dojo.xhrGet: Options
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Overview
Options (almost) always used: url, load
dojo.xhrGet({url: "some-address", load: someFunc});
load is not strictly required; you might want to just fire off some data to the server and not display anything p y y g Options apply equally to dojo.xhrPost and others
Options
Name content t t Description
Data t D t to send t server, in the form on an object with param names d to i th f bj t ith and raw (non-escaped) param values. The object property names become request param names and property values get URLencoded and become request param values. & and = inserted automatically. Sent in the appropriate place depending on t ti ll S t i th i t l d di whether it is GET or POST. Function to be called if request fails due to server error, expired timeout, or exception thrown from main response handler. Function gets passed 2 args: the data returned from the server (formatted according to the handleAs property), and an ioArgs object. The ioArgs object has many properties, but most useful are args, query, url, and xhr. Id of a form whose fields will be serialized to form the query data. Function to be called whether or not request was successful. More common t use separate load and error properties. M to t l d d ti The format in which to pass the response to the handler function. Legal values are text, json, xml, json-comment-optional, jsoncomment-filtered, and javascript.
Default Empty E t
error
None
Options (Continued)
Name headers h d load Description
Extra E t HTTP request headers to be sent to the server. th d t b t t th Function to be called if request succeeds. Function gets passed 2 args: the data returned from the server (formatted according to the handleAs property), and an ioArgs object. The ioArgs object p p y), g j g j has many properties, but most useful are args, query, url, and xhr. use GET and you could get different responses back from the same data If true then the URL is appended with a data. true, dojo.preventCache parameter with a timestamp that changes with each request. Should the request be synchronous? Use synchronous requests with caution since they lock up the browser. Timeout in milliseconds. If request takes longer, the error handler will be called instead of the load handler. The address to req est Sho ld be a relati e URL request. Should relative URL.
preventCache Is browser prohibited from caching the page? Set to true if you
false
46
Prototype
function ajaxResult(address, resultRegion) { new Ajax.Updater(resultRegion, address); j p g }
Dojo
N explicit support for content-centric Ajax. Response No li it tf t t t i Aj R handler uses html to do the insert.
Ext-JS
48
Sh t t Shortcut
See upcoming section on form property
49
50
53
54
jQuery
function ajaxResult(address, resultRegion) { $(resultRegion).load(address); }
55
Syntax
dojo.xhrGet({ url: , load: , form: "some-id" }); j G ({ , ,
Note: no # before the id
Advantages
One simple option, no matter how many input elements Only takes values of active elements (e.g., unchecked radio buttons or checkboxes are ignored) Giving names to input elements is familiar to HTML developers
57
58
60
61
Approach
Server
Returns JSON object with no extra parens. E.g.:
{ cto: "Resig ", ceo: "Gates ", coo: "Ellison" }
Response handler
Receives JavaScript data as first argument. No need for parsing or eval. Must build HTML from result. E.g.: i l M b ild f l E
function handler(companyExecutives) { dojo.query("#some-id") .html("<b>Chief Technology Officer is " + companyExecutives.cto + "</b>"); }
63
Strings
64
function showNumberList(jsonData) { var list = makeList(jsonData.fg, jsonData.bg, jsonData.fontSize, jsonData fontSize jsonData.numbers); int dojo.query("#result3").html(list); Array of doubles }
66
67
68
Notes
No enclosing parens. Dojo will wrap in parens and then pass to eval (or equivalent functionality) eval Types
fg and bg: Strings f tSi fontSize: i t int numbers: Array of doubles
69
71
Wrap-up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Usage in industry
Leader: jQuery 2nd tier: Ext-JS, Dojo, YUI, Prototype, Scriptaculous, GWT
Rich GUIs
Leaders: Ext-JS, YUI, Dojo 2nd tier: jQuery UI GWT UI,
Looking ahead
All these entries are likely to c ge s g c change significantly y Lurking on the horizon: Google Closure library
Familiarity to JS developers
73
Lowest: GWT
Mastering Dojo
b C i Ri k et al by Craig Riecke l
http://dojotoolkit.org/docs
Moderately complete Moderately well organized Large number of explicit examples
74
Summary
Assigning event handlers programmatically
dojo.addOnLoad(function() { dojo.query("#some-id").onclick(someFunction); });
75
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.