<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tales from the Evil Empire : JSON</title><link>http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx</link><description>Tags: JSON</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Microsoft Ajax 4.0 Preview 4 now available</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/18/microsoft-ajax-4-0-preview-4-now-available.aspx</link><pubDate>Wed, 18 Mar 2009 07:49:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6973656</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6973656</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/18/microsoft-ajax-4-0-preview-4-now-available.aspx#comments</comments><description>&lt;p&gt;The Microsoft Ajax team made the &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645"&gt;fourth preview of the 4.0 version available on CodePlex&lt;/a&gt;. This is an important release because it enables the full client data story, complete with the ability to get changes back to the server automatically.&lt;/p&gt;  &lt;p&gt;Here’s a quick recap of some of the available features:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Getting a &lt;strong&gt;client representation of data&lt;/strong&gt; from an ADO.NET and REST data service.&lt;/li&gt;    &lt;li&gt;Rendering data on the client using &lt;strong&gt;templates&lt;/strong&gt;.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Declarative instantiation&lt;/strong&gt; of client components.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Live bindings&lt;/strong&gt;, enabling changes in the UI and in the data to be automatically propagated.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Command bubbling&lt;/strong&gt; for codeless wiring of events in template-driven controls.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Data identity&lt;/strong&gt; and association management for efficient and consistent client-server data exchanges.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Sending changes back&lt;/strong&gt; to ADO.NET and REST data services.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In a nutshell, it is probably the easiest way to build a data-driven client application. Check this out:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;peopleView&amp;quot; &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dataview&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;
     &lt;/span&gt;&lt;span style="color: #a31515"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;dataprovider&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ $create(Sys.Data.AdoNetDataContext,
                               {serviceUri: 'PeopleIKnow.svc'})}}&amp;quot;
     &lt;/span&gt;&lt;span style="color: #a31515"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;fetchoperation&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;PeopleIKnow&amp;quot;
     &lt;/span&gt;&lt;span style="color: #a31515"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;autofetch&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;true&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;legend&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{binding FirstName}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{binding LastName}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;legend&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;img code&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;if&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ Photo }}&amp;quot;
             &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ 'Images/' + Photo }}&amp;quot;
             &lt;/span&gt;&lt;span style="color: red"&gt;alt&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ FirstName + ' ' + LastName }}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ $id('firstName') }}&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editInPlace name&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{binding FirstName}&amp;quot;
               &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;inplace&amp;quot; &lt;/span&gt;&lt;span style="color: #a31515"&gt;inplace&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;cssclass&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editing&amp;quot;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ $id('lastName') }}&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editInPlace name&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{binding LastName}&amp;quot;
               &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;inplace&amp;quot; &lt;/span&gt;&lt;span style="color: #a31515"&gt;inplace&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;cssclass&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editing&amp;quot;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;saveButton&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Save&amp;quot; /&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This creates a DataView that queries the PeopleIKnow.svc ADO.NET data service, and repeats the markup in the div over the data. The legend contains two spans that will respond to live changes to the data ({binding FirstName and {binding LastName}). The image will only be rendered if there is a photo to show (code:if). It builds the image path using a simple JavaScript expression: {{ ‘Images/’ + Photo }}.&lt;/p&gt;

&lt;p&gt;The two input tags are augmented by a custom &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/11/20/building-a-neat-edit-in-place-behavior.aspx"&gt;edit&lt;/a&gt; &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/11/24/simplifying-the-edit-in-place-behavior.aspx"&gt;in place&lt;/a&gt; behavior (sys:attach=”inplace”) and are bound to the FirstName and LastName data columns so that any change to the value of the field will be propagated to everything that depends on the same data: the data itself of course but also the legend of the fieldset (see video below).&lt;/p&gt;

&lt;p&gt;The save button is hooked to the following handler:&lt;/p&gt;

&lt;pre class="code"&gt;$addHandler($get(&lt;span style="color: #a31515"&gt;&amp;quot;saveButton&amp;quot;&lt;/span&gt;), &lt;span style="color: #a31515"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;function&lt;/span&gt;() {
    $find(&lt;span style="color: #a31515"&gt;&amp;quot;peopleView&amp;quot;&lt;/span&gt;).get_dataProvider().saveChanges();
}, &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This handler is super-simple as it only has to call saveChanges on the data provider of the DataView. This is enough because any changes made in the input fields have been propagated to the client data model already, which tracked all changes and can build a simple JSON object to send back to the data service. Here is an example of the kind of JSON object that travels back to the server after I’ve changed Simon’s name through the UI:&lt;/p&gt;

&lt;pre&gt;{&lt;br /&gt;  &amp;quot;__metadata&amp;quot;:{&lt;br /&gt;    &amp;quot;uri&amp;quot;:&amp;quot;http://127.0.0.1:26402/Asp.Net_Ajax_Preview_4/&lt;br /&gt;           PeopleIKnow.svc/PeopleIKnow(1)&amp;quot;,&lt;br /&gt;    &amp;quot;type&amp;quot;:&amp;quot;PeopleIKnowModel.PeopleIKnow&amp;quot;&lt;br /&gt;  },&lt;br /&gt;  &amp;quot;ID&amp;quot;:1,&lt;br /&gt;  &amp;quot;FirstName&amp;quot;:&amp;quot;Simon&amp;quot;,&lt;br /&gt;  &amp;quot;LastName&amp;quot;:&amp;quot;Calvert&amp;quot;,&lt;br /&gt;  &amp;quot;Photo&amp;quot;:&amp;quot;simoncal.jpg&amp;quot;
}&lt;/pre&gt;

&lt;p&gt;Here’s the application running:&lt;/p&gt;

&lt;embed src="http://images.video.msn.com/flash/soapbox1_1.swf" width="432" height="364" id="m6a75m3o" type="application/x-shockwave-flash" allowFullScreen="true" allowScriptAccess="always" pluginspage="http://macromedia.com/go/getflashplayer" flashvars="c=v&amp;v=140e8d2a-8d01-49ab-b96f-77e219f93e40&amp;ifs=true&amp;fr=msnvideo&amp;mkt=en-US"&gt;&lt;/embed&gt;&lt;noembed&gt;&lt;br/&gt;&lt;a href="http://video.msn.com/video.aspx?vid=140e8d2a-8d01-49ab-b96f-77e219f93e40" target="_new" title="ASP.NET Ajax 4.0 Preview 4 "&gt;Video: ASP.NET Ajax 4.0 Preview 4 &lt;/a&gt;&lt;/noembed&gt;

&lt;p&gt;The source code can be downloaded from here (contains code licensed under &lt;a href="http://opensource.org/licenses/ms-pl.html"&gt;MS-PL&lt;/a&gt;):

  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Asp.Net_Ajax_Preview_4.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Asp.Net_Ajax_Preview_4.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Asp.Net_Ajax_Preview_4.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But wait, there’s more in stock for the next preview… I’ll post more details about some of those features in the following weeks… Stay tuned…&lt;/p&gt;

&lt;p&gt;&lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645"&gt;http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6973656" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx">JSON</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>JavaScript Behavior Sheets: an experiment</title><link>http://weblogs.asp.net/bleroy/archive/2009/01/18/javascript-behavior-sheets-an-experiment.aspx</link><pubDate>Sun, 18 Jan 2009 08:53:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6843536</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6843536</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/01/18/javascript-behavior-sheets-an-experiment.aspx#comments</comments><description>&lt;P&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; MARGIN: 0px 7px 0px 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title="(c) Bertrand Le Roy 2004" border=0 alt="(c) Bertrand Le Roy 2004" align=left src="http://weblogs.asp.net/blogs/bleroy/ZenShadows_7A57622D.jpg" width=164 height=244 mce_src="http://weblogs.asp.net/blogs/bleroy/ZenShadows_7A57622D.jpg"&gt; Here’s a little experiment. I’m really after feedback on this one as I’m trying to decide whether this is a good idea. It’s also entirely possible somebody else did this before. That would be good feedback too. Anyway, here it is.&lt;/P&gt;
&lt;P&gt;Despite its shortcomings, CSS has a number of features that make it very compelling. First, it decouples styling from markup. Second, its selector syntax is simple, yet reasonably powerful.&lt;/P&gt;
&lt;P&gt;So we have semantic markup on the one hand, and styles on the other hand, and the only coupling between the two is the selectors in the stylesheet.&lt;/P&gt;
&lt;P&gt;In Ajax applications, there is a third kind of entity in the mix, JavaScript behavior. There are of course ways to decouple the script behavior from the markup, which are usually referred to as &lt;A href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript" mce_href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript"&gt;unobtrusive JavaScript&lt;/A&gt;. &lt;A href="http://www.jquery.com/" mce_href="http://www.jquery.com"&gt;jQuery&lt;/A&gt; &lt;FONT face="Courier New"&gt;also &lt;/FONT&gt;introduced back in 2005 a way to associate script behavior with the DOM using the same selector syntax that CSS uses.&lt;/P&gt;
&lt;P&gt;But way before &lt;A href="http://jquery.com/" mce_href="http://jquery.com/"&gt;jQuery&lt;/A&gt;, &lt;A href="http://msdn.microsoft.com/en-us/library/ms532147.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms532147.aspx"&gt;Internet Explorer 5 enabled developers to specify behavior in stylesheets&lt;/A&gt;. You could do this for example:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #a31515"&gt;.hilite &lt;/SPAN&gt;{ &lt;SPAN style="COLOR: red"&gt;behavior&lt;/SPAN&gt;:&lt;SPAN style="COLOR: blue"&gt;url(hilite.htc) &lt;/SPAN&gt;}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This was a pretty neat idea at the time despite the challenges that came with it (in terms of performance for example), but it was never adopted by the other browsers despite having been &lt;A href="http://www.w3.org/TR/1999/WD-becss-19990804.html" mce_href="http://www.w3.org/TR/1999/WD-becss-19990804.html"&gt;submitted to W3C&lt;/A&gt;. The feature was never very widely used. There *is* &lt;A href="http://dean.edwards.name/moz-behaviors/overview/" mce_href="http://dean.edwards.name/moz-behaviors/overview/"&gt;a Firefox implementation of HTC behaviors&lt;/A&gt; but it doesn’t seem to have helped much in terms of adoption of this feature.&lt;/P&gt;
&lt;P&gt;And one thing that bothers me with that idea is that while it does decouple behavior from markup, it also &lt;EM&gt;couples&lt;/EM&gt; behavior to styling at least in location. Putting styles and behavior in the same file, in retrospect, looks like one step forward, and one step backwards. Sure, you could always use two separate files but the system did nothing to encourage you to do that.&lt;/P&gt;
&lt;P&gt;What I’m trying to do here is separate styling, markup *and* behavior, leverage selectors in a nice, declarative way &lt;EM&gt;à la&lt;/EM&gt; CSS and still work on all modern browsers. The coupling mechanism between style and markup (CSS selectors), is reasonably good and is already known by all Web developers so it remains a natural choice as jQuery showed clearly. Let’s see if one can express behavior in a way that is close to the already well-known CSS pattern.&lt;/P&gt;
&lt;P&gt;In this implementation, it is possible to add event handlers and Microsoft Ajax behaviors from a “JavaScript Behavior Sheet” which can be embedded in the page or be loaded from a separate file using a modified script tag:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;script &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="text/behavior" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="BehaviorSheet.jss"&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;script&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The contents of the tag or file is in the simple JSON notation for an object, omitting the curly braces:&lt;/P&gt;&lt;PRE class=code&gt;"input[type=text].nomorethanfive": {
    click: function(e) {
        alert("You clicked input #" + e.target.id);
    },
    "Bleroy.Sample.CharCount": {
        maxLength: 5,
        overflow: function(source, args) {
            $(source.get_element()).jFade({
                 property: 'background',
                 start: 'FFFF00',
                 end: 'FFFFFF',
                 steps: 25,
                 duration: 30
            });
        }
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The top-level entities that can be found in there are the CSS selectors (note the quotes that &lt;EM&gt;are&lt;/EM&gt; a departure from CSS notation but made the prototyping so much simpler). Each selector is associated with an object that contains event and object definitions.&lt;/P&gt;
&lt;P&gt;The event definitions consist in the event name and the handler to associate with it:&lt;/P&gt;&lt;PRE class=code&gt;click: function(e) {
    alert("You clicked input #" + e.target.id);
},&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The implementation of this feature uses the new &lt;A href="http://docs.jquery.com/Events/live#typefn" mce_href="http://docs.jquery.com/Events/live#typefn"&gt;live events from jQuery&lt;/A&gt;. The result of that definition is that clicking on any input of type text with the class “nomorethanfive” will display an alert giving the id of the input that was clicked. That is a pretty efficient way to hook up events to multiple elements…&lt;/P&gt;
&lt;P&gt;The behavior instantiation specifies the class to instantiate, “Bleroy.Sample.CharCount” and lists the properties, fields and events to set (here, the maxLength property is set to 5 and the overflow event is hooked to a function that flashes the element’s background yellow):&lt;/P&gt;&lt;PRE class=code&gt;"Bleroy.Sample.CharCount": {
    maxLength: 5,
    overflow: function(source, args) {
        $(source.get_element()).jFade({
             property: 'background',
             start: 'FFFF00',
             end: 'FFFFFF',
             steps: 25,
             duration: 30
        });
    }
}&lt;/PRE&gt;
&lt;P&gt;Here’s what the page looks like: &lt;BR&gt;&lt;/P&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:beb84b52-ccf2-4861-9ec4-3bb1f8e468bc class=wlWriterEditableSmartContent&gt;
&lt;DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-TOP: 0px" id=4314ce20-b9b6-441e-9466-068510fa6144&gt;
&lt;DIV&gt;&lt;A href="http://video.msn.com/video.aspx?vid=bd271e94-33e0-46fd-a09f-17c5ebe09f11" target=_new&gt;&lt;IMG style="BORDER-BOTTOM-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none" alt="" src="http://weblogs.asp.net/blogs/bleroy/videofbd13523437b_5803F39D.jpg" onload="var downlevelDiv = document.getElementById('4314ce20-b9b6-441e-9466-068510fa6144'); downlevelDiv.innerHTML = &amp;quot;&lt;div&gt;&lt;embed src=\&amp;quot;http://images.video.msn.com/flash/soapbox1_1.swf\&amp;quot; quality=\&amp;quot;high\&amp;quot; width=\&amp;quot;432\&amp;quot; height=\&amp;quot;364\&amp;quot; wmode=\&amp;quot;transparent\&amp;quot; type=\&amp;quot;application/x-shockwave-flash\&amp;quot; pluginspage=\&amp;quot;http://macromedia.com/go/getflashplayer\&amp;quot; flashvars=\&amp;quot;c=v&amp;amp;v=bd271e94-33e0-46fd-a09f-17c5ebe09f11&amp;amp;from=writer&amp;amp;mkt=en-US\&amp;quot; &gt;&lt;\/embed&gt;&lt;\/div&gt;&amp;quot;;" galleryimg="no"&gt;&lt;/A&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;I should point out that while the events will handle DOM mutations (such as adding new elements that match the selector) just fine, component instantiation won’t in this implementation, which is a limitation that is quite hard to work around in current browsers.&lt;/P&gt;
&lt;P&gt;So here it is. All the code for this is &lt;A href="http://weblogs.asp.net/blogs/bleroy/Samples/BehaviorSheet.zip" mce_href="http://weblogs.asp.net/blogs/bleroy/Samples/BehaviorSheet.zip"&gt;available from the link below&lt;/A&gt; (contains &lt;A href="http://www.gimiti.com/kltan/wordpress/?p=43" mce_href="http://www.gimiti.com/kltan/wordpress/?p=43"&gt;jFade&lt;/A&gt;, code licensed under &lt;A href="http://opensource.org/licenses/mit-license.php" mce_href="http://opensource.org/licenses/mit-license.php"&gt;MIT&lt;/A&gt;: &lt;A href="http://jquery.com/" mce_href="http://jquery.com/"&gt;jQuery&lt;/A&gt;, and code under &lt;A href="http://opensource.org/licenses/ms-pl.html" mce_href="http://opensource.org/licenses/ms-pl.html"&gt;MS-PL&lt;/A&gt;: &lt;A href="http://www.asp.net/ajax/" mce_href="http://www.asp.net/ajax/"&gt;Microsoft Ajax&lt;/A&gt; and my own code), with some tests written with &lt;A href="http://weblogs.asp.net/bleroy/archive/2008/12/09/really-simple-testing-for-javascript.aspx" mce_href="http://weblogs.asp.net/bleroy/archive/2008/12/09/really-simple-testing-for-javascript.aspx"&gt;RST.js&lt;/A&gt;. So what do you think?&lt;/P&gt;
&lt;P&gt;&lt;A title=http://weblogs.asp.net/blogs/bleroy/Samples/BehaviorSheet.zip href="http://weblogs.asp.net/blogs/bleroy/Samples/BehaviorSheet.zip" mce_href="http://weblogs.asp.net/blogs/bleroy/Samples/BehaviorSheet.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/BehaviorSheet.zip&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt; interestingly, the intent behind &lt;A href="http://code.google.com/p/reglib/"&gt;Reglib&lt;/A&gt; by &lt;A href="http://blogs.sun.com/greimer/"&gt;Greg Reimer&lt;/A&gt; is pretty close to this and he even uses the words “Behavior Sheets”.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt; Stuart Langridge did &lt;A href="http://www.kryogenix.org/code/browser/jses/" mce_href="http://www.kryogenix.org/code/browser/jses/"&gt;something very close to declarative events as they are done here&lt;/A&gt; &lt;EM&gt;back in November 2003&lt;/EM&gt;. Thanks to Clayton for pointing that out. One should note that the limitations of the time are no longer preventing the technique from reaching its full potential.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6843536" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Firefox/default.aspx">Firefox</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx">JSON</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>PDC 2008 ASP.NET AJAX Futures talk available online</title><link>http://weblogs.asp.net/bleroy/archive/2008/11/03/pdc-2008-asp-net-ajax-futures-talk-available-online.aspx</link><pubDate>Mon, 03 Nov 2008 18:10:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6718193</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6718193</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/11/03/pdc-2008-asp-net-ajax-futures-talk-available-online.aspx#comments</comments><description>&lt;P&gt;The full 83 minutes of &lt;A href="http://channel9.msdn.com/pdc2008/PC32/" mce_href="http://channel9.msdn.com/pdc2008/PC32/"&gt;my PDC talk&lt;/A&gt; are available on &lt;A href="http://channel9.msdn.com/" mce_href="http://channel9.msdn.com"&gt;the Channel 9 web site&lt;/A&gt;. You can &lt;A href="http://channel9.msdn.com/pdc2008/PC32/" mce_href="http://channel9.msdn.com/pdc2008/PC32/"&gt;watch the session online&lt;/A&gt; (using Silverlight) or download the video in a number of formats. &lt;A href="http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/PC32.pptx" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/PC32.pptx"&gt;Slides&lt;/A&gt; and &lt;A href="http://mschnlnine.vo.llnwd.net/d1/pdc08/ZIP/PC32.ZIP" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/ZIP/PC32.ZIP"&gt;source code for the demo&lt;/A&gt; are also available.&lt;/P&gt;
&lt;P&gt;&lt;A title="Watch the PDC 2008 ASP.NET AJAX Futures talk." href="http://channel9.msdn.com/pdc2008/PC32/" mce_href="http://channel9.msdn.com/pdc2008/PC32/"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title="PDC 2008 ASP.NET AJAX Futures" border=0 alt="PDC 2008 ASP.NET AJAX Futures" src="http://weblogs.asp.net/blogs/bleroy/image_49E75279.png" width=504 height=258 mce_src="http://weblogs.asp.net/blogs/bleroy/image_49E75279.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Watch online: &lt;BR&gt;&lt;A title=http://channel9.msdn.com/pdc2008/PC32/ href="http://channel9.msdn.com/pdc2008/PC32/" mce_href="http://channel9.msdn.com/pdc2008/PC32/"&gt;http://channel9.msdn.com/pdc2008/PC32/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Download &lt;A href="http://mschnlnine.vo.llnwd.net/d1/pdc08/mp4/PC32.mp4" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/mp4/PC32.mp4"&gt;MP4 / iPod&lt;/A&gt;, &lt;A href="http://mschnlnine.vo.llnwd.net/d1/pdc08/ZUNE/PC32.wmv" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/ZUNE/PC32.wmv"&gt;Zune&lt;/A&gt;, &lt;A href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV/PC32.wmv" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV/PC32.wmv"&gt;WMV&lt;/A&gt; or &lt;A href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/PC32.wmv" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/PC32.wmv"&gt;WMV-HQ&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Slides: &lt;BR&gt;&lt;A title=http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/PC32.pptx href="http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/PC32.pptx" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/PC32.pptx"&gt;http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/PC32.pptx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Demo code (contains MIT and MS-PL licensed JavaScript files): &lt;BR&gt;&lt;A title=http://mschnlnine.vo.llnwd.net/d1/pdc08/ZIP/PC32.ZIP href="http://mschnlnine.vo.llnwd.net/d1/pdc08/ZIP/PC32.ZIP" mce_href="http://mschnlnine.vo.llnwd.net/d1/pdc08/ZIP/PC32.ZIP"&gt;http://mschnlnine.vo.llnwd.net/d1/pdc08/ZIP/PC32.ZIP&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;A href="http://channel9.msdn.com/pdc2008/PC31/" mce_href="http://channel9.msdn.com/pdc2008/PC31/"&gt;Another talk you may be interested in is Stephen Walther's&lt;/A&gt;. Stephen did an amazing job explaining how jQuery and ASP.NET Ajax work and fit together. The talk is very accessible even if you have no prior knowledge of jQuery: &lt;A href="http://channel9.msdn.com/pdc2008/PC31/"&gt;http://channel9.msdn.com/pdc2008/PC31/&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6718193" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx">JSON</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/PDC/default.aspx">PDC</category></item><item><title>Client templates in MSDN Magazine</title><link>http://weblogs.asp.net/bleroy/archive/2008/10/05/client-templates-in-msdn-magazine.aspx</link><pubDate>Mon, 06 Oct 2008 04:48:16 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6658942</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6658942</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/10/05/client-templates-in-msdn-magazine.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/magazine/cc972638.aspx"&gt;My first full-length article&lt;/a&gt; in &lt;a href="http://msdn.microsoft.com/en-us/magazine/default.aspx"&gt;MSDN Magazine&lt;/a&gt; is out with the &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc992993.aspx"&gt;October issue&lt;/a&gt; and it’s about Microsoft AJAX &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/09/02/using-client-templates-part-2-live-bindings.aspx"&gt;client templates&lt;/a&gt;. Check it out…&lt;/p&gt;  &lt;p&gt;&lt;a title="http://msdn.microsoft.com/en-us/magazine/cc972638.aspx" href="http://msdn.microsoft.com/en-us/magazine/cc972638.aspx"&gt;http://msdn.microsoft.com/en-us/magazine/cc972638.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6658942" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx">JSON</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>Using client templates, part 1</title><link>http://weblogs.asp.net/bleroy/archive/2008/07/30/using-client-templates-part-1.aspx</link><pubDate>Thu, 31 Jul 2008 06:39:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6461783</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>36</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6461783</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/07/30/using-client-templates-part-1.aspx#comments</comments><description>&lt;P&gt;Last week, &lt;A href="http://weblogs.asp.net/bleroy/archive/2008/07/21/asp-net-ajax-4-0-codeplex-preview-1-available.aspx" mce_href="http://weblogs.asp.net/bleroy/archive/2008/07/21/asp-net-ajax-4-0-codeplex-preview-1-available.aspx"&gt;we shipped the first preview for the Ajax work we're doing in ASP.NET 4.0&lt;/A&gt; under the simple form of a simple script file (release and debug versions). This should show how much emphasis this release puts on the client-side. As a matter of facts, I'll use a plain HTML file here instead of an ASPX file to make it perfectly clear that everything here (except for the web service that provides the data) runs on the client.&lt;/P&gt;
&lt;P&gt;One of the scenarios we're trying to improve is updating parts of the page with new data without a postback (in other words, Ajax). That's a scenario you could implement in a number of manners. First, you could put an &lt;A href="http://msdn.microsoft.com/en-us/library/bb386454.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb386454.aspx"&gt;UpdatePanel&lt;/A&gt; control around your server rendering of the data and be done with it. That works, but is a little more chatty than you'd wish. It's all the chattier if you're dealing with a relatively large page with a lot of &lt;A href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx" mce_href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx"&gt;ViewState&lt;/A&gt; (in which case your small partial update pays a tax for the rest of the page that won't get updated). Even without &lt;A href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx" mce_href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx"&gt;ViewState&lt;/A&gt;, HTML isn't necessarily the most compact representation of your data.&lt;/P&gt;
&lt;P&gt;It's usually more efficient, at least in terms of network traffic, to transmit pure JSON data and do the rendering on the client. Now doing that without a proper template engine is clearly not the best use of your time. You could do it either with DOM APIs and a hard to maintain mess of &lt;A href="http://msdn.microsoft.com/en-us/library/ms536389.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms536389.aspx"&gt;document.createElement&lt;/A&gt; and &lt;A href="http://msdn.microsoft.com/en-us/library/ms535934(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms535934(VS.85).aspx"&gt;appendChild&lt;/A&gt; calls, or you could concatenate strings. The latter solution isn't much easier to maintain (how do you explain your HTML designer how to modify that stuff?) and is quite dangerous, in the same way that &lt;A href="http://weblogs.asp.net/bleroy/archive/2004/08/18/please-please-please-learn-about-injection-attacks.aspx" mce_href="http://weblogs.asp.net/bleroy/archive/2004/08/18/please-please-please-learn-about-injection-attacks.aspx"&gt;building a SQL query using string concatenation is&lt;/A&gt;. Nobody in their right mind does that anymore, right? I mean, ...right?&lt;/P&gt;
&lt;P&gt;A good template engine should be highly readable, as close to designable HTML as possible (bonus points if it &lt;STRONG&gt;*is*&lt;/STRONG&gt; a version of HTML that can be validated), it must have a good expression language and should perform well.&lt;/P&gt;
&lt;P&gt;Our engine is using markup that can be (but doesn't &lt;STRONG&gt;*have*&lt;/STRONG&gt; to be) valid XHTML, it uses JavaScript as the expression language (which means you don't have to learn yet another language), and we're quite pleased with how it performs. Let's have a look.&lt;/P&gt;
&lt;P&gt;The page we're building here is a very simple example of getting some data from a web service (names and photos) and rendering that on the client. Here's what the template looks like:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="peopleIKnow" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="sys-template"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;fieldset&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;legend&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;{{ FirstName }} {{ LastName }}&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;legend&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;&amp;lt;!--* if (Photo) { *--&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;img sys&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;:&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;src&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="{{ 'Images/' + Photo }}"&lt;BR&gt;             &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;alt&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="{{ FirstName + ' ' + LastName }}" /&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;&amp;lt;!--* } *--&amp;gt;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;fieldset&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;And here's what it renders like:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/blogs/bleroy/WindowsLiveWriter/TemplateRenderingSmall.PNG"&gt;&lt;IMG border=0 src="http://weblogs.asp.net/blogs/bleroy/WindowsLiveWriter/TemplateRenderingSmall.PNG"&gt;&lt;/A&gt;&lt;/P&gt;&lt;/A&gt;
&lt;P&gt;A few things to notice here.&lt;/P&gt;
&lt;P&gt;The syntax to insert data, be it into text nodes or attribute values, is {{ expression }}, where expression is any valid JavaScript expression. The most common case will be that you want to inject a column of the current data item. For that case, you won't have to use anything like the server-side's Eval("columnName") or anything like Container.DataItem.columnName or $T.row.columnName. You just use the column name and that's it: {{ FirstName }}. This will just inject the value of the FirstName column into the markup. And of course when you need something more complex, like a combination of columns or some additional formatting, you have all the power of JavaScript and the Ajax library at your disposal.&lt;/P&gt;
&lt;P&gt;The alt attribute of the image is one example of that: here, we're combining the first and last name. You could be tempted here to do something like alt="{{ FirstName }} {{ LastName }}". This won't work as the engine only accepts expressions for the full attribute and within text nodes (in the same way that on the server-side, &amp;lt;%# %&amp;gt; blocks have to be the full attribute).&lt;/P&gt;
&lt;P&gt;The src attribute is one of those attributes that you have to prefix with the system namespace sys if you want to bind them (together with id, checked and a few others). There are several reasons for that. First, the template is part of the initial markup of the page. If you used src as usual here, the browser would try to download an image named "{{ 'Images/' + Photo }}", which will of course result in an unnecessary network request, a 404 and some junk in your server logs. Second, an Internet Explorer bug prevents us from reading the binding expression from the DOM (the browser won't give you the actual value that you set, but will always give you what it thinks is the right value, in this case the server-resolved url, which is useless to us). Finally, it can help achieving XHTML compliance (for which, I know, there is a small violation in this template in that the imag tag is missing an actual src attribute, which you can fix -if you have to- by including a src="about:blank" attribute). Some attributes don't accept just any value in XHTML. To summarize, if binding an attribute directly doesn't work, chances are prefixing it with "sys:" just will.&lt;/P&gt;
&lt;P&gt;Last thing is this weird comment block, which we're using to mix JavaScript code into the markup. More precisely, we're doing conditional rendering of the image depending on the existence of the Photo field. There has been much debate over the use of a special comment-based syntax for this. We don't especially like using comments here as we agree that you should ideally be able to remove comments from markup without any impact. We decided to use them anyway because there wasn't a better solution that was still compatible with our XHTML constraint. We did consider admitting both &amp;lt;!--* *--&amp;gt; blocks and something like &amp;lt;script type="application/ms-template"&amp;gt; but that just seemed too verbose and it's rarely a good thing to have two solutions for exactly the same problem. It would be interesting to hear your comments on that, by the way...&lt;/P&gt;
&lt;P&gt;So how do you transform this template into markup? Depends whether you're a component developer, in which case you're going to use the template APIs directly from your component, or if you're an application developer. I'll focus on the latter in this post.&lt;/P&gt;
&lt;P&gt;To render the template with data, you simply instantiate a DataView control and feed it data:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;peopleIKnowView = $create(Sys.Preview.UI.DataView, {}, {}, {},&lt;BR&gt;                              $get(&lt;SPAN style="COLOR: #a31515"&gt;"peopleIKnow"&lt;/SPAN&gt;));
PeopleIKnow.GetPeople(&lt;SPAN style="COLOR: blue"&gt;function&lt;/SPAN&gt;(results) {
    peopleIKnowView.set_data(results);
});&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;In this code, we're using $create to create and initialize an instance of DataView on the "peopleIKnow" div tag, then we invoke the web service, and set the data property on the Dataview control from the callback function of the web service call. That's it.&lt;/P&gt;
&lt;P&gt;Next time, I'll get into more details into what really happens when you instantiate a template, and I'll give some debugging tips.&lt;/P&gt;
&lt;P&gt;The full code for this can be downloaded here: &lt;A href="http://weblogs.asp.net/bleroy/attachment/6461783.ashx"&gt;http://weblogs.asp.net/bleroy/attachment/6461783.ashx&lt;/A&gt;. Included script files are subject to the licenses for the &lt;A href="http://msdn.microsoft.com/en-us/asp.net/bb944808.aspx" mce_href="http://msdn.microsoft.com/en-us/asp.net/bb944808.aspx"&gt;Ajax Library&lt;/A&gt; and &lt;A href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=15511" mce_href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=15511"&gt;ASP.NET Ajax 4.0 CodePlex Preview 1&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Part 2 can be found here: &lt;A href="http://weblogs.asp.net/bleroy/archive/2008/09/02/using-client-templates-part-2-live-bindings.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2008/09/02/using-client-templates-part-2-live-bindings.aspx&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6461783" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/bleroy/attachment/6461783.ashx" length="106600" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Injection/default.aspx">Injection</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx">JSON</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>Dates and JSON</title><link>http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx</link><pubDate>Fri, 18 Jan 2008 20:47:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5617073</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>24</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=5617073</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx#comments</comments><description>&lt;P&gt;&lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON&lt;/A&gt; is a great data format and it's taken the Internet by storm for a number of good reasons. But because of a strange oversight in &lt;A href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" mce_href="http://www.ecma-international.org/publications/standards/Ecma-262.htm"&gt;the EcmaScript specs&lt;/A&gt;, there is no standard way of describing dates in &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON&lt;/A&gt;. There's been a lot of discussion on this topic and it still remains a problem today.&lt;/P&gt;
&lt;P&gt;In the &lt;A href="http://asp.net/ajax/" mce_href="http://asp.net/ajax/"&gt;Microsoft Ajax Library&lt;/A&gt;, we've tried a number of different approaches before we converged to the solution we're using today.&lt;/P&gt;
&lt;P&gt;The first thing we tried was to inject Date constructors in the &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON&lt;/A&gt; string. This is a (very) bad idea for a number of reasons. First, it simply does not conform to the &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON specs&lt;/A&gt;. Second, any &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON&lt;/A&gt; parser that validates its input before parsing it will cough on such a thing. Finally, it establishes a precedent: why would it be allowed for dates and not for arbitrary types? This would just defeat the purpose of &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The second approach, which is what most people settled on, is to adopt some string form of date and just interpret any string that conforms to this format as a date. We've &lt;A href="http://www.nikhilk.net/Entry.aspx?id=145" mce_href="http://www.nikhilk.net/Entry.aspx?id=145"&gt;been using "@1163531522089@" where the number is the number of milliseconds since January 1st 1970 UTC&lt;/A&gt; for a while (not super-readable) and &lt;A href="http://www.json.com/2007/10/24/lossless-json-dates/" mce_href="http://www.json.com/2007/10/24/lossless-json-dates/"&gt;some people are just using&lt;/A&gt; the &lt;A href="http://www.w3.org/TR/NOTE-datetime" mce_href="http://www.w3.org/TR/NOTE-datetime"&gt;ISO 8601&lt;/A&gt; format. These are both almost acceptable compromises (if everyone agrees on them) but have a fundamental flaw which is that there can be false positives: what if you want to serialize the "1997-07-16T19:20:30.45+01:00" &lt;EM&gt;string&lt;/EM&gt;, as a real string, not as a date?&lt;/P&gt;
&lt;P&gt;To be perfectly honest, &lt;A class="" href="http://www.json.com/json-schema-proposal/" mce_href="http://www.json.com/json-schema-proposal/"&gt;JSON Schema&lt;/A&gt; does solve the problem by making it possible to "subtype" a string as a date literal, but this is still work in progress and it will take time before any significant adoption is reached.&lt;/P&gt;
&lt;P&gt;Our current approach is using a small loophole in the &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON&lt;/A&gt; specs. In a &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;JSON&lt;/A&gt; string literal, you may (or may not) escape some characters. Among those characters, weirdly enough, there is the slash character ('/'). This is weird because there actually is no reason that I can think of why you'd want to do that. We've used it to our benefit to disambiguate a string from a date literal.&lt;/P&gt;
&lt;P&gt;The new format is "\/Date(1198908717056)\/" where the number is again the number of milliseconds since January 1st 1970 UTC. I would gladly agree that this is still not super readable, which could be solved by using &lt;A href="http://www.w3.org/TR/NOTE-datetime" mce_href="http://www.w3.org/TR/NOTE-datetime"&gt;ISO 8601&lt;/A&gt; instead.&lt;/P&gt;
&lt;P&gt;The point is that this disambiguates a date literal from a string that looks like the same date literal, while remaining &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;pure JSON&lt;/A&gt; that will be parsed by &lt;A href="http://www.json.org/" mce_href="http://www.json.org/"&gt;any standard JSON parser&lt;/A&gt;. Of course, a parser that doesn't know about this convention will just see a string, but parsers that do will be able to parse those as dates without a risk for false positives (except if the originating serializer escaped slashes, but I don't know of one that does).&lt;/P&gt;
&lt;P&gt;Here's how you'd encode a string that contains the same literal I described above: "/Date(1198908717056)/".&lt;/P&gt;
&lt;P&gt;Notice how a simple eval-based parser will just return the exact same string in both cases, but if you run a simple search for "\\/Date\((\d+)\)\\/" and replace with "new Date($1)" &lt;EM&gt;before&lt;/EM&gt; the eval (but &lt;EM&gt;after&lt;/EM&gt; validation), you'll get the dates right in the final object graph. The string I described above will just go through that filter undetected and will remain a string.&lt;/P&gt;
&lt;P&gt;We're pretty much satisfied with this solution to the date problem, but of course for the moment very few serializers and parsers support that convention. It would be great if this could become the consensus across the industry.&lt;/P&gt;
&lt;P&gt;I'd love to read any comments you may have on that subject.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5617073" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx">JSON</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item></channel></rss>