<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/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>LA.NET [EN] : Javascript</title><link>http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx</link><description>Tags: Javascript</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>undefined and NaN changes are no longer permitted in ECMAScript 5</title><link>http://msmvps.com/blogs/luisabreu/archive/2011/01/03/undefined-and-nan-changes-are-no-longer-permitted-in-ecmascript-5.aspx</link><pubDate>Mon, 03 Jan 2011 12:29:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1785588</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1785588</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2011/01/03/undefined-and-nan-changes-are-no-longer-permitted-in-ecmascript-5.aspx#comments</comments><description>&lt;p&gt;Happy New Year to everyone! After a small vacation period, I’m back :) &lt;/p&gt;  &lt;p&gt;Now that the HTML book has gone to revision (more about that in future posts), I guess it’s time to start posting some useful tips in this blog. One of my new year’s resolution is to pay much more attention to JavaScript and that’s why I’ve chose this topic for this year’s first post. If you’ve been reading my blog, you’ll probably recall an old &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/09/02/don-t-change-nan-and-undefined-values.aspx"&gt;post&lt;/a&gt; where I advert for the possibility that anyone can redefine the global undefined and NaN variables. Since this might end up producing some very undesirable effects, I’m really happy to find out that &lt;a href="http://en.wikipedia.org/wiki/ECMAScript"&gt;ECMAScript 5&lt;/a&gt; (ie, the JavaScript specification) has forbidden this type of information. &lt;/p&gt;  &lt;p&gt;To be more precise, the value properties Nan, Infinity and undefined introduced by the Global Object can’t be changed or configured, nor can they be enumerated in a for–in enumeration. What does that mean? Take a look at the following sample:&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:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c95060c6-0cda-4398-93b0-ce216d5f53d0" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background-color:#000000;max-height:300px;overflow:auto;padding:2px 5px;"&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;DOCTYPE&lt;/span&gt;&lt;span style="color:#f3f3f3;"&gt; &lt;/span&gt;&lt;span style="color:#fdf8b9;"&gt;html&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#6afd51;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;html&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;  &lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;head&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;    &lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;title&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#f3f3f3;"&gt;Testing undefined assignment&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;title&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;    &lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;script&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;      alert(undefined);&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;      undefined = &lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;quot;hi&amp;quot;&lt;/span&gt;&lt;span style="color:#f3f3f3;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;      alert(undefined);&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;    &lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;script&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;  &lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;head&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#f3f3f3;"&gt;  &lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;body&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;body&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#6afd51;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a2c4fd;"&gt;html&lt;/span&gt;&lt;span style="color:#6afd51;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Now, if you run the previous code in IE9 or in FF 4 (or even in Safari 5), you’ll end up getting the string undefined twice. Notice that you don’t end up getting an exception. You won’t simply be able to change the value…it’s as if you never run the undefined assignment…&lt;/p&gt;  &lt;p&gt;Unfortunately, not all browsers are compatible with this new behavior, but I’m thinking that it won’t take long until all of them are compatible with the specs. And that’s it for now. Stay tuned for more&amp;#160; JavaScript posts.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1785588" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Oh damn! Can’t you see that’s a constructor?</title><link>http://msmvps.com/blogs/luisabreu/archive/2010/09/22/oh-damn-can-t-you-see-that-s-a-constructor.aspx</link><pubDate>Wed, 22 Sep 2010 18:05:53 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1778571</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1778571</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2010/09/22/oh-damn-can-t-you-see-that-s-a-constructor.aspx#comments</comments><description>&lt;p&gt;Even though I’ve been in Silverlight/C#/WCF land in these last months, the truth is that I’ve always been a JavaScript aficionado. In fact, if you’ve been reading my blog, you’ll probably recall a small &lt;a href="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx"&gt;series&lt;/a&gt; I’ve done on it a few months ago(if not, then don’t worry: you can still read it &lt;a href="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx"&gt;here&lt;/a&gt;). After speaking with a friend today, I think I might have missed a cool advice on how to define constructors in JavaScript.&lt;/p&gt;  &lt;p&gt;As we all know, JavaScript OO programming is a little different from what we’re used to in traditional languages like C# or Java. You see, you can’t really reuse the same style of programming because things work in a completely different way in JavaScript land. Anyway, I still believe that it’s one of the coolest, more flexible languages you can use&amp;#160; (provided you don’t try to write JavaScript a la C# or Java). I’m not really getting into details here, but there are times where you want to create a constructor. oh, and yes, in JavaScript a function can be a constructor. And this is where problems might occur. Let’s start with a simple example:&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:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:495bd363-3981-48a6-aa43-37d71933a710" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background-color:#ffffff;max-height:300px;overflow:auto;padding:2px 5px;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; Point = &lt;span style="color:#0000ff;"&gt;function&lt;/span&gt; (x, y) {&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.x = x;&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.y = y;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; pt = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Point(1, 1); &lt;span style="color:#006400;"&gt;//correct usage&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; anotherPt = Point(1, 1);&lt;span style="color:#006400;"&gt;//hum...not the intended usage&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Ok, so can you see what’s happening here? Btw, before you start laughing, I’ve seen this happen&amp;#160; in the past (yes, in the real world!). When Point was created, it was meant to be used as constructor. But that doesn’t mean that a rookie or someone who is not paying attention can’t&amp;#160; call the Point function directly without using the new instruction. When that happens, the this special variable won’t point to a Point instance. In this case, it was probably referencing the window object, but it all depends on the existing context at time of invocation.&lt;/p&gt;  &lt;p&gt;Now, if you’re writing frameworks, this is really a nightmare. You need to have some way of not allowing this. The good news is that there’s a way to fix this and it involves using the instanceof operator in 2 or 3 lines of code. Take a look at the next snippet:&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:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:30d03041-86f8-4bb8-9906-03e56d78806a" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background-color:#ffffff;max-height:500px;overflow:auto;padding:2px 5px;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; Point = &lt;span style="color:#0000ff;"&gt;function&lt;/span&gt; (x, y) {&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;instanceof&lt;/span&gt; Point)) {&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Point(x, y);&lt;br /&gt;     }&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.x = x;&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.y = y;&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;And that’s it! really! When the function is invoked, we start by checking if this points to an instance of Point. When you use new, this does reference a Point instance. However, that doesn’t happen when you call the Point function as a function. With this small change, you’re ensuring that you’ll always get a new initialized Point instance from that method. Wasn’t that easy enough? And that’s it for now. Stay tuned for more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1778571" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>oh, gosh! SO many ways to require a script…</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/30/oh-gosh-so-many-ways-to-require-a-script.aspx</link><pubDate>Mon, 30 Nov 2009 20:45:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1743090</guid><dc:creator>luisabreu</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1743090</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/30/oh-gosh-so-many-ways-to-require-a-script.aspx#comments</comments><description>&lt;p&gt;I’d like to apologize for the lack of posts lately…no, I haven’t given up in my Silverlight series, but I’ve been busy updating my existing ASP.NET book to the 4.0 version. It seems like I will have a lot of work (much more than I had anticipated, but the truth is that I really enjoy writing about technical stuff) so the Silverlight series won’t really go as fast as the one on MS AJAX…sorry for that…now back to business :)&lt;/p&gt;  &lt;p&gt;I really didn’t gave it any thoughts until I spoke with a friend who he asked me about the options we have for requiring a script (when using MS AJAX, of course!). So, lets see…we can specify it by:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;using the scripts object : Sys.scripts.Core loads the basic MS AJAX JavaScript file&lt;/li&gt;    &lt;li&gt;using the components object: Sys.components.dataView loads all the JavaScript files required for using the DataView component;&lt;/li&gt;    &lt;li&gt;using the plugins object: Sys.plugins.bind will load all the JavaScript file required for using the bind method;&lt;/li&gt;    &lt;li&gt;name: “History” will load all the scripts required for supporting the history feature.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;All of them requires that you “define” your scripts in a JavaScript file which must be included in the page after the start.js file. And the best thing is that you can mix them all when using the Sys.required method. Fantastic, right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1743090" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Some observations on MS AJAX and JQuery integration</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/24/some-observations-on-ms-ajax-and-jquery-integration.aspx</link><pubDate>Tue, 24 Nov 2009 09:16:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1741918</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1741918</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/24/some-observations-on-ms-ajax-and-jquery-integration.aspx#comments</comments><description>&lt;p&gt;In my last &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/23/ms-ajax-beta-new-plugins.aspx"&gt;post&lt;/a&gt;, I’ve talked a little bit about the new plugins introduced by the latest release of MS AJAX (beta, at the time of writing). One of the things that didn’t enjoy much was that I had to call the JQuery’s &lt;a href="http://docs.jquery.com/Core/get"&gt;get&lt;/a&gt; method in order to get a reference to the DOM element array. What I was forgetting was that MS AJAX has really &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/10/17/the-sys-get-helper.aspx"&gt;good integration&lt;/a&gt; with JQuery (something that &lt;a href="http://encosia.com/"&gt;Dave Ward&lt;/a&gt; was kind enough to reminded me).&lt;/p&gt;  &lt;p&gt;In practice, this means that I can simply pass a string with the selector I want and I’ll automatically get the correct DOM element array. here’s the updated code:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
  &lt;/span&gt;&lt;span style="color:#bab774;"&gt;Sys&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.require(
        [&lt;/span&gt;&lt;span style="color:#bab774;"&gt;Sys&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.&lt;/span&gt;&lt;span style="color:#bab774;"&gt;scripts&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.&lt;/span&gt;&lt;span style="color:#bab774;"&gt;jQuery&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, &lt;/span&gt;&lt;span style="color:#bab774;"&gt;Sys&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.&lt;/span&gt;&lt;span style="color:#bab774;"&gt;scripts&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.&lt;/span&gt;&lt;span style="color:#bab774;"&gt;ComponentModel&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;],
        &lt;/span&gt;&lt;span style="color:#949df3;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;() {
          &lt;/span&gt;&lt;span style="color:#bab774;"&gt;$&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.&lt;/span&gt;&lt;span style="color:#bab774;"&gt;addHandler&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(
                &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
                &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
                &lt;/span&gt;&lt;span style="color:#949df3;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#bab774;"&gt;evt&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;) {
                  &lt;/span&gt;&lt;span style="color:#bab774;"&gt;alert&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;JQuery: &amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;+ &lt;/span&gt;&lt;span style="color:#bab774;"&gt;evt&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.&lt;/span&gt;&lt;span style="color:#bab774;"&gt;target&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.&lt;/span&gt;&lt;span style="color:#bab774;"&gt;id&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
                });
        }
    );  
  &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Notice that you can use more complex selectors too. All the selectors which can’t be interpreted by MS AJAX will be delegated to JQuery. As you can see, MS AJAX is becoming a really cool lib, don’t you think?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1741918" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>MS AJAX beta – new plugins</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/23/ms-ajax-beta-new-plugins.aspx</link><pubDate>Mon, 23 Nov 2009 14:47:58 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1741765</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1741765</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/23/ms-ajax-beta-new-plugins.aspx#comments</comments><description>&lt;p&gt;The MS AJAX beta release adds some new plugins to the previous ones. Besides the existing setCommand and bind, we now have access to the following plugins:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;addHander: you can use this plugin to set up a handler for one event fired by one or several DOM elements; &lt;/li&gt;    &lt;li&gt;removeHandler: as you can probably guess from its name, you can use this to cancel an existing DOM event subscription; &lt;/li&gt;    &lt;li&gt;addHandlers: similar to the addHandler. However, in this case you can hook up several events (instead of passing the name of the event, you’re supposed to use a literal JS object where you specify the events and corresponding handlers); &lt;/li&gt;    &lt;li&gt;clearHandlers: used for cancelling all event subscriptions that have been previously setup; &lt;/li&gt;    &lt;li&gt;activateElements: used for activating the indicated elements with a specific binding context. Generally, you get activation for free. However, there are a couple of times where&amp;#160; you may need to activate an element explicitly. In those cases, this is the quickest (and shortest – yes, I’m counting key strokes) way to do that. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I’m not sure if you recall it, but plugins get promoted to Sys helper methods. Take a look at the following code which shows how to use the addHandler plugin to hook up the click event over all the existing buttons:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head &lt;/span&gt;&lt;span style="color:green;"&gt;runat&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
 &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot; &lt;br /&gt;       &lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Scripts/MicrosoftAjax/Start.debug.js&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;br /&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
 &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
   &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Sys.require([Sys.scripts.ComponentModel],
     &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;() {
       &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;buttons = document.getElementsByTagName(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
       Sys.addHandler(buttons,
                      &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
                      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(evt) {
                         alert(evt.target.id);
                      });           
          
        });
    &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;bt1&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;value&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Button 1&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;bt2&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;value&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Button 2&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;bt3&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;value&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Button 3&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Whenever you click over a button, you should get an alert message with the ID of the button. Now, since we’re talking about handlers, here’s how you’d use the addHanders plugin:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;Sys.addHandlers(Sys.get(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;#bt1&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;),
    {
        click: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(evt) {
            alert(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;bt-1&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
        }
    }
    );     &lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, I’ve used a literal JS object for specifying the events I want to handle. You should only use this plugin when you want to pass several handlers to different events. Notice that both plugins (addHandler and addHandlers) expect a reference to one or more DOM elements (you can see how to pass several references in the first snippet; the second passes a single DOM element reference). That’s why you should only use the second (addHandlers) when you need to setup several events at once. &lt;/p&gt;

&lt;p&gt;Here’s what you get when you click “Button 1” after adding the last snippet to the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/8877.addHandlers_5F00_044F3340.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="addHandlers" border="0" alt="addHandlers" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/8053.addHandlers_5F00_thumb_5F00_745FEE7B.jpg" width="268" height="193" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Notice that if you add this last snippet to the previous page, you should see two alert messages because you’re effectively adding two handlers to “button 1” click event.&lt;/p&gt;

&lt;p&gt;One of the nicest things about plugins is their integration with JQuery. For instance, take a look at the following code:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;html &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head &lt;/span&gt;&lt;span style="color:green;"&gt;runat&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot; &lt;br /&gt;   &lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Scripts/MicrosoftAjax/Start.debug.js&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Sys.require(&lt;br /&gt;        [Sys.scripts.jQuery,Sys.scripts.ComponentModel],
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;() {
            $.addHandler(
                $(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;).get(),
                &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(evt) {
                 alert(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;JQuery: &amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;+ evt.target.id);
                });
        }
    );  
    &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;bt1&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;value&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Button 1&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;bt2&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;value&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Button 2&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;bt3&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;value&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Button 3&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;html&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, I’ve started by requiring JQuery and the MS AJAX JavaScript files needed for using the addHandler method (oh, btw, I could have specified the dependency through the Sys.plugins.addHandler too). After getting all the files, my anonymous callback function will be called and that’s where everything interesting is happening. &lt;/p&gt;

&lt;p&gt;There’s an addHandler method which got added to the jQuery object (aliased to the $) during JQuery loading. Notice that the addHandler plugin still expects the same arguments and that’s why we need to use the JQuery’s get method to get a reference to an array with all the DOM elements of the internal wrapped set.&lt;/p&gt;

&lt;p&gt;This behavior is pretty cool and also works with other plugins. The only thing you should keep in mind is that non-behavior/control plugins always get added to the $ object; on the other hand, behaviors/controls get added to the $.fn object (and that means that these plugins are “transformed” into “instance” methods of the JQuery object – take a look at this &lt;a href="http://weblogs.asp.net/scottgu/archive/2009/10/15/announcing-microsoft-ajax-library-preview-6-and-the-microsoft-ajax-minifier.aspx"&gt;post&lt;/a&gt; to see what I mean).&lt;/p&gt;

&lt;p&gt;And that’s it&amp;#39;. Stay tuned for more on MS AJAX.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1741765" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>MS AJAX beta – ACT support</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/23/ms-ajax-beta-act-support.aspx</link><pubDate>Mon, 23 Nov 2009 13:35:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1741753</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1741753</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/23/ms-ajax-beta-act-support.aspx#comments</comments><description>&lt;p&gt;In the last week, &lt;a href="http://www.asp.net/ajaxlibrary/download.ashx"&gt;MS AJAX beta&lt;/a&gt; was released during &lt;a href="http://microsoftpdc.com/"&gt;PDC 2009&lt;/a&gt;. One of the things that this released added is ACT script loader support. If you go into the samples, you’ll see that there is an Extended folder which holds all the client files for the behaviors and controls exposed by the AjaxControlToolkit.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/5635.extended_5F00_7821468F.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="extended" border="0" alt="extended" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/6215.extended_5F00_thumb_5F00_0035208D.jpg" width="177" height="263" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The code files have all been updated so that its&amp;#39; definitions are wrapped into an execute function (which is also placed inside an anonymous function). As you probably recall from previous posts, this function is called when all the dependencies have been loaded. Notice that&amp;#160; with the previous CTP you could download the ACT from codeplex and make the changes present in all the files that come with this release. In other words, wrapping all the code within functions is the easiest part when you want to adapt an existing script so that it can be used with the new script loader component.&lt;/p&gt;  &lt;p&gt;To me, the most important script file is the ExtendedControl Javascript file because it contains all the definitions (and dependencies) required by all the ACT controls. In practice, this means that you can simply drop this file on a page (plus the base start.js Javascript file) and use any of the ACT controls. Don’t believe me? Here’s some demo code which uses the calendar behavior:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;html &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head &lt;/span&gt;&lt;span style="color:green;"&gt;runat&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
 &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;link &lt;/span&gt;&lt;span style="color:green;"&gt;rel&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;stylesheet&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/css&amp;quot; 
   &lt;/span&gt;&lt;span style="color:green;"&gt;href&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Scripts/MicrosoftAjax/Extended/Calendar/Calendar.css&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
 &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot; 
   &lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Scripts/MicrosoftAjax/Start.debug.js&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
 &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot; 
   &lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Scripts/MicrosoftAjax/Extended/ExtendedControls.debug.js&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
 &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
   &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Sys.require([Sys.components.calendar],
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;() {
      Sys.create.calendar(Sys.get(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;#date&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;));
   });
&lt;/span&gt;&lt;span style="color:#c3c567;"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;date&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;html&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And that’s it: I’ve just added two scripts and that’s all I needed to use the CalendarBehavior. Btw, here’s the results I got on my machine after loading the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/2021.calendar_5F00_65E4919B.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="calendar" border="0" alt="calendar" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/0535.calendar_5F00_thumb_5F00_4A8BED00.jpg" width="269" height="239" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Nice, right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1741753" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>MS AJAX Lib beta is out</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/20/ms-ajax-lib-beta-is-out.aspx</link><pubDate>Fri, 20 Nov 2009 14:10:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1741009</guid><dc:creator>luisabreu</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1741009</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/20/ms-ajax-lib-beta-is-out.aspx#comments</comments><description>&lt;p&gt;You can get it from &lt;a href="http://www.asp.net/ajaxlibrary/download.ashx"&gt;here&lt;/a&gt;. I’m curious to see if it contains new features…&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1741009" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>No more hacks for the MS AJAX’s binding problem</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/13/no-more-hacks-for-the-ms-ajax-s-binding-problem.aspx</link><pubDate>Fri, 13 Nov 2009 09:26:35 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1739457</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1739457</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/13/no-more-hacks-for-the-ms-ajax-s-binding-problem.aspx#comments</comments><description>&lt;p&gt;At least, when the RTM version is released!&lt;/p&gt;  &lt;p&gt;Ok, I guess you need some context: in the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/11/ms-ajax-bindings-and-serialization.aspx"&gt;post&lt;/a&gt;, I talked about a problem which was mentioned to me by Andy in the comments of an existing post. The problem was that the __msajaxBindings field ended up being added to an object used in a live binding and that really means trouble when you try to serialize it (because you end up with a cyclic reference which generates a runtime exception).&lt;/p&gt;  &lt;p&gt;In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/11/ms-ajax-bindings-and-serialization.aspx"&gt;post&lt;/a&gt;, I suggested a hack for it. Fortunately, &lt;a href="http://weblogs.asp.net/infinitiesloop/"&gt;Dave&lt;/a&gt; saw it and emailed me assuring that the hack won’t be necessary and that the internal __msajax… fields should only be added to HTML elements. As you’re probably expecting by now, the problem I mentioned in the previous post is the result of a bug (which will be solved before the RTM is out). &lt;/p&gt;  &lt;p&gt;After being alerted by &lt;a href="http://weblogs.asp.net/infinitiesloop/"&gt;Dave&lt;/a&gt;, I’ve decided to take a closer look at the code…And yes, there’s a problem and it’s on the Sys.UI.DomElement.isDomElement method. This method ends up invoking the _isDomElement method which has a check that returns a “false positive”:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;Sys._isDomElement = &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Sys$_isDomElement(obj) {
   &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;val = &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;false&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
   &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;typeof &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(obj.nodeType) !== &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;#39;number&amp;#39;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;) {
     &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;doc = obj.ownerDocument || obj.document || obj;
     &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(doc != obj) {
       &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;w = doc.defaultView || doc.parentWindow;
       val = (w != obj);
     }
     &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;else &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{
       &lt;strong&gt;val = (&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="color:#a89a86;"&gt;typeof &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(doc.body) === &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;#39;undefined&amp;#39;&lt;/span&gt;&lt;/strong&gt;&lt;span style="color:#a79a86;"&gt;&lt;strong&gt;);&lt;/strong&gt;
     }
  }
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;!val;
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Can you spot the problem? Yep, the val = (typeof(doc.body)… line is responsible for considering the object a DOM element (&lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/11/ms-ajax-bindings-and-serialization.aspx"&gt;remember&lt;/a&gt;: section had a property called body and if you look at the code, you’ll see that it ends up messing everything). While the good guys at MS don’t publish a new release which solves this bug, you really shouldn’t use any property named &lt;strong&gt;body&lt;/strong&gt; in an object which is used in a binding relationship (at least, if you intend to serialize that object). Ok, to be fair, you can always use the previous awful &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/11/ms-ajax-bindings-and-serialization.aspx"&gt;hack&lt;/a&gt;, but If I were you, I’d renamed the property :)&lt;/p&gt;

&lt;p&gt;And that’s it for now. Stay tuned for more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1739457" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>MS AJAX: bindings and serialization</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/11/ms-ajax-bindings-and-serialization.aspx</link><pubDate>Wed, 11 Nov 2009 19:30:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1739055</guid><dc:creator>luisabreu</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1739055</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/11/ms-ajax-bindings-and-serialization.aspx#comments</comments><description>&lt;p&gt;In my “goodbye MS AJAX post”, reader Andy asked a really interesting &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/09/the-adonetdatacontext-object-what-about-saving.aspx#1738872"&gt;question&lt;/a&gt;: how to serialize an object which is used in a binding relationship? The main problem is that the JavaScriptSerializer doesn’t support circular references. Unfortunately, it doesn’t also provide a way for you to specify which properties should be serialized. However, the problem is still there: how do you serialize the objects used in binding relationships?&lt;/p&gt;  &lt;p&gt;I’m still hopping that the team will fix the serializer before MS AJAX RTMs is released, but meanwhile, we’re still left with the problem. I’ve thought a little bit about it and it seems like the best option is to “suspend” the binding, serialize it, and then “resume” it again. Notice that the code you’re about to see has not been tested (it was written during my bus trip home, so it might have one or two bugs :) ). Let’s take a look at the example (I’ve reused most of Andy’s example and I’d like you to concentrate on the save method):&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;html &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head &lt;/span&gt;&lt;span style="color:green;"&gt;runat&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;title&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;title&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Scripts/MicrosoftAjax/start.debug.js&amp;quot; 
        &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;instance = [
            { title: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;#39;New document&amp;#39;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
                content: {
                    sections: [
                        { body: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;#39;Enter text here&amp;#39; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;},
                        { body: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;#39;other text&amp;#39; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;}
                    ]
                }
            }
        ];
        Sys.require([Sys.components.dataView],
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;() {
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;view = Sys.create.dataView(
                        &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;#maincontent&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
                        { itemTemplate: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;#39;#edit&amp;#39;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, 
                          data: instance});
        }); &lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;onSave(){            
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;data = Sys.get(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;$maincontent&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;).get_data();
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;backup = {};
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;cleanupMetadata(obj, propName, key) {
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(obj.hasOwnProperty(propName)) {
                    backup[key] = {};
                    backup[key][propName] = obj[propName];
                    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;delete &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;obj[propName];
                }
            }
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;restoreMetadata(obj, propName, key) {
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(backup[key] !== undefined) {
                    obj[propName] = backup[key][propName];
                    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;delete &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;backup[key];
                }
            }
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;for &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;i = &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;; i &amp;lt; data.length; i++) {
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;sections = data[i].content.sections;
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;for &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;j = &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;; j &amp;lt; sections.length; j++) {
                    cleanupMetadata(sections[j], &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;_msajaxBindings&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, &lt;br /&gt;                                        i+&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;-&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;+j);
                }
            }
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;serialized = &lt;br /&gt;                   Sys.Serialization.JavaScriptSerializer.serialize(data);
            &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;for &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;i = &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;; i &amp;lt; data.length; i++) {
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;sections = data[i].content.sections;
                &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;for &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;j = &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;; j &amp;lt; sections.length; j++) {
                    restoreMetadata(&lt;br /&gt;                             sections[i], &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;_msajaxBindings&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, i + &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;-&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;+ j);
                }
            }
            alert(serialized);            
        }&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;
    &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;dataview&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys.UI.DataView&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;edit&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;class&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;label&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Title&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;label&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;input &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;value&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{binding title}&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;attach&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;dataview&amp;quot; 
         &lt;/span&gt;&lt;span style="color:green;"&gt;dataview&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;data&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{ content.sections }}&amp;quot;
         &lt;/span&gt;&lt;span style="color:green;"&gt;class&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;label&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;label&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;textarea&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{binding body}&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;textarea&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt; 
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;    
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;maincontent&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;button &lt;/span&gt;&lt;span style="color:green;"&gt;onclick&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;onSave()&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Save&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;button&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;html&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;We’re using two helper methods: cleanupMetadata and restoreMetadata. In the cleanupMetadata, we store the _msajaxBinding info in the backup object. As you can see, we’re building a key by combining the positions of the objects so that we can recover that metadata later. Yes, this isn’t really reusable code and I’d really prefer to have the serializer filter the properties which shouldn’t be serialized. However, this approach will probably be enough for now if you need to serialize an object used in a binding relationship.&lt;/p&gt;

&lt;p&gt;And that’s it for now…&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1739055" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>The AdoNetDataContext object: what about saving?</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/09/the-adonetdatacontext-object-what-about-saving.aspx</link><pubDate>Mon, 09 Nov 2009 14:34:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1738594</guid><dc:creator>luisabreu</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1738594</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/09/the-adonetdatacontext-object-what-about-saving.aspx#comments</comments><description>&lt;p&gt;Well, this really doesn’t deserve any special attention. Ok, let me put it another way: there’s a lot of work going on behind the scenes, but you shouldn’t have to worry with any of it. &lt;/p&gt;  &lt;p&gt;I’ve thought about writing a post on the objects that end up being used by the AdoNetDataContext to propagate changes back to the server, but after some thought, I’ve give up on that idea because I’m not seeing anyone use any of these classes directly (yes, the AdoNetDataContext should do it all for you).&lt;/p&gt;  &lt;p&gt;And that’s why I’m wrapping up this series on MS AJAX preview 6. I hope you’ve enjoyed it as much as did…Now, I do need to find another topic to keep me busy looking at new stuff…any ideas?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1738594" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Getting your ADO.NET Data Services uris right: the easy way</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/09/getting-your-ado-net-data-services-uris-right-the-easy-way.aspx</link><pubDate>Mon, 09 Nov 2009 12:13:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1738578</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1738578</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/09/getting-your-ado-net-data-services-uris-right-the-easy-way.aspx#comments</comments><description>&lt;p&gt;Here we are again, for more on the MS AJAX library. In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/09/getting-started-with-the-adonetdatacontext.aspx"&gt;post&lt;/a&gt;, we’ve seen that we can interact with ADO.NET Data Services by using the AdoNetDataContext instances. Today, we’ll be looking at more advanced features of the ADO.NET Data Services which are also available in the MS AJAX library.&lt;/p&gt;  &lt;p&gt;Before going on, I must confess that I’m not really an ADO.NET Data Service user (I guess this has made me curious enough for taking a peek in the future). However, since the MS AJAX library offers such great support for it, I couldn’t resist doing some digging and seeing which options are available for interacting with an existing data service.&lt;/p&gt;  &lt;p&gt;According to the &lt;a href="http://msdn.microsoft.com/en-gb/library/cc956153.aspx"&gt;docs&lt;/a&gt;, ADO.NET Data Services can “be surfaced as a REST-style resource collection that is addressable with URIs and that agents can interact with using standard HTTP verbs such as GET, POST, PUT or DELETE”. In practice, and if we go back to our previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/09/getting-started-with-the-adonetdatacontext.aspx"&gt;example&lt;/a&gt;, we can get all the images by using the uri &lt;a href="http://:&amp;hellip;/Services/ImageDataService.svc/Images"&gt;http://:…/Services/ImageDataService.svc/Images&lt;/a&gt;. Besides this simple option, which returns all the images contained in the object, there are still other interesting options. For instance:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;we can navigate through related properties; &lt;/li&gt;    &lt;li&gt;we can expand associated properties; &lt;/li&gt;    &lt;li&gt;we can filter the returned results; &lt;/li&gt;    &lt;li&gt;we can paginate the returned data. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;All these operations are specified through uris. We can easily do them by building the uris by hand. For instance, we can modify the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/09/getting-started-with-the-adonetdatacontext.aspx"&gt;example&lt;/a&gt; so that it also displays the associated :&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys.UI.DataView&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div 
      &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;myView&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;class&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;sys-template&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;attach&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;dv&amp;quot;      
      &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;autofetch&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;true&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;dataprovider&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{ _ctx }}&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;fetchoperation&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Images&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;fetchparameters&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{ {$expand: &amp;#39;Tags&amp;#39;} }}&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;h2&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{{Name}}&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;h2&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;img &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{Uri}}&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;alt&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{Name}}&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;      
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;class&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;sys-template&amp;quot;
            &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;attach&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;dv&amp;quot;
            &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;data&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{ Tags }}&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;span&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{{Name}}&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;span&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
        &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;By using the $expand parameter, we’ll end up getting each image’s associated tags and we can display them by using a nested DataView control. The next image shows what happens when we load the previous page:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/3580.expand_5F00_3901BAC9.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="expand" border="0" alt="expand" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/2084.expand_5F00_thumb_5F00_25082833.jpg" width="225" height="255" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We could specify all the “special” parameters this way. However, there is another way: we can rely on the Sys.Data.AdoNetQueryBuilder class for doing building the uri for us.&lt;/p&gt;

&lt;p&gt;The AdoNetQueryBuilder’s constructor expects the base uri and exposes several properties which we can use to build the uri which performs a specific operation against the existing ADO.NET Data Service. You’ll probably end up using the ones which are present in the following list:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;skip and top: we can combine them for paginating data; &lt;/li&gt;

  &lt;li&gt;orderby: used for indicating the sorting order; &lt;/li&gt;

  &lt;li&gt;filter: responsible for filtering the returned data; &lt;/li&gt;

  &lt;li&gt;expand: used for specifying the properties which will be “expanded” and included in the response. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I must confess that I really didn’t find the use of this object intuitive at first. However, after running some experiences, everything started making sense. In order to show you how you might end up using this object, suppose we want to get all the images and associated tags and that we want to order them by name. Here’s a possible solution:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head &lt;/span&gt;&lt;span style="color:green;"&gt;runat&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;   
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;style &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
      &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.sys-template{
        display: none;
      }      
      img{
        width:320px;
        height:212px;
      }
    &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;style&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot; 
      &lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;../Scripts/MicrosoftAjax/start.debug.js&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;_ctx = &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
      Sys.require([Sys.components.adoNetDataContext,
                   Sys.components.dataView],
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;() {
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;uriBuilder = &lt;br /&gt;              &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;new &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Sys.Data.AdoNetQueryBuilder(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Images&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
        uriBuilder.set_expand(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Tags&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);&lt;/span&gt;&lt;span style="color:#006400;"&gt;//$expand
        &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;uriBuilder.set_orderby(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);&lt;/span&gt;&lt;span style="color:#006400;"&gt;//$orderby
        &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;_ctx = Sys.create.adoNetDataContext({
          serviceUri: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;../Services/ImagesDataService.svc&amp;quot;
        &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;});
        Sys.create.dataView(
             Sys.get(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;#myView&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;),
             { dataProvider: _ctx,
               autoFetch: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;true&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
               fetchOperation: uriBuilder.toString()
             });

      });
    &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys.UI.DataView&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div 
      &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;myView&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;class&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;h2&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{{Name}}&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;h2&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;img &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{Uri}}&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;alt&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{Name}}&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;      
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;class&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;sys-template&amp;quot;
            &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;attach&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;dv&amp;quot;
            &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;data&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{ Tags }}&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;span&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{{Name}}&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;span&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
        &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This time, I’ve opted for using an imperative approach. Things are really similar to what we had before; however, notice that now we’re using an AdoNetQueryBuilder object to set the fetchOperation property of the DataView object. &lt;/p&gt;

&lt;p&gt;We start by instantiating the AdoNetQueryBuilder object. If you recall it, I said that this wasn’t really intuitive…it was only looking at the source code that I’ve understood that I was supposed to pass the name of the resource I wanted to access (instead of passing the base uri for the service – not sure on why the constructor’s only parameter is called uri, when resource would have make things so much easier!). Since I was interested in retrieving images, I only had to pass the string “Images” to the constructor.&lt;/p&gt;

&lt;p&gt;After having an instance and setting the required properties (recall from above that we wanted to get images+tags and to order everything by name), we can simply call the toString method to get the string which represents the operation we need to perform. And that’s it. We’ll still be speaking about MS AJAX in future posts, so stay tuned for more!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1738578" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Getting started with the AdoNetDataContext</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/09/getting-started-with-the-adonetdatacontext.aspx</link><pubDate>Mon, 09 Nov 2009 10:54:35 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1738566</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1738566</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/09/getting-started-with-the-adonetdatacontext.aspx#comments</comments><description>&lt;p&gt;Today we’re going to start looking at the AdoNetDataContext. This class expands the DataContext object and uses several specific methods for ensuring the correct interaction with an existing ADO.NET Data Service. &lt;/p&gt;  &lt;p&gt;These internal methods added by the AdoNetDataContext are essentially used in identity related operations. In other words, it extends the base DataContext class by introducing several methods which turn on identity support (btw, I’m talking about the stuff we’ve seen in &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/08/the-datacontext-component-creating-entities.aspx"&gt;several&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/05/the-datacontext-component-linking-objects.aspx"&gt;of&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/the-datacontext-component-improving-the-identity-algorithm.aspx"&gt;the&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx"&gt;previous&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/02/the-datacontext-component-committing-changes-take-ii.aspx"&gt;posts&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;In this post, I’ll be reusing the sample ADO.NET Data Service which is included in &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34488"&gt;MS AJAX preview 6&lt;/a&gt;. Here’s the model used internally by the ImageDataService.svc ADO.NET service:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/1016.adonetmodel_5F00_6F4C8629.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="adonetmodel" border="0" alt="adonetmodel" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/4643.adonetmodel_5F00_thumb_5F00_549FEA10.jpg" width="371" height="154" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In this case, and since this is just an intro post, we’ll build a simple page which shows all the images returned by this ADO.NET service:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head &lt;/span&gt;&lt;span style="color:green;"&gt;runat&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;   
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;style &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
      &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;.sys-template{
        display: none;
      }      
      img{
        width:320px;
        height:212px;
      }
    &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;style&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot; 
      &lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;../Scripts/MicrosoftAjax/start.debug.js&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;_ctx = &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
      Sys.require([Sys.components.adoNetDataContext, 
                   Sys.components.dataView],
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;() {
        _ctx = Sys.create.adoNetDataContext({
            serviceUri: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;../Services/ImagesDataService.svc&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
            mergeOption: Sys.Data.MergeOption.appendOnly
        });        
    });
    &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;script&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;head&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;BLOCKED SCRIPTSys.UI.DataView&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div 
      &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;myView&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;class&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;sys-template&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;attach&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;dv&amp;quot;      
      &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;autofetch&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;true&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;dataprovider&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{ _ctx }}&amp;quot;
      &lt;/span&gt;&lt;span style="color:green;"&gt;dv&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;fetchoperation&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;Images&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;h2&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{{Name}}&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;h2&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;img &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;src&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{Uri}}&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;:&lt;/span&gt;&lt;span style="color:green;"&gt;alt&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;{{Name}}&amp;quot; &lt;/span&gt;&lt;span style="color:#c3c567;"&gt;/&amp;gt;      
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;body&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;By now, you shouldn’t really be surprised by anything that is shown in the previous snippet. As you can see, the AdoNetDataContext class protects us from all those nasty details associated with the communication with the ImageDataService.svc server side service. I’m not going to show you how to add, edit or remove items because the samples released with &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34488"&gt;preview 6&lt;/a&gt; already have code which shows how to do those things.&lt;/p&gt;

&lt;p&gt;On the next post, we’ll see how to do more interesting operations by using the AdoNetQueryBuilder object. Stay tuned for more on MS AJAX.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1738566" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>The DataContext component: creating entities</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/08/the-datacontext-component-creating-entities.aspx</link><pubDate>Sun, 08 Nov 2009 12:53:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1738378</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1738378</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/08/the-datacontext-component-creating-entities.aspx#comments</comments><description>&lt;p&gt;Before we can move on and take a look at the AdoNetDataContext component, we need to talk about one more topic: creating entities and how it might be integrated with DataContext component. The DataContext exposes a createEntity method which can be used for&amp;#160; creating new objects of a specific type. Internally, that method will always delegate the creation of a new entity to the function to which the createEntityMethod property points to.&lt;/p&gt;  &lt;p&gt;As you’re probably wondering, this is something you’ll only need to configure if you need to use the DataContext features associated with identity (and even in that case, setting the createEntityMethod is optional if you’ve set the getNewIdentityMethod property). &lt;/p&gt;  &lt;p&gt;The createEntityMethod property should reference a function which receives two parameters (a DataContext reference and a string with the “type” of the object that should be created) and initializes the metadata necessary for tracking that object. If you recall the previous posts, you’ll surely &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/the-datacontext-component-improving-the-identity-algorithm.aspx"&gt;remember&lt;/a&gt; that we’ve added an internal __meta property to all the objects stored by the DataContext instance. &lt;/p&gt;  &lt;p&gt;We could improve our DataContext used &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/the-datacontext-component-improving-the-identity-algorithm.aspx"&gt;at the time&lt;/a&gt; by setting the createEntityMethod to a function which initializes the metadata associated to that object. Here’s how I’d augment the entityManager we’ve been using with a new method used for creating new entities:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;createNewEntity: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(dataContext, entitySet) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;obj = {};
    entityManager.getNewIdentity(dataContext, obj, entitySet);
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;obj;
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, we’re simply relying in the getNewIdentity method because it is responsible for setting the __meta object used for that purpose. Referencing entityManager instead of this might seem little strange at first, but that’s needed because the DataContext won’t call the reference through the entityManager instance (it will simply call the function through the reference it got to it during initialization:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx = Sys.create.dataContext(
  {
      serviceUri: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;PeopleService.svc&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
      getIdentityMethod: entityManager.getEntityId,
      getNewIdentityMethod: entityManager.getNewIdentity,
      isDeferredPropertyMethod: entityManager.isDeferredProperty,
      getDeferredPropertyFetchOperationMethod: &lt;br /&gt;                                 entityManager.getDeferredProperty,
      createEntityMethod: entityManager.createNewEntity
  }
);&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And that’s it. There’s really not much to say about the createEntity method. Stay tuned for more on MS AJAX.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1738378" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>The DataContext component: deferred properties</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/07/the-datacontext-component-deferred-properties.aspx</link><pubDate>Sat, 07 Nov 2009 21:39:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1738278</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1738278</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/07/the-datacontext-component-deferred-properties.aspx#comments</comments><description>&lt;p&gt;Today we’ll keep looking at the DataContext component and we’ll see how it supports deferred properties. Deferred properties are properties which are lazy loaded as “needed”. Using deferred properties means that the component will try to make a remote call for getting the value of that property. As you’re probably expecting, this means that you’ll have to write code on the client and server side. On the client, we’ll need some JavaScript functions which are responsible for:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;checking if a specific property is deferred; &lt;/li&gt;    &lt;li&gt;returning an instance of the Sys.NetWebServiceOperation which has all the data needed for performing the remote call which returns the value of that property. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;After creating the functions and configuring the DataContext component’s isDeferredPropertyMethod and getDeferredPropertyFetchOperationMethod , we can get the deferred value of the property by using the DataContext’s fetchDeferredProperty method. Let’s take a look at the client side code (which augments the code we introduced in previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/05/the-datacontext-component-linking-objects.aspx"&gt;posts&lt;/a&gt;):&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;entityManager = {
    metaCounter: {},
    getEntityId: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(dataContext, entity) {
       &lt;/span&gt;&lt;span style="color:#006400;"&gt;//removed: check previous posts
    &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;},
    getNewIdentity: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(dataContext, entity, entitySet) {
        &lt;/span&gt;&lt;span style="color:#006400;"&gt;//removed: check previous posts
    &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;},
    isDeferredProperty: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(dataContext, entity, propertyName) {
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;id = dataContext.getIdentity(entity);
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;propertyName === &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;contacts&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;&amp;amp;&amp;amp;
               id !== &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;&amp;amp;&amp;amp;
               /$new/.test(id);
    },
    getDeferredProperty: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;br /&gt;         dataContext, entity, propName, parameters, userContext) {
        &lt;/span&gt;&lt;span style="color:#006400;"&gt;//returns web service operation
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;id = dataContext.getIdentity(entity);
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return new &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;Sys.Net.WebServiceOperation(
            &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;GetContactsFor&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
            {id: parseInt(/(\d+)/.exec(id)[&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;]) },
            &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;get&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;)
            ;
    }

};&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;It goes without saying (I think!) that this is just demo code (you’ll need better code for real world scenarios). As you can see, the contacts property will only be lazily loaded when we’re trying to get it from an object which has been previously persisted in the server side (once again, you’ll need to go back to the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/05/the-datacontext-component-linking-objects.aspx"&gt;previous&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/the-datacontext-component-improving-the-identity-algorithm.aspx"&gt;posts&lt;/a&gt; to &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx"&gt;understand&lt;/a&gt; what’s going on here). In the real world, I’d probably check that property against undefined too so that you’d only ask for the data when the property hasn’t been initialized.&lt;/p&gt;

&lt;p&gt;The getDeferredProperty tries to find the id of the current person’s instance (don’t forget that person instances which have been previously persisted are on the form people$xxx where xxx is an integer which identifies the current instance) and uses it to build a JS literal object which is used to pass parameters to the server side code. Notice that the Sys.Net.WebService’s “constructor” expects three parameters: the first identifies the name of the operation; the second is a literal object which represent the parameters that will be passed to the server side method. Finally, the third parameter is a string which identifies the HTTP method that should be used in the server side call.&lt;/p&gt;

&lt;p&gt;Now, we’re ready to create a new DataContext instance:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx = Sys.create.dataContext(
  {
    serviceUri: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;PeopleService.svc&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
    getIdentityMethod: entityManager.getEntityId,
    getNewIdentityMethod: entityManager.getNewIdentity,
    isDeferredPropertyMethod: entityManager.isDeferredProperty,
    getDeferredPropertyFetchOperationMethod: &lt;br /&gt;                      entityManager.getDeferredProperty
  }
);&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;One additional note before looking at the server side code: we *really* need to set the serviceUri property when we intend to use deferred property fetching. If you don’t set it, you’ll end up receiving an exception when you try to get the value of the property from the server side:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;ctx.fetchDeferredProperty(
    people[&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;], 
    &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;contacts&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, 
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, 
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(){
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;contacts = people[&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;].contacts;
       &lt;/span&gt;&lt;span style="color:#006400;"&gt;//contacts already filled here!
    &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;});&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The fetchDeferredProperty method receives several parameters:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a reference to the entity whose property is going to be obtained through the remote call; &lt;/li&gt;

  &lt;li&gt;name of the property which is being lazily fetched; &lt;/li&gt;

  &lt;li&gt;parameter: literal object with parameter info that will be passed to the server side. Note that this object will be merged with the parameter object encapsulated by the WebServiceOperation returned from the DataContext’s getDeferredPropertyFetchOperationMethod function; &lt;/li&gt;

  &lt;li&gt;mergeOptions: the Sys.Data.MergeOption which identifies the merge which will be used when the data is returned from the web service; &lt;/li&gt;

  &lt;li&gt;succeededCalback: callback function which will be invoked when the data is returned from the server and has already been passed to the property; &lt;/li&gt;

  &lt;li&gt;failedCallback: function invoked when the remote call ends up generating an error; &lt;/li&gt;

  &lt;li&gt;timeout: timeout defined in ms; &lt;/li&gt;

  &lt;li&gt;userContext: object which will be passed to the callback method when the DataContext receives a response from the server side. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order for the previous client code work, we need to update our server side service’s &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx"&gt;code&lt;/a&gt; by adding the GetContactsFor method. Once again, and since is just demo code, I’ll implement in the easiest way possible: I’ll make it return a collection with a single predefined contact:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;[&lt;/span&gt;&lt;span style="color:#009b00;"&gt;OperationContract&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;]
&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;public &lt;/span&gt;&lt;span style="color:#80ff80;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color:#80ff00;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#009b00;"&gt;Contact&lt;/span&gt;&lt;span style="color:#80ff00;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color:#e8e8e8;"&gt;GetContactsFor&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#ff8080;"&gt;Int32 &lt;/span&gt;&lt;span style="color:#e8e8e8;"&gt;id&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;) {
    &lt;/span&gt;&lt;span style="color:#d6d694;"&gt;return new &lt;/span&gt;&lt;span style="color:#009b00;"&gt;List&lt;/span&gt;&lt;span style="color:#80ff00;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#009b00;"&gt;Contact&lt;/span&gt;&lt;span style="color:#80ff00;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{
        &lt;/span&gt;&lt;span style="color:#d6d694;"&gt;new &lt;/span&gt;&lt;span style="color:#009b00;"&gt;Contact&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{ 
                &lt;/span&gt;&lt;span style="color:#e8e8e8;"&gt;Type &lt;/span&gt;&lt;span style="color:#80ff00;"&gt;= &lt;/span&gt;&lt;span style="color:#f07b21;"&gt;ContactType&lt;/span&gt;&lt;span style="color:#80ff00;"&gt;.&lt;/span&gt;&lt;span style="color:#e8e8e8;"&gt;Phone&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
                &lt;/span&gt;&lt;span style="color:#e8e8e8;"&gt;Ct  &lt;/span&gt;&lt;span style="color:#80ff00;"&gt;= &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;123123123&amp;quot;
        &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;}
    };
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And there you go: you don’t need anything else for making the DataContext component get the value of the contacts property lazily. And that’s it for now. Stay tuned for more on MS AJAX.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1738278" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>The DataContext component: linking objects</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/05/the-datacontext-component-linking-objects.aspx</link><pubDate>Thu, 05 Nov 2009 11:48:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737766</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1737766</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/05/the-datacontext-component-linking-objects.aspx#comments</comments><description>&lt;p&gt;Today we’ll keep going in our study of the DataContext component and we’ll see how we can establish relationships between objects through links. I guess you’re probably asking something like: “Hum…establishing relationships between objects…how about using properties?” For instance, suppose we’re working with person and contact instances from the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/the-datacontext-component-improving-the-identity-algorithm.aspx"&gt;post&lt;/a&gt;…We can say that a person has one contact and that is easily expressed through JavaScript which looks like this:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;personWithContact = {
  id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;6&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
  name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;dumb&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;,
  contact: { value: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;1&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, contact: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;123123123&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;}
};&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now, this is perfectly good code…really…so, why would I want to use links then? Glad you asked :)&lt;/p&gt;

&lt;p&gt;Before going on, I’d like to point out that this is a feature which was added in order to support ADO.NET Data Services. Fortunately, the team wrote it in a generic way so that you could use it even if you’re not using ADO.NET Data Services (I said fortunately because you can reuse this feature if you want). But I’m divagating…why would we use links? For bookkeeping, of course! Whenever you create a link between two objects, you end up setting a property in the source and you’ll also get an entry in the changes array (internal _changelist array). &lt;/p&gt;

&lt;p&gt;When working with links, you’ve got several methods which you can use:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;addLink: you should use this method when you need to add items to a collection. For instance, suppose you’ve got a property which is an array and you want to add an object to that array. The addLink method is the way to go; &lt;/li&gt;

  &lt;li&gt;setLink: also used for adding a new link between two objects. This is the method you should use when you’re thinking of “simple” properties (instead of collections); &lt;/li&gt;

  &lt;li&gt;removeLink: used for deleting an existing link between two objects. You should only use this method when you’re removing an item from a collection &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As I’ve said above, you should keep in mind that these methods end up changing the internal changes list maintained by the DataContext control. Establishing and cancelling links produces different results when you’re working with single properties and collections. For instance, take a look at the following code (based in code presented in the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/regular-expressions-and-javascript.aspx"&gt;post&lt;/a&gt;):&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;newPerson = { name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;joseph&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, &lt;br /&gt;                  contacts: [] };
&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ct1 = { value: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;1&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, contact: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;123123123&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;};
&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ct2 = { value: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;2&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, contact: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;222333444&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;};
&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx.addLink(newPerson, &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;contacts&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, ct1);&lt;/span&gt;&lt;span style="color:#006400;"&gt;//collection
&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx.setLink(newPerson, &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;contact&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, ct2);&lt;/span&gt;&lt;span style="color:#006400;"&gt;//single prop
&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx.setLink(newPerson, &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;contact&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);&lt;/span&gt;&lt;span style="color:#006400;"&gt;//removing single
&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx.removeLink(newPerson, &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;contacts&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, ct1);&lt;/span&gt;&lt;span style="color:#006400;"&gt;//removing coll.&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In the previous snippet, we’ve introduced a new person instance and several contacts in order to show you how to establish links between the objects. If you stop the code in the addLink method, you’ll see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/4135.addlink_5F00_77ED1156.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="addlink" border="0" alt="addlink" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/6675.addlink_5F00_thumb_5F00_63874BCB.jpg" width="313" height="350" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see, establishing the link through the add method ended up adding the contact to the contacts array of the person object and inserting a new entry in the changes list. If you stop the code’s execution after the setLink line, you’ll see something similar (depending on what changes were made to the DataContext):&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/4137.addlink2_5F00_2CC9F0C7.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="addlink2" border="0" alt="addlink2" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/4135.addlink2_5F00_thumb_5F00_7FD48DEB.jpg" width="325" height="338" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the main difference is that setLink ends up setting an item instead of adding it to a collection. Canceling a relationship established through the setLink method involves using that method again, but passing null instead of a valid value. On the other hand, removing an item from the collection should always be done with the removeLink method. Interestingly, these methods produce different results in this scenario (don’t forget that we still haven’t committed the changes, ie, we still haven’t committed our setLink and addLink changes). Take a look at the following picture to see what I mean:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/8741.addlink3_5F00_22491CA7.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="addlink3" border="0" alt="addlink3" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/1805.addlink3_5F00_thumb_5F00_7553B9CB.jpg" width="315" height="336" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see, the removeLink call ended up deleting the previous addLink entry (while also removing the entry from the contacts array). However, the setLink method doesn’t affect the changes array in the same way; instead, it will simply update the existing entry so that it points to the new target that has been passed to it. Notice that this means that you’ll have to pay attention to this if you’re writing custom code in the server side which is responsible for persisting the changes.&lt;/p&gt;

&lt;p&gt;And I guess this sums it up quite nicely. but there’s still more to discuss…so stay tuned for more on MS AJAX.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737766" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Regular expressions and JavaScript – part II</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/05/regular-expressions-and-javascript-part-ii.aspx</link><pubDate>Thu, 05 Nov 2009 09:27:51 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737760</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1737760</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/05/regular-expressions-and-javascript-part-ii.aspx#comments</comments><description>&lt;p&gt;In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/regular-expressions-and-javascript.aspx"&gt;post&lt;/a&gt;, we’ve seen that we can build regular expression objections through a constructor ( RegExp) or by using a literal (/…/). Today I thought I’d be writing some code for illustrating the use of regular expressions. However, I think I’ll concentrate on MS AJAX and I’ll point you to this &lt;a href="http://www.regular-expressions.info/"&gt;site&lt;/a&gt; if you want to know more about regular expressions.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737760" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Regular expressions and JavaScript</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/04/regular-expressions-and-javascript.aspx</link><pubDate>Wed, 04 Nov 2009 15:00:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737630</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1737630</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/04/regular-expressions-and-javascript.aspx#comments</comments><description>&lt;p&gt;After writing my &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/04/getting-the-css-display-value-there-s-more-to-it-than-you-might-think-at-first.aspx"&gt;last post&lt;/a&gt; on getting style properties from JavaScript, I’ve noticed that I still haven’t written anything about JavaScript and regular expressions…and that’s why I’ll be writing a couple of posts on this topic. Before going on, a disclaimer: I’m not really an expert on regular expressions (do note that people have &lt;a href="http://www.amazon.com/Regular-Expressions-Cookbook-Jan-Goyvaerts/dp/0596520689/ref=sr_1_2?ie=UTF8&amp;amp;s=books&amp;amp;qid=1257344577&amp;amp;sr=8-2"&gt;written&lt;/a&gt; &lt;a href="http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1257344577&amp;amp;sr=8-1"&gt;books&lt;/a&gt; on them! and since we’re at it, I’m not really an expert in any field…but I’m really curious and that counts, right? :) ). However, I guess I’ll be able to share with you some of the basics about them I’ve picked along the years.&lt;/p&gt;  &lt;p&gt;I think I won’t be far from truth by saying that you can see regular expressions as a language which you can use to search, replace and extract data from/in an existing string. Unfortunately, regular expressions tend to be rather cryptic… and that means that they might be hard to read and/or write. A regular expression is always a string which might use one or more characters from a &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp"&gt;special characters&lt;/a&gt;’ list. Special characters are so called because they have a special meaning. For instance, \d means something like “match any digit”…I’m not going to show you all speciall characters here. Instead, I’ll redirect you to this &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp"&gt;page&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I’ve already mentioned that writing and reading complex regular expressions is not for those who have heart problems…the main problem I see with them is that, unlike traditional code, you can’t really use white spaces nor can you use comments for explaining something better (notice that I’m talking about the regular expression itself here). A regular expression string is really a compact string which gets harder and harder to read as you get better at understanding them…&lt;/p&gt;  &lt;p&gt;In JavaScript, regular expressions are represented by &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp"&gt;RegExp&lt;/a&gt; objects. Instantiating a RegExp object means that you’ll have to (at least!) pass an expression which identifies a pattern. If you want, you can also specify the type of operation which should performed by specifying an optional flags parameter (for instance, passing “i” to it means you don’t care about case). Here’s a quick example of a new Regular expression which will match strings which have at least 1 digit:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;reg = &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;new &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;RegExp(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;\\d&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
alert(reg.test(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;have 1 digit&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;));&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The previous snippet introduced one of the methods exposed by the &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp"&gt;RegExp class&lt;/a&gt;: the test method. You can use it for checking if the string that was passed to it matches the regular expression encapsulated by that &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp"&gt;RegExp&lt;/a&gt; object. There’s still one more interesting thing to notice about the previous snippet: we had to escape the \ char since we’re using a string to define the regular expression.&lt;/p&gt;

&lt;p&gt;Notice that JavaScript also supports the concept of regular expression literal. A regular expression literal is delimited by a pair of slashes (/reg. expression goes here/). For instance, the next snippet is equivalent to our first example:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;alert(/\d/.test(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;have 1 digit&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;));&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In case you’re wondering, the flag parameter can also be specified when using regular expression literals:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;alert(/\d/g.test(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;have 1 digit&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;));&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;If you need to, you can pass several flags by just putting the corresponding letters after the ending slash (don’t worry to much about these flags…we’ll come back to them in future pots). Most of the time, you’ll probably end up using regular expressions literals due to their compactness (when compared with the equivalent RegExp instantiation).&lt;/p&gt;

&lt;p&gt;As you can probably guess, the hardest part on using regular expressions is building the regular expression itself. And that’ what we’ll start looking at on the next post. Stay tuned for more on regular expressions and JavaScript.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737630" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Getting the CSS display value: there’s more to it than you might think at first</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/04/getting-the-css-display-value-there-s-more-to-it-than-you-might-think-at-first.aspx</link><pubDate>Wed, 04 Nov 2009 12:24:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737605</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1737605</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/04/getting-the-css-display-value-there-s-more-to-it-than-you-might-think-at-first.aspx#comments</comments><description>&lt;p&gt;Well, I’m spoiled… that’s what happens when you use frameworks like &lt;a href="http://www.jquery.com"&gt;JQuery&lt;/a&gt;…Everything is good until that one day…someone says something stupid: “I need you to write some JS code but I don’t want external references on &lt;a href="http://www.jquery.com"&gt;JQuery&lt;/a&gt;!”. Yep, I thought exactly the same thing: WTF???&lt;/p&gt;  &lt;p&gt;“Now, that we’re clear on external frameworks, can you please write me a function which&amp;#160; receives a reference to a DOM element and returns the value of its CSS display property?”, someone asked. “Sure! no problem”, I said…After all, getting the display is just a matter of getting the CSS’ display property and we all know that it’s readily available from the style object, right? So, here’s my first attempt on this problem:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;display(elem) {
  &lt;/span&gt;&lt;span style="color:#006400;"&gt;//basic parameter checking removed
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;elem.style.display;
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Simple, right…Then I though about writing a simple test. I’ve started with the following HTML:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;a1&amp;quot; &lt;/span&gt;&lt;span style="color:green;"&gt;style&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#009b00;"&gt;display&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;: none&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And now, the BLOCKED SCRIPT&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;alert(display(document.getElementById(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;a1&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;)));&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And yes, an alert with ‘none’ popped up…things were good…until that I’ve decided to add two more elements to my test page:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;a2&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;sss&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div &lt;/span&gt;&lt;span style="color:green;"&gt;id&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;a3&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;sss&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;div&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;How about testing CSS applied through style sheets:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;style &lt;/span&gt;&lt;span style="color:green;"&gt;type&lt;/span&gt;&lt;span style="color:#a1b070;"&gt;=&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
    &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;#a3{
        display: none;
    }
&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#d6d694;"&gt;style&lt;/span&gt;&lt;span style="color:#c3c567;"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And then, I’ve simply duplicated the alert calls:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;alert(display(document.getElementById(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;a2&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;)));
alert(display(document.getElementById(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;a3&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;)));&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;“And…wtf is going on here??? Where are the display values???” Yes, you’re screwed…style.display will only return a “valid” value when it’s explicitly set from JS or from the style attribute which is applied to the element (ie, the style attribute that you can use in the HTML tags). Now, how do we solve this? &lt;/p&gt;

&lt;p&gt;There is a way, but once again, we need to write some browser testing code&amp;#160; because different browsers use different approaches…I’m talking about the currentStyle vs getComputedStyle dilemma…Here’s my next attempt for a cross-browser solution (where cross-browser really means IE, FF, Chrome and Opera):&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;display(elem) {
  &lt;/span&gt;&lt;span style="color:#006400;"&gt;//basic parameter checking removed
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(elem.style.display) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;elem.style.display;
  }
  
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(elem.currentStyle) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;elem.currentStyle.display;
  }
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(document.defaultView.getComputedStyle) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;document.defaultView&lt;br /&gt;                   .getComputedStyle(elem, &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;)&lt;br /&gt;                   .getPropertyValue(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;display&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
  }
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now, before you start thinking in adapting this code so that you can get any style property, there’s one additional detail you should keep in mind: Mozilla and Opera browsers expect CSS syntax while IE relies on JS names (ex.: background-color vs backgroundColor).&lt;/p&gt;

&lt;p&gt;In my opinion, the easies solution for this problem is require CSS names and adapt then, when needed, convert it to JS syntax. Here’s a possible solution which expects CSS property names syntax:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;display(elem, propName) {
  &lt;/span&gt;&lt;span style="color:#006400;"&gt;//basic parameter checking removed
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(elem.style.display) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;elem.style[propName];
  }
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(elem.currentStyle) {&lt;br /&gt;   &lt;span style="color:#006400;"&gt;//copied directly from JQuery’s source –:)&lt;/span&gt;
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;jsName = propName&lt;br /&gt;       .replace(/\-(\w)/g, &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(all, letter) {
           &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;letter.toUpperCase();
       });
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;elem.currentStyle[jsName];
  }
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(document.defaultView.getComputedStyle) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;document.defaultView
                   .getComputedStyle(elem, &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;)
                   .getPropertyValue(propName);
  }
  &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Well, even with this code, it’s still interesting to notice that if, for example, you set the background-color CSS property to red, you’ll get different results (some browsers return red while others return rgb(255, 0,0). You gotta love web development!!!&lt;/p&gt;

&lt;p&gt;I hope this imaginary scenario will help you (I should have probably said your boss!) understand that web development is more complicated than it seems at first…yes, we don’t have to do memory management (though we still need to pay attention to memory leaks), but there are still a ton of stuff to worry about…and yes, doing web development without using a modern JS framework (ex.: &lt;a href="http://www.jquery.com"&gt;JQuery&lt;/a&gt;) is pure suicide. DON’T DO IT!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737605" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>The DataContext component: improving the identity algorithm</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/04/the-datacontext-component-improving-the-identity-algorithm.aspx</link><pubDate>Wed, 04 Nov 2009 11:20:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737594</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1737594</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/04/the-datacontext-component-improving-the-identity-algorithm.aspx#comments</comments><description>&lt;p&gt;In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx"&gt;post&lt;/a&gt;, we&amp;rsquo;ve started looking at identity and how it&amp;rsquo;s supported by the DataContext component. Today we&amp;rsquo;ll improve the initial algorithm so that we&amp;rsquo;re able to support more advanced scenarios. &lt;/p&gt;
&lt;p&gt;Even though the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx"&gt;first algorithm&lt;/a&gt; for identity management was really simple, it ended up showing that identity management ends up adding (at least) two important problems:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;how to generate new identities; &lt;/li&gt;
&lt;li&gt;how to distinguish new entities from existing ones and how to distinguish different types of entities. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One possible solution for these problems relies on adding some sort of metadata to our objects. Going back to our &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx"&gt;initial example&lt;/a&gt;, we&amp;rsquo;ll start by adding an internal __meta object which will hold two important pieces of data:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the type of object; &lt;/li&gt;
&lt;li&gt;the identity of that object; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Notice that the identity of the object must be enough for identifying that object (even amongst objects of different &amp;ldquo;types&amp;rdquo;). Another important detail to keep in mind is that we need some way to identify the current type of an object (if we were using a strongly typed language, this wouldn&amp;rsquo;t be necessary, but with dynamic languages, it&amp;rsquo;s something that must also be tracked). It&amp;rsquo;s also important to understand that the objects that will be passed to the trackData method should have this info correctly filled up (unlike objects which will be inserted, where the metadata is supposed to be generated by the identity methods):&lt;/p&gt;
&lt;pre class="code" style="background-color:black;"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;people = [
    { id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;1&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;luis&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, &lt;br /&gt;        __meta: { set: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people$1&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;} },
    { id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;2&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;john&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, &lt;br /&gt;        __meta: { set: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people$2&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;} },
    { id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;3&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;peter&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, &lt;br /&gt;       __meta: { set: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people$3&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;} }
  ];&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;There&amp;rsquo;s not much we can say about the previous snippet. It represents a collection of person instances which have their __meta properties set up. Notice that from now on, the identity of each of these objects is given by the __meta.id value (instead of relying in the id field of the object).&lt;/p&gt;
&lt;p&gt;Our implementation of the identity related methods need to handle more details. Due to that, I&amp;rsquo;ve decided to create a helper object which encapsulates all the work needed for getting and creating new identities:&lt;/p&gt;
&lt;pre class="code" style="background-color:black;"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;entityManager = {
  metaCounter: {},
  getEntityId: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(dataContext, entity) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;entity.__meta &amp;amp;&amp;amp; entity.__meta.id ?
           entity.__meta.id :
           &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;null&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
  },
  getNewIdentity: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(dataContext, entity, entitySet) {
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(!entitySet) {
      Error.invalidOperation(&lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;entity set must be defined&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
    }
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;_this =entityManager&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;createMetaInfo(entity, entitySet) {
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;generatedId = _this.metaCounter[entitySet] =
        _this.metaCounter[entitySet] !== undefined ?
            _this.metaCounter[entitySet] + 1 : &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;{
        set: entitySet,
        id: entitySet + &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;$new:&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;+ generatedId
      };
    }
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(!(entity.__meta &amp;amp;&amp;amp; _entity.__meta.id)) {
      entity.__meta = createMetaInfo(entity, entitySet);
    }
    &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;entity.__meta.id;
  }
};&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;A couple of observations regarding the previous helper:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the identity of an item relies on the __meta.id property. As you can see, identity relies on concatenating IDs with the type of the current object; &lt;/li&gt;
&lt;li&gt;new identities must be different from those that belong to objects which have already been persisted (probably in a db on the server side). That&amp;rsquo;s why new identity use the $new instead of using only $ (I think this is easy to handle on the server side); &lt;/li&gt;
&lt;li&gt;I&amp;rsquo;ve introduced the metaCounter object so that we can reuse this object for allowing the insertion of different types of objects. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now, we can start adding several kinds of objects without any problem. For instance, take a look at the following code:&lt;/p&gt;
&lt;pre class="code" style="background-color:black;"&gt;&lt;span style="color:#a79a86;"&gt;ctx.insertEntity({ name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;joseph&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;}, &lt;br /&gt;                   &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
ctx.insertEntity({ name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;joseph&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;}, &lt;br /&gt;                   &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;people&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);
ctx.insertEntity({ value: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;1&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, contact: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;123123123&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;}, &lt;br /&gt;                   &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;contacts&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the next image shows the final result:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/3554.betteralgorithm_5F00_006E477C.jpg"&gt;&lt;img height="455" width="357" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/0728.betteralgorithm_5F00_thumb_5F00_32F1A4EE.jpg" alt="betteralgorithm" border="0" title="betteralgorithm" style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;As you can see, with this approach we no longer set the id property of a new person object (that will be done by the server side, when it process an eventual commit performed by the DataContext instance). &lt;/p&gt;
&lt;p&gt;Notice that until now we&amp;rsquo;ve seen the client code needed for supporting addition of new items. A complete solution would still need more work in the server side in order to ensure that items are correctly persisted. On the next post, we&amp;rsquo;ll start looking at links between objects. Stay tuned for more on MS AJAX.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737594" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>The DataContext component: getting started with identities</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx</link><pubDate>Tue, 03 Nov 2009 12:36:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737331</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1737331</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/11/03/the-datacontext-component-getting-started-with-identities.aspx#comments</comments><description>&lt;p&gt;We’ve already met several interesting features of the DataContext component. Today we’ll keep going and we’ll start discussing identity. I’m not sure if you’ve noticed, but the DataContext exposes several identity related methods. For instance, the getIdentity method is used by the control to get the identity of an object.&lt;/p&gt;  &lt;p&gt;In order for you to activate identity related features, you need to (at least) configure the component so that it knows how to get or create a new identity for an object. This is done by setting two properties:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;getIdentityMethod: function used for getting the identity of an object. This method expects two parameters: the first references the DataContext; the second, references the entity you’re interested in getting the ID of; &lt;/li&gt;    &lt;li&gt;getNewIdentityMethod: function which is used for creating a new identity for an object. This method expects three parameters: the first two match the getIdentityMethod’s parameters; the last one is an optional string which identifies the type of object that is asking for a new identity. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Setting these properties is all that is need for activating identity support for a DataContext component. Notice that using identities means&amp;#160; that you’ll end up using the internal _items object for storing all the objects maintained in the current DataContext component. This object is used as a dictionary, where the identity of each object is used as the key of a specific entry. In practice, this means that you can mix object of different types in the same _items dictionary (we’ll talk about this in a future post).&lt;/p&gt;  &lt;p&gt;As you’ve probably guessed, identity is one of those features which exists (mainly) for supporting ADO.NET data service. However, nothing prevents us from exploring it, right? So let’s get starting…and we’ll start simple…Suppose you want to track changes to the array presented in the next snippet:&lt;/p&gt;  &lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;people = [
    { id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;1&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;luis&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;},
    { id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;2&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;john&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;},
    { id: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;3&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;peter&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;}
  ];&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In this case, things are pretty simple, right? We’re only interested in managing simple objects (lets call them person instances) and that means that we can use the id value as the identity of each object. There’s still one little problem: what to do with new items? &lt;/p&gt;

&lt;p&gt;In this case, and since we’re supposed to create a function for calculating new identities, we can write it so that it also sets the id of any new person object. Here’s the code needed for instantiating a DataContext component which does exactly that:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx = Sys.create.dataContext(
  {
    getIdentityMethod: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(dataContext, entity) {
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;entity.id;
    },
    getNewIdentityMethod: &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;function &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;br /&gt;                          dataContext, entity, entitySet) {
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(entity.id === undefined) {
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;contextItems = ctx.get_items();
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;max = &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;0&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
        &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;for &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(&lt;/span&gt;&lt;span style="color:#a89a86;"&gt;var &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;id &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;in &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;contextItems) {
          &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;if &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;(parseInt(id) &amp;gt; max) {
            max = parseInt(id);
          }
        }
        entity.id = max + &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;1&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;;
      }
      &lt;/span&gt;&lt;span style="color:#a89a86;"&gt;return &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;entity.id;
    }
  }
);&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;ctx.trackData(people);&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;With these properties sets, we can add new objects without having to worry with the ID of a new person instance:&lt;/p&gt;

&lt;pre style="background-color:black;" class="code"&gt;&lt;span style="color:#a79a86;"&gt;ctx.insertEntity({ name: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;joseph&amp;quot;&lt;/span&gt;&lt;span style="color:#a79a86;"&gt;, address: &lt;/span&gt;&lt;span style="color:#44b1e3;"&gt;&amp;quot;fx&amp;quot; &lt;/span&gt;&lt;span style="color:#a79a86;"&gt;});&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The next image shows the changes made to the DataContext instance after the previous insert:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/1805.identitychanges_5F00_1C64CCAF.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="identitychanges" border="0" alt="identitychanges" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/6114.identitychanges_5F00_thumb_5F00_6F0336DE.jpg" width="306" height="404" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;div&gt;
  &lt;p&gt;As you can see, identity activation leads to several changes:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;new elements get added to the private _items dictionary with the new identity used as the key;&lt;/li&gt;

    &lt;li&gt;the new inserted object is updated: a new id property is added and set to the new identity which was calculated by the DataContext’s newIdentityMethod function;&lt;/li&gt;

    &lt;li&gt;the _inserts array references only the id of the item that was inserted (notice the id$4 entry in the previous image). &lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;The example we’ve seen is really intended to get you started with identities. Unfortunately, things aren’t so easy in the real world. there will be times where you can’t really set the ID of an item (ex.: suppose it gets mapped to an auto number field of a table). There will also be other times where you need to support several “types” of objects…in these scenarios, you’ll need a more advanced way to identifying each instance. &lt;/p&gt;

  &lt;p&gt;In the next post we’ll take a look at more advanced ways of generating IDs for these scenarios. Stay tuned for more on MS AJAX.&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737331" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MS+AJAX/default.aspx">MS AJAX</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/Javascript/default.aspx">Javascript</category></item></channel></rss>