| By Kurt Cagle | Article Rating: |
|
| March 26, 2007 09:00 AM EDT | Reads: |
4,057 |
This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs for the special pre-order price, click here for more information. Aimed at everyone from enterprise developers to self-taught scripters, Real-World AJAX: Secrets of the Masters is the perfect book for anyone who wants to start developing AJAX applications.
Binding.xhtml
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<binding src="xincludeBinding.js" target="xinclude"/>
<script type="text/javascript" src="bindings.js" defer="no"/>
<style type="text/css"><![CDATA[
@import url('jsbinding.css');
]]></style>
</head>
<body>
<xinclude src="ArtOfTheFugue.xhtml#intro" id="sidebar" buttons="yes"/>
</body>
</html>
The first thing that may strike you as you're looking at this code is the fact that there is no inline scripting; the page as given is entirely XML driven. The presentation was handled by a simple CSS file included in the style block (which could have also been handled by a <link> element) (jsbinding.css):
jsbinding.css
#sidebar {
width:6in;
text-align:justify;
display:block;
background-color:lightYellow;
padding:5px;
border:inset 2px gray;
}
p {font-size:9pt;font-family:Arial;}
pre {font-size:8pt;}
.xincludeButton {}
.xincludeButtonSelected {font-weight:bold;}
.xincludeButtonBar {
width:100%;
background-color:lightBlue;
margin-left:-5px;
padding:5px;
padding-top:3px;}
.xincludeButtonBar_top {margin-top:-5px;}
.xincludeButtonBar_bottom {margin-bottom:-5px;}
.xincludeTitle {
font-size:18pt;
font-family:Arial;
width:100%;
background-color:#E0E0FF;
padding:5px;
margin-left:-5px;
margin-top:-5px;
margin-bottom:-5px;}
Most of the structure in the CSS defines the subordinate elements - the general display region, the button bar, each individual button - in terms of their visual appearances. Putting the visual (and other media) appearance in a separate CSS layer is important from a componentization layer, because this presentation layer makes it possible to reuse the component while providing very different interfaces as appropriate.
There are two pieces of "magic" that are going on in the background to make this particular XHTML file work. The first piece lays the foundation for creating bindings in general within XHTML, using the <binding> element to create an association between a binding definition and a given name (which may or may not be an element name). For instance, in the Binding.xhtml file, the element that bound the <xinclude> element to its definition was given by:
<binding src="xincludeBinding.js" target="xinclude"/>
However, in order for the page to understand how to use these binding elements, it was also necessary to invoke the script bindings.js so that the code to evaluate the binding definitions was given.
<script type="text/javascript" src="bindings.js" defer="no"/>
It's important here that the evaluation of this script is not deferred (thus defer="no") . The script needs to ensure that an event is added onto the window such that it will automatically perform the binding code once the window is initially loaded. This gets around the requirement of needing to place an onload event handler on the <body> element, keeping that particular slot open and making the code more modular.
The code for handling the binding is contained in bindings.js:
Bindings.js
window.addEventListener("load",function(){applyBindings()},true);
function applyBindings(){
var bindingset=document.getElementsByTagName("binding");
window.bindings = new Object;
for (var i=0;i != bindingset.length;i++){
var bindingNode =bindingset.item(i);
var bindingObj = {target:bindingNode.getAttribute('target'),
src:bindingNode.getAttribute('src'),
http:new XMLHttpRequest(),
code:null};
bindingObj.http.open("GET","" + bindingObj.src,false);
bindingObj.http.send(null);
var text = ""+bindingObj.http.responseText;
bindingObj.code = eval(text);
window.bindings[bindingObj.target] = bindingObj;
applyBinding(bindingObj.target);
}
}
function dump(obj){
var buf = "";
for (key in obj){
buf+=key+":"+obj[key]+"\n";
}
alert(buf);
}
function applyBinding(bindingTarget,boundNode){
if (boundNode != null){
var targets = [boundNode];
}
else {
var targets = document.getElementsByTagName(bindingTarget);
}
var binding = window.bindings[bindingTarget];
for (var index=0;index!=targets.length;index++){
if (boundNode !=null){
var target = boundNode;
}
else {
var target = targets.item(index);
}
target._bindingConstructor = binding.code.constructor;
var methods = binding.code.methods;
for (method in methods){
target[method] = methods[method];
}
var events = binding.code.events;
for (eventItem in events){
target.addEventListener(eventItem,events[eventItem],false);
}
target._bindingConstructor();
}
}
function getXPath(xpath,node){
var xslt ='<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:
xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output method="xml"/
><xsl:template match="/"><root><xsl:copy-of select="'+xpath+'"/></root></xsl:
template><xsl:template match="*"><root><xsl:copy-of select="'+xpath+'"/></root>
xsl:template></xsl:stylesheet>';
xsltDoc = (new DOMParser()).parseFromString(xslt,"text/xml");
var proc = new XSLTProcessor();
proc.importStylesheet(xsltDoc);
var doc = proc.transformToDocument(node);
return doc.documentElement.childNodes;
}
function xml_alert(node){
alert((new XMLSerializer()).serializeToString(node));
}
This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs, click here to order.
Published March 26, 2007 Reads 4,057
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Kurt Cagle
Kurt Cagle is a developer and author, with nearly 20 books to his name and several dozen articles. He writes about Web technologies, open source, Java, and .NET programming issues. He has also worked with Microsoft and others to develop white papers on these technologies. He is the owner of Cagle Communications and a co-author of Real-World AJAX: Secrets of the Masters (SYS-CON books, 2006).
- Kindle 2 vs Nook
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX



































