<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Visoft, Inc. Blogs &#187; REST</title>
	<atom:link href="http://blogs.visoftinc.com/category/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.visoftinc.com</link>
	<description>.NET, ASP.NET, AJAX, Ruby and more</description>
	<lastBuildDate>Mon, 12 Mar 2012 18:05:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>Introducing a Ruby OData Client Library</title>
		<link>http://blogs.visoftinc.com/2010/06/12/introducing-a-ruby-odata-client-library/</link>
		<comments>http://blogs.visoftinc.com/2010/06/12/introducing-a-ruby-odata-client-library/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 04:37:28 +0000</pubDate>
		<dc:creator>Damien White</dc:creator>
				<category><![CDATA[OData]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby_odata]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[WCF Data Services]]></category>
		<category><![CDATA[Web Services]]></category>
		<guid isPermaLink="false">/archive/2010/06/12/Introducing-a-Ruby-OData-Client-Library.aspx</guid>
		<description><![CDATA[Ever since the ADO.NET team started development on Astoria (pre-release), I have loved the concept.&#160; Since its release (it was called ADO.NET Data Services and is now WCF Data Services), I’ve used it a ton (you may remember the example from this post).&#160; Back at MIX10, Microsoft announced a commitment to the Open Data Protocol [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.odata.org/"><img style="border-right-width: 0px; margin: 20px 10px 20px 20px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="OData_logo_MS_small" border="0" alt="OData_logo_MS_small" align="right" src="http://blogs.visoftinc.com/wp-content/uploads/OData_logo_MS_small.png" width="240" height="29" /></a> Ever since the ADO.NET team started development on Astoria (pre-release), I have loved the concept.&#160; Since its release (it was called ADO.NET Data Services and is now WCF Data Services), I’ve used it a ton (you may remember the example from <a href="http://blogs.visoftinc.com/archive/2009/05/27/ASP.NET-4.0-AJAX-Preview-4-Data-Binding.aspx">this post</a>).&#160; Back at MIX10, Microsoft announced a commitment to the <a href="http://www.odata.org/" target="_blank">Open Data Protocol</a> (<a href="http://www.odata.org/" target="_blank">OData</a>).&#160; <a href="http://msdn.microsoft.com/en-us/library/cc668792(VS.100).aspx" target="_blank">WCF Data Services</a> enables you to create services that use OData to expose and consume data, both with .NET 4 and .NET 3.5 SP1.&#160; OData services are very powerful and there are quite a few <a href="http://www.odata.org/producers" target="_blank">live producers</a> such as <a href="http://odata.netflix.com/Catalog/" target="_blank">Netflix</a> and <a href="http://www.nerddinner.com/Services/OData.svc/" target="_blank">Nerd Dinner</a>.</p>
<p>OData is so powerful because it’s REST based and you can access the services from just about everywhere, including just a simple URL.&#160; For example, using the Netflix OData service to access the best movie ever made by title: <a href="http://odata.netflix.com/Catalog/Titles?$filter=Name eq 'Office Space'">http://odata.netflix.com/Catalog/Titles?$filter=Name eq &#8216;Office Space&#8217;</a>.&#160; Pretty simple.&#160; Of course accessing things solely based on URLs in code isn’t the best, and hence the reason for my post.</p>
<h2>Client Libraries</h2>
<p>When using OData, it’s convenient to utilize an SDK to access the services.&#160; For example, in <a href="http://msdn.microsoft.com/en-us/library/cc838234(VS.96).aspx" target="_blank">Silverlight</a> you can access <a href="http://msdn.microsoft.com/en-us/library/ff650916(v=VS.96).aspx" target="_blank">OData services using LINQ</a>.&#160; There’s also a fantastic <a href="http://www.asp.net/ajaxLibrary/odata.ashx" target="_blank">AJAX Library</a> (which I used in a <a href="http://blogs.visoftinc.com/archive/2009/05/27/ASP.NET-4.0-AJAX-Preview-4-Data-Binding.aspx" target="_blank">previous post</a>).&#160; There are SDKs for <a href="http://odataphp.codeplex.com/" target="_blank">PHP</a>, <a href="http://odataobjc.codeplex.com/" target="_blank">Objective-C</a>, and many others, but there was one missing that I wanted to use… Ruby.&#160; I am assuming this comes as no surprise given my new found <a href="http://blogs.visoftinc.com/category/Ruby.aspx">love for Ruby</a>.</p>
<p><span id="more-71"></span><br />
<h2>Introducing ruby_odata</h2>
<p>I’ve been messing around with Ruby enough to start working on my first Ruby open-source project.&#160; It’s always good to contribute to the open-source community.&#160; While I’m not a seasoned Ruby developer, I figure this is a good project to start on.&#160; Now I can code stuff in Ruby and consume WCF services I have written in the past and will continue to write in the future (that is until there is “OData on <a href="http://rubyonrails.org/" target="_blank">Rails</a>,” perhaps an idea for a new project?).</p>
<p>The source code, like all good Ruby code, is <a href="http://github.com/visoft/ruby_odata" target="_blank">hosted on GitHub</a>.&#160; The <a href="http://github.com/visoft/ruby_odata/blob/master/README.rdoc" target="_blank">README</a> will give you a good idea of how to use the library.&#160; Currently (as of 06/12/2010) there are limitations, such as not being able to batch changes (like multiple Adds), but no worries, I’m working on adding them (actually, I cut three releases today, adding extra querying abilities).&#160; That said, it is functional enough to use for most operations right now.&#160; Once it’s been better tested in the real world and I’ve been able to incorporate most if not all of the OData functionality, I will be versioning more responsibly.&#160; Right now, because I’m adding functionality quickly and I want others to be able to use it, you’ll be seeing lots of updates so the version number will be a bit jumpy for a while.</p>
<h2>The Genesis of ruby_odata</h2>
<p>I started writing ruby_odata in order to interface with my database via services so I could do testing in Ruby and create objects from within the tests that I could use in my ASP.NET MVC application.&#160; I wrote about BDD testing in <a href="http://blogs.visoftinc.com/archive/2010/06/10/Behavior-Driven-Development-(BDD)-with-Cucumber-and-ASP.NET-MVC.aspx">an earlier post</a> where I fixed the speed of running the tests using Cucumber with <a href="http://github.com/tenderlove/mechanize" target="_blank">Mechanize</a>.&#160; Another area missing in .NET is the ability to automatically generate objects to test against like you can with <a href="http://github.com/ianwhite/pickle" target="_blank">Ian White’s Pickle</a>.&#160;&#160; <a href="http://github.com/ianwhite/pickle" target="_blank">Pickle</a> interacts nicely with factories and your database to create objects for tests.&#160; For example, a step like: <span style="font-family: courier new">Given a user exists with name: &quot;Damien&quot; </span>will automatically create a new user in the test database.&#160; This works dandy in the Ruby world (with <a href="http://rails.rubyonrails.org/classes/ActiveRecord/Base.html" target="_blank">ActiveRecord</a> for example), but against something like Entity Framework, not so much.&#160; With <a href="http://github.com/visoft/ruby_odata" target="_blank">ruby_odata</a> however, this is something that could work in a similar way. (Yet another idea for a project, a Pickle adapter that utilizes ruby_odata.&#160; Man, so many ideas, so little time.)</p>
<h2>Usage Overview</h2>
<p>The library is pretty straightforward to use.&#160; At the heart is the <strong>OData::Service</strong>.&#160; This is the root object that drives all of your operations against the services.&#160;&#160; Once you instantiate the <strong>OData::Service</strong>, dynamic methods based on the OData service’s collections are available to call.&#160; For example, using the <a href="http://odata.netflix.com/Catalog/" target="_blank">Netflix OData service</a>, we can see there is a collection called “Titles.”&#160; Within ruby_odata, the <strong>OData::Service </strong>will respond to a “Titles” method:</p>
<pre class="brush: ruby;">svc = OData::Service.new &quot;http://odata.netflix.com/Catalog/&quot;
puts &quot;Responds to Titles? #{svc.respond_to? :Titles}&quot; # =&gt; Responds to Titles? true</pre>
<p>
  <br />So, just by looking at the collections on the <a href="http://odata.netflix.com/Catalog/" target="_blank">main service page</a>, or <a href="http://odata.netflix.com/Catalog/$metadata" target="_blank">metadata page</a>, you can see what you can query.&#160; Here’s an example of querying for a movie (like we did earlier with a straight up URL):</p>
<p></p>
<pre class="brush: ruby;">svc.Titles.filter(&quot;Name eq 'Office Space'&quot;)
movie = svc.execute
# Now we can access properties of the Title (defined in the service metadata)
puts &quot;Average Rating: #{movie.AverageRating}&quot;
puts &quot;Release Year: #{movie.ReleaseYear}&quot;
puts &quot;URL: #{movie.Url}&quot;
# =&gt; Average Rating: 4.2
# =&gt; Release Year: 1999
# =&gt; URL: http://www.netflix.com/Movie/Office_Space/20358351</pre>
<p>Pretty cool, huh?</p>
<h2>The Basics</h2>
<p>There is a simple pattern for using the library.&#160; For queries, you call a collection on the service (like you saw with <strong>Titles</strong> in the Netflix example above), and that returns an <strong>OData::QueryBuilder</strong> object to you in which you can fluently add options like <strong>filter</strong> or <strong>expand</strong>.&#160; If you use multiple calls to filter, it will perform an “AND” query against the service, and multiple expand calls will yield multiple items being expanded.&#160; You can also mix the calls, like <strong>.filter(…).expand(…)</strong>.&#160; Once you are done building a query, you call the <strong>execute</strong> method on the service.&#160; The result of the execute is either a single entity or a collection of the entities based on your query.</p>
<p>In order to add, update, or delete, you need to call the appropriate method on the <strong>OData::Service</strong>.&#160; To add, you would use the dynamic method <strong>AddTo&lt;Collection Name&gt;</strong>, where the &lt;Collection Name&gt; is the name of the collection on the service (as defined by the metadata).&#160; To update, you would use <strong>update_object,</strong> passing in the updated version of the object.&#160; Finally, to delete, you would use <strong>delete_object</strong>, passing the object to delete.&#160; Once you do one of these operations, you need to persist the change using the <strong>save_changes</strong> method on the <strong>OData::Service</strong>.&#160; Currently, you need to call save_changes after each operation in order to persist the data.&#160; In the future you will be able to batch operations instead of persisting them one at a time.</p>
<h2>More Examples</h2>
<p>For more examples, download the <a href="http://github.com/visoft/ruby_odata" target="_blank">source code</a> from GitHub and setup the testing environment (instructions for setting up the test environment are located in the <a href="http://github.com/visoft/ruby_odata/blob/master/README.rdoc" target="_blank">README</a>).&#160; When you setup the test environment, you can follow along with the samples as shown in the <a href="http://github.com/visoft/ruby_odata/blob/master/README.rdoc" target="_blank">README</a>.&#160; There are examples for all the CRUD operations.</p>
<h2>Conclusion</h2>
<p>There you have it, an OData library for Ruby.&#160; I am very excited by the potential of <a href="http://www.odata.org/" target="_blank">OData</a>, and having the ability to consume the services easily within Ruby is extremely useful.&#160; I hope you find the library helpful.&#160; The current version is a bit of a work in progress since everything isn’t fully supported yet, but I will continue to work on the project.&#160; You can get involved by checking out the resources for the project:</p>
<ul>
<li>Source Code (hosted on GitHub): <a title="http://github.com/visoft/ruby_odata" href="http://github.com/visoft/ruby_odata">http://github.com/visoft/ruby_odata</a> </li>
<li>Documentation (hosted on rdoc.info): <a title="http://rdoc.info/projects/visoft/ruby_odata" href="http://rdoc.info/projects/visoft/ruby_odata">http://rdoc.info/projects/visoft/ruby_odata</a> </li>
<li>Issue tracking (hosted on GitHub): <a title="https://github.com/visoft/ruby_odata/issues" href="https://github.com/visoft/ruby_odata/issues">https://github.com/visoft/ruby_odata/issues</a></li>
<li>Wiki (hosted on GitHub): <a title="http://wiki.github.com/visoft/ruby_odata/" href="http://wiki.github.com/visoft/ruby_odata/">http://wiki.github.com/visoft/ruby_odata/</a> </li>
</ul>
<p>If you just want to start using it without the source code, you can install ruby_odata as a <a href="http://rubygems.org/gems/ruby_odata" target="_blank">gem from RubyGems.org</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.visoftinc.com/2010/06/12/introducing-a-ruby-odata-client-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET 4.0 AJAX &#8211; Preview 4 &#8211; Client Templates</title>
		<link>http://blogs.visoftinc.com/2009/04/28/asp-net-4-0-ajax-preview-4-client-templates/</link>
		<comments>http://blogs.visoftinc.com/2009/04/28/asp-net-4-0-ajax-preview-4-client-templates/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 00:21:39 +0000</pubDate>
		<dc:creator>Damien White</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[ASP.NET AJAX]]></category>
		<guid isPermaLink="false">/archive/2009/04/28/ASP.NET-4.0-AJAX-Preview-4-Client-Templates.aspx</guid>
		<description><![CDATA[A little over a month ago, Microsoft released the fourth preview of ASP.NET 4.0 AJAX.&#160; This is the new release of the Microsoft AJAX Framework that will be released with ASP.NET 4.0.&#160; We’re getting closer to the RTM release of .NET 4.0 (hopefully later this year), so I figured this would be a good time [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-right-width: 0px; margin: 0px 10px 5px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Gears" border="0" alt="Gears" align="left" src="http://blogs.visoftinc.com/wp-content/uploads/WindowsLiveWriter/ASP.NETAJAX4.0Preview4ClientTemplates_13796/image_11.png" width="244" height="163" />A little over a month ago, Microsoft released the fourth preview of ASP.NET 4.0 AJAX.&#160; This is the new release of the Microsoft AJAX Framework that will be released with ASP.NET 4.0.&#160; We’re getting closer to the RTM release of .NET 4.0 (hopefully later this year), so I figured this would be a good time to start posting about ASP.NET 4.0 AJAX.&#160; There are some very exciting things coming along with ASP.NET 4.0, and the ASP.NET AJAX component is no exception.&#160; The best news about ASP.NET AJAX is that you can start using it today without having to wait for the full blown ASP.NET AJAX framework.&#160; Keep in mind that these components are still in &quot;preview&quot; mode (meaning no Microsoft support), though they are usable at your own risk.&#160; For more information, you can check out the <a href="http://aspnet.codeplex.com/license" target="_blank">license</a> on CodePlex.</p>
<p>In this post, I’ll be talking about the client templates that can be found in the latest release.&#160; Client templates are very powerful.&#160; They are a key component for rich client-side applications.&#160; Up until now, I have been using <a href="http://jtemplates.tpython.com/" target="_blank">jTemplates</a>, a client template solution plugin for jQuery.&#160; If you aren’t familiar with jTemplates, check out Dave Ward’s article “<a href="http://encosia.com/2008/06/26/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/">Use jQuery and ASP.NET AJAX to build a client side Repeater</a>.”&#160; jTemplates is really a nice plugin, but it’s a bit disconnected from the flow of the page.&#160; Enter ASP.NET 4.0 AJAX and the DataView control.</p>
<p><span id="more-29"></span></p>
<h2>Introducing the Sys.UI.DataView Control </h2>
<p>The ASP.NET AJAX DataView (not to be confused with the ADO.NET DataView), is a new control that functions similar to a server-side repeater as you will see.&#160; The first thing to note with the DataView is that this control operates without ASP.NET, meaning you can use this in any framework or even a straight HTML page.&#160; This shouldn’t be too much of a surprise, since you can download the ASP.NET AJAX Library 3.5 as <a href="http://www.asp.net/AJAX/downloads/" target="_blank">standalone scripts</a> today.&#160; Having said that, let’s start with an simple example that will just run in a standard HTML page.&#160; For this first example, I will walk through declaratively setting up template binding.&#160; Let’s get started…</p>
<p>Want to follow along with the examples? <a href="http://192.168.1.162/wp-content/uploads/WindowsLiveWriter/ASP.NETAJAX4.0Preview4ClientTemplates_13796/DataViewSamples_1.zip">Download Them &#8211; 88K</a></p>
<h3>DataView and Declarative Binding</h3>
<p>In order to use the Microsoft AJAX Templates, you need to first reference two scripts, MicrosoftAjax.js (or MicrosoftAjax.debug.js) and MicrosoftAjaxTemplates.js (or MicrosoftAjaxTemplates.debug.js).&#160; These files can be found in the <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645" target="_blank">ASP.NET Ajax Preview 4.zip</a> file from CodePlex.&#160; These would be added to the head of your page.</p>
<pre class="brush: xml;">&lt;script type=&quot;text/javascript&quot; src=&quot;scripts/MicrosoftAjax.debug.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;scripts/MicrosoftAjaxTemplates.debug.js&quot;&gt;&lt;/script&gt;</pre>
<p>Next, we’ll move on to the body.&#160; In the body tag, you need to specify a few namespaces and add a command, as shown in the snippet below.</p>
<pre class="brush: xml;">&lt;body   xmlns:sys=&quot;javascript:Sys&quot;
        xmlns:dataview=&quot;javascript:Sys.UI.DataView&quot;
        sys:activate=&quot;*&quot;&gt;
    ...
&lt;/body&gt;</pre>
<p>The first namespace being registered is “sys”.&#160; This is needed for all the templates, whether you activate them declaratively or imperatively.&#160;&#160; The sys prefix points to the Microsoft AJAX Library’s Sys class.&#160;&#160; The next two attributes are needed for declarative activation/binding.&#160; In the second xmlns attribute, we are registering the prefix “dataview.”&#160; This namespace refers to the Sys.UI.DataView AJAX control.&#160; The final attribute being added to the body tag is sys:activate.&#160; The value for sys:activate can be “*” which means “activate anything on the page with sys:attach (which we will discuss shortly).”&#160; Activating in this sense, means binding the templates when the page loads.&#160; Note, that instead of using an asterisk for sys:activate, you could also have a comma-delimited list of the ids to bind on page load.&#160; This is a better approach if you have a complex page since wildcards always seem to introduce some sort of delay.</p>
<p>Now that we have the framework setup, let’s move on to the actual template using the DataView.&#160; For this example, I will render an unordered list, but you can use any standard HTML markup that you want.&#160; To keep this example simple, I will bind to a JavaScript array defined in page, but later will discuss binding to a service.</p>
<pre class="brush: xml;">&lt;ul id=&quot;nflList&quot; class=&quot;sys-template&quot;
     sys:attach=&quot;dataview&quot;
     dataview:data=&quot;{{ nfcEast }}&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;{{ Homepage }}&quot;&gt;{{ Team }}&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</pre>
<p>Let’s take a closer look at what’s going on.&#160; Starting with the UL element, you can see the typical id and class attributes.&#160; You may think the class definition looks a bit peculiar, and you’d be correct.&#160; The class <strong>sys-template</strong> is used as a convention for hiding the template until binding occurs.&#160; This class needs to be defined somewhere in your stylesheet. </p>
<pre class="brush: css;">.sys-template { display:none; }</pre>
<p>Remember, you can always define multiple classes on an element by space-delimiting them.&#160; For example:</p>
<pre class="brush: xml;">&lt;ul class=”myClass sys-template” ...&gt;&lt;/ul&gt;</pre>
<p>Next, we have our sys:attach, which I briefly mentioned earlier.&#160; This property assigns the DataView&#160; to this element.&#160; This is equal to you manually wiring up the template using the <a href="http://msdn.microsoft.com/en-us/library/bb397487.aspx" target="_blank">$create shortcut</a> (which will be discussed in the DataView and Imperative Binding section).&#160; The sys:attach value is a reference to the namespace of dataview that we defined on the body element earlier.</p>
<p>The final attribute is dataview:data.&#160; As you may have guessed, this is a reference to the data source we need to bind to, and remember in this example I’m just binding to a JavaScript array.&#160; Note, the array name is wrapped in double curly-braces {{ nfcEast }}.&#160; I’m missing football season (Go Giants), so I decided to go with an NFL theme for the sample data, as you can see.</p>
<pre class="brush: js;">var nfcEast = [
    { Team: &quot;New York Giants&quot;, Homepage: &quot;http://www.giants.com/&quot; },
    { Team: &quot;Philadelphia Eagles&quot;, Homepage: &quot;http://www.philadelphiaeagles.com/&quot; },
    { Team: &quot;Dallas Cowboys&quot;, Homepage: &quot;http://www.dallascowboys.com/&quot; },
    { Team: &quot;Washington Redskins&quot;, Homepage: &quot;http://www.redskins.com/&quot; }
]</pre>
<p>Now on to the template itself.&#160; This is where we set up the binding of the properties.&#160; The {{ &#8230; }} blocks designate one-way / one-time binding.&#160; This syntax is very similar to the server side Eval() statements that we use when binding in ASP.NET.&#160; When this is bound, the DataView acts like a repeater and creates a new &lt;li&gt;&lt;/li&gt; for each row of data.&#160; You can see the rendered HTML in Figure 1.&#160; The output is shown in Figure 2.</p>
<p>&#160;<img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure 1: The Rendered HTML After Binding (From FireBug)" border="0" alt="Figure 1: The Rendered HTML After Binding (From FireBug)" src="http://blogs.visoftinc.com/wp-content/uploads/WindowsLiveWriter/ASP.NETAJAX4.0Preview4ClientTemplates_13796/image10.png" width="485" height="181" /></p>
<p><strong>Figure 1: The Rendered HTML After Binding (From FireBug)</strong></p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure 2: The Output" border="0" alt="Figure 2: The Output" src="http://blogs.visoftinc.com/wp-content/uploads/WindowsLiveWriter/ASP.NETAJAX4.0Preview4ClientTemplates_13796/image14.png" width="262" height="130" />&#160; <br /><strong>Figure 2: The Output</strong></p>
<h3>DataView and Imperative Binding</h3>
<p>In the last example, I showed how you can use declarative binding and the DataView control.&#160; Let’s now look at how the code looks if we decide to wire things up ourselves.&#160; I will keep everything the same as the last example so that you can clearly see the differences.</p>
<p>First, let’s review the changes to the HTML code:</p>
<pre class="brush: xml;">&lt;body xmlns:sys=&quot;javascript:Sys&quot;&gt;
    &lt;ul id=&quot;nflList&quot; class=&quot;sys-template&quot;&gt;
        &lt;li&gt;&lt;a href=&quot;{{ Homepage }}&quot;&gt;{{ Team }}&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/body&gt; </pre>
<p>You’ll notice that body only has a namespace declaration for sys and the &lt;ul&gt;&lt;/ul&gt; now just has an id and class.</p>
<p>Now for the JavaScript to do the binding:</p>
<pre class="brush: js;">Sys.Application.add_init(appInit);function appInit() {
    $create(
        Sys.UI.DataView,
        { data: nfcEast },
        null,
        null,
        $get(&quot;nflList&quot;)
    );
} </pre>
<p>Not too much here, and it’s standard code ASP.NET AJAX is today.&#160; First, we are wiring up a handler (the appInit function) for the application init event.&#160; This init event is raised after all scripts have been loaded, but before objects are created.&#160; In the appInit function, we are simply manually wiring up the Sys.UI.DataView control as we would do with an ASP.NET AJAX control in the current ASP.NET AJAX library.&#160; The $create method takes five parameters, the signature is <a href="http://msdn.microsoft.com/en-us/library/bb397487.aspx" target="_blank">$create</a>(Type type, properties, events, references, element).&#160; For the parameters, we start by passing the Sys.UI.DataView for the type.&#160; Next, we have an object literal of the property and the value, in this case we are setting the data property with our array.&#160; There are no events or references needed, so those values are simply null.&#160; Finally, we need to pass the element using the <a href="http://msdn.microsoft.com/en-us/library/bb397717.aspx" target="_blank">$get shortcut</a> (which does a document.getElementById under the covers).</p>
<p>Running this code is identical to the declarative method, in that on the application initialization, the template is filled with data.</p>
<p>One other useful function is replacing the data in your template.&#160; This can simply be done using the syntax:</p>
<pre class="brush: js;">$find(&quot;ID OF THE ELEMENT&quot;).set_data(THE JSON DATA);</pre>
<h2>Binding the DataView to a WCF REST Service</h2>
<p>As promised earlier, let’s walk through binding a DataView client-template to a WCF Service.&#160; The DataView can bind to any <a href="http://www.json.org/" target="_blank">JSON</a> serialized object.&#160;&#160; For this particular example, I will use a REST WCF service hosted in an ASP.NET Website.&#160;&#160; If you are not familiar with WCF and REST services, have a look at <a href="http://blogs.conchango.com/anthonysteele/archive/2008/03/15/rest-from-wcf-3-5.aspx" target="_blank">this</a> or <a href="http://www.code-magazine.com/Article.aspx?quickid=080014" target="_blank">this</a> article.&#160; First, let’s take a look at the service itself.</p>
<p>In the .svc file, I’ve declared the ServiceHost:</p>
<pre class="brush: xml;">&lt;%@ ServiceHost Language=&quot;C#&quot; Service=&quot;SampleService&quot;
    CodeBehind=&quot;~/App_Code/SampleService.svc.cs&quot;
    Factory=&quot;System.ServiceModel.Activation.WebServiceHostFactory&quot; %&gt;</pre>
<p>The Factory declaration there is to eliminate the need of configuring the service in the web.config.&#160; The <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.webservicehostfactory.aspx" target="_blank">WebServiceHostFactory</a> automatically uses the WebHttpBinding and WebHttpBehavior.&#160; Now for the code…</p>
<pre class="brush: csharp;">[ServiceContract(Namespace = &quot;&quot;)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SampleService{
    public SampleService() {}
    [DataContract]
    public class NFLTeam
    {
        [DataMember]
        public string Team { get; set; }
        [DataMember]
        public string Homepage { get; set; }
    }
    [OperationContract]
    [WebGet(UriTemplate = &quot;getTeams&quot;,
            ResponseFormat = WebMessageFormat.Json)]
    public List&lt;NFLTeam&gt; GetTeams()
    {
        return new List&lt;NFLTeam&gt; {
            new NFLTeam { Team = &quot;New York Giants&quot;, Homepage = &quot;http://www.giants.com/&quot; },
            new NFLTeam { Team = &quot;Philadelphia Eagles&quot;, Homepage = &quot;http://www.philadelphiaeagles.com/&quot; },
            new NFLTeam { Team = &quot;Dallas Cowboys&quot;, Homepage = &quot;http://www.dallascowboys.com/&quot; },
            new NFLTeam { Team = &quot;Washington Redskins&quot;, Homepage = &quot;http://www.redskins.com/&quot; }
        };
    }
}</pre>
<p>Nothing really out of the ordinary going on in the code.&#160; I’m simply returning a generic list of objects for this example so that you don’t need a database or anything to run the samples if you download them.&#160; One thing to note if you are not familiar with REST services in WCF, the WebGet attribute specifies that if we are using a WebHttpBinding, this method can be called using the GET verb.&#160; As parameters, I have set the UriTemplate to be getTeams, this means that the URL to access the service will be ../SampleService.svc/getTeams.&#160; Finally, since the DataView binds to <a href="http://json.org/" target="_blank">JSON</a> objects, the ResponseFormat property specifies the result should be serialized as <a href="http://www.json.org/example.html" target="_blank">JSON</a>.</p>
<p>Now with our service in tack, we can work on the DataView code.&#160; Again, in this example I will show the setup in a simple HTML page, illustrating how the Microsoft ASP.NET AJAX 4.0 Library can be used without ASP.NET.&#160; I will skip the script and body declarations since I showed them in the previous examples.&#160; For this DataView, I will use an HTML TABLE and bind declaratively. </p>
<pre class="brush: xml;">&lt;table&gt;
    &lt;tbody id=&quot;nflListBody&quot; class=&quot;sys-template&quot;
         sys:attach=&quot;dataview&quot;
         dataview:autofetch=&quot;true&quot;
         dataview:httpverb=&quot;GET&quot;
         dataview:dataprovider=&quot;SampleService.svc&quot;
         dataview:fetchoperation=&quot;getTeams&quot;&gt;
        &lt;tr&gt;
            &lt;td&gt;{{ Team }}&lt;/td&gt;
            &lt;td&gt;&lt;a href=&quot;{{ Homepage }}&quot;&gt;{{ Homepage }}&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
 &lt;/table&gt;  </pre>
<p>A few of the attributes required for the DataView should look familiar, the id, class, and the sys:attach.&#160; Let’s take a closer look at the rest.</p>
<table border="1" cellspacing="0" cellpadding="2" width="494">
<tbody>
<tr>
<td valign="top" width="166"><span class="attr">dataview:autofetch</span></td>
<td valign="top" width="326">Indicates if the data should be retrieved immediately from a data source when the page loads.</td>
</tr>
<tr>
<td valign="top" width="172"><span class="attr">dataview:httpverb</span></td>
<td valign="top" width="321">A string indicating the the HTTP method.&#160; The default method is POST, but in my case, the REST service is using GET.</td>
</tr>
<tr>
<td valign="top" width="177"><span class="attr">dataview:dataprovider</span></td>
<td valign="top" width="317">The data provider the DataView should use to get the data.&#160; This is a path to the svc file.&#160; In my example, the service is in the root with the sample page.</td>
</tr>
<tr>
<td valign="top" width="181"><span class="attr">dataview:fetchoperation</span></td>
<td valign="top" width="314">The querystring or method name to use to retrieve the data.</td>
</tr>
</tbody>
</table>
<p>The DataView template itself should look familiar.&#160; Again I am using one-way, one-time bindings.&#160; Really the only difference is where the template is defined for the table.&#160; The DataView element for a table should be defined on the &lt;tbody&gt; so that the repeating works correctly, otherwise if you add it to the table and just have a structure without the tbody like &lt;table&gt;&lt;tr&gt;&lt;td&gt;&#8230;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;, you’ll get incorrect output as shown in Figure 3.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure 3: Incorrect output if you put the DataView on the TABLE Element" border="0" alt="Figure 3: Incorrect output if you put the DataView on the TABLE Element" src="http://blogs.visoftinc.com/wp-content/uploads/WindowsLiveWriter/ASP.NETAJAX4.0Preview4ClientTemplates_13796/image_6.png" width="615" height="143" /></p>
<p><strong>Figure 3: Incorrect output if you put the DataView on the TABLE Element</strong></p>
<h2></h2>
<h2>Passing Parameters Using the DataView and REST</h2>
<p>One final note on using REST services with the DataView.&#160; During the course of my testing, I ran across a problem when trying to pass parameters to a REST service.&#160; In the last example, I didn’t pass any parameters to the method being called by the DataView, but if you were going to pass parameters, you would typically use the fetchParameters property for example:</p>
<pre class="brush: xml;">&lt;div class=&quot;list sys-template&quot;
    sys:attach=&quot;dataview&quot;
    dataview:autofetch=&quot;true&quot;
    dataview:httpverb=&quot;GET&quot;
    dataview:dataprovider=&quot;SampleService.svc&quot;
    dataview:fetchoperation=&quot;getGreeting&quot;
    dataview:fetchparameters=&quot;{{ {greeting: 'Hello World'} }}&quot;&gt;
    {{ Text }}
&lt;/div&gt;</pre>
<p>When the DataView goes out and makes the request using the fetchparamerters, it ends up being: /SampleService.svc/getGreeting<strong>?greeting=Hello%20World</strong>.</p>
<p>The proper syntax for the REST service should be /SampleService.svc/getGreeting<strong>/Hello%20World</strong>.<strong>&#160; </strong>In order to get the proper syntax for REST service, you should append the parameters as part of the fetchoperation.</p>
<pre class="brush: xml;">&lt;div id=&quot;greeting&quot; class=&quot;sys-template&quot;
     sys:attach=&quot;dataview&quot;
     dataview:autofetch=&quot;true&quot;
     dataview:httpverb=&quot;GET&quot;
     dataview:dataprovider=&quot;SampleService.svc&quot;
     dataview:fetchoperation=&quot;getGreeting/Hello World&quot;&gt;
    {{ Text }}
&lt;/div&gt;</pre>
<div id="codeSnippetWrapper">
<p>Notice the fetchopertion in that example, appending the parameters to the end gives us the correct format.</p>
</div>
<h2></h2>
<h2>Conclusion</h2>
<p>I hope you enjoyed this introduction to the DataView and ASP.NET 4.0 AJAX.&#160; If you’ve been inspired to find out more about the DataView and the other features found in ASP.NET 4.0 AJAX, be sure to check out the samples and documentation found on <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645" target="_blank">Codeplex</a>. </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:15230aad-812d-4386-89d0-6fb61c163722" class="wlWriterSmartContent">
<p>Looking to try the code in this post out for yourself? <a href="http://192.168.1.162/wp-content/uploads/WindowsLiveWriter/ASP.NETAJAX4.0Preview4ClientTemplates_13796/DataViewSamples_1.zip">Download Them &#8211; 88K</a></p>
</div>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c7a46742-0f2f-4813-a48c-ea6d3614c1a9" class="wlWriterSmartContent">Technorati Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/AJAX" rel="tag">AJAX</a>,<a href="http://technorati.com/tags/WCF" rel="tag">WCF</a>,<a href="http://technorati.com/tags/REST" rel="tag">REST</a></div>
]]></content:encoded>
			<wfw:commentRss>http://blogs.visoftinc.com/2009/04/28/asp-net-4-0-ajax-preview-4-client-templates/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

