Escolar Documentos
Profissional Documentos
Cultura Documentos
Relevance, LLC
Table of Contents
Ajax Architecture................................................................................................................................................1
License and Usage..................................................................................................................................2
Agenda....................................................................................................................................................3
What's Wrong With the Web?................................................................................................................3
What is Ajax?..........................................................................................................................................4
A Bit More Detail, Please.......................................................................................................................4
Ajax is a Client-Tier State of Mind.........................................................................................................5
What is Ajax For? -- Some Examples.....................................................................................................5
Position 1: This Sh** Ain't New!...........................................................................................................6
Position 2: Holy Nth Tier, Batman!........................................................................................................7
The New Client Tier...............................................................................................................................7
Both Positions are Right!........................................................................................................................8
Message to Conservative Managers........................................................................................................8
Message to Progressive Managers..........................................................................................................9
A Pragmatic Start....................................................................................................................................9
The Zip Code Example.........................................................................................................................10
Zip Code Step 1: Plain Old Web..........................................................................................................10
Zip Code Step 2: Add DOM Ids...........................................................................................................11
Zip Code Step 3a: Add XHR................................................................................................................12
Zip Code Step 3b: Bind Event Handler................................................................................................12
Zip Code Step 4: Add XHR Callback...................................................................................................13
Where Are We?.....................................................................................................................................13
Time To Get Some Tools......................................................................................................................14
Why Firefox?........................................................................................................................................14
The JavaScript Shell.............................................................................................................................15
JavaScript Shell in Action.....................................................................................................................16
Firebug..................................................................................................................................................17
Firebug in Action..................................................................................................................................18
Web Developer Toolbar........................................................................................................................19
Web Developer Toolbar in Action........................................................................................................20
Prototype...............................................................................................................................................20
Zip Code Step 5: $ Method...................................................................................................................21
Zip Code Step 6: Event.observe............................................................................................................21
Zip Code Step 7: Ajax.Request.............................................................................................................22
Zip Code Step 8: Error Handling..........................................................................................................22
Zip Code Step 9: Progress Indicator.....................................................................................................24
Scriptaculous.........................................................................................................................................25
Zip Code Step 10: Fade Effect..............................................................................................................25
Zip Code Step 11: Autocomplete..........................................................................................................26
Zip Code Step 12: Autocomplete is Mostly CSS.................................................................................27
Architectural Decisions.........................................................................................................................28
What to Send on the Wire?...................................................................................................................28
Setting innerHTML/View-Centric Ajax...............................................................................................29
Polling with View-Centric Ajax...........................................................................................................29
View-Centric Polling: Lessons Learned...............................................................................................30
JavaScript eval/Code-Centric Ajax.......................................................................................................30
JSON (etc.) / Model-Centric Ajax........................................................................................................31
Polling with Model Centric Ajax..........................................................................................................31
What Library to Use?............................................................................................................................32
Prototype's Role....................................................................................................................................33
i
Table of Contents
Ajax Architecture
Scriptaculous's Role..............................................................................................................................34
Dojo: A Full Stack................................................................................................................................35
Avoiding Ajax Pitfalls..........................................................................................................................36
The Back Button Issue..........................................................................................................................37
Back Button Solutions..........................................................................................................................38
Bookmarking........................................................................................................................................38
Bookmarking Solution..........................................................................................................................39
Link to this Page (local.google.com)....................................................................................................40
References.............................................................................................................................................40
Projects Cited.........................................................................................................................................40
ii
Ajax Architecture
A Relevance/Codecite Presentation
Copyright 2005 Relevance, LLC
Ajax applications have unique architectural challenges and opportunities. This presentation will show you
how to take advantage of the Ajax's strengths, and work around its quirks.
We'll start with an overview of Ajax, and then dive right into an extended example where we add Ajax to an
existing web application. Along the way we'll cover several tools that we use to aid in Ajax development:
We'll also look at two popular JavaScript Ajax libraries: Prototype and Scriptaculous.
With the example application under our belts, we'll move to a discussion of Ajax architectural questions,
including:
Ajax Architecture 1
License and Usage
This presentation is Copyright 2005-6, Relevance LLC. You may use any code you find here, subject to the
terms below. If you want to deliver this presentation, please send email to contact@relevancellc.com for
permission and details.
Sample code associated with this presentation is Copyright (c) 2005-6 Relevance, LLC
(www.relevancellc.com), unless otherwise marked. Code citations from other projects are subject to the
license(s) appropriate to those projects. You are responsible for complying with licenses for code you use.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
A Pragmatic Start
• Add Ajax on the Edges
• Stay Degradable
• Wait for MVC Frameworks to Mature
Why Firefox?
• As Good In Most Ways
• Better In Some
• Plugin Community is Compelling
Prototype
• Core Support for Dynamic Web Apps
• Used by Scriptaculous and Rico
• Driven and Inspired by Ruby on Rails
• Simple and Elegant
function updatezip(request) {
var data = request.responseXML;
$("city").value = data.getElementsByTagName("city")[0].textContent;
$("state").value = data.getElementsByTagName("state")[0].textContent;
}
function checkzip(zip) {
new Ajax.Request("checkzip", {
method: "get",
parameters: "zip=" + zip,
onSuccess: updatezip
});
}
function updatezip(request) {
var data = request.responseXML;
if (!data || !data.getElementsByTagName("city")[0]) {
badzip();
} else {
$("city").value = data.getElementsByTagName("city")[0].textContent;
$("state").value = data.getElementsByTagName("state")[0].textContent;
Element.remove("err");
}
}
function badzip() {
new Insertion.After("table", "<div id='err'>Zip Not Recognized</div>");
}
function checkzip(zip) {
Element.show("checking_zip");
new Ajax.Request("checkzip", {
method: "get",
parameters: "zip=" + zip,
onSuccess: updatezip,
onFailure: badzip,
onComplete: function() {
Element.hide("checking_zip");
}
});
}
Bookmarking
• Ajax Lets Users Get "Between" Pages
• Where Should Bookmarks Point?
References
Projects Cited
• Rails Exploration Application
• Pragmatic Chat Sample Application