<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Coding in the cloud &#187; Javascript / Html5</title>
	<atom:link href="http://joeriks.com/category/javascript-html5/feed/" rel="self" type="application/rss+xml" />
	<link>http://joeriks.com</link>
	<description>Jonas Eriksson (@joeriks) about web programming</description>
	<lastBuildDate>Mon, 13 May 2013 06:40:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='joeriks.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/de130fb9c93626140637e0ebadf04954?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Coding in the cloud &#187; Javascript / Html5</title>
		<link>http://joeriks.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://joeriks.com/osd.xml" title="Coding in the cloud" />
	<atom:link rel='hub' href='http://joeriks.com/?pushpress=hub'/>
		<item>
		<title>Some thoughts in between SPA projects</title>
		<link>http://joeriks.com/2013/05/02/some-thoughts-in-between-spa-projects/</link>
		<comments>http://joeriks.com/2013/05/02/some-thoughts-in-between-spa-projects/#comments</comments>
		<pubDate>Thu, 02 May 2013 07:37:49 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[SPA]]></category>

		<guid isPermaLink="false">http://joeriks.com/?p=3837</guid>
		<description><![CDATA[Yay, I just launched a project. It&#8217;s an internal single page application. On the server side a restful api built with ServiceStack serving json data and files (reports in PDF format and images) on a separate service-address (cross domain). And a client side application primarily for iPads, but built with Html5 for wider platform reach [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=3837&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Yay, I just launched a project. It&#8217;s an internal single page application. On the server side a restful api built with ServiceStack serving json data and files (reports in PDF format and images) on a separate service-address (cross domain). And a client side application primarily for iPads, but built with Html5 for wider platform reach in the future.</strong></p>
<p>I&#8217;m currently trying to collect my thoughts about what I could have done better, and what to change for next project (or for refactoring the same project). One thing is the team. I was the developer for both client and server side + the UI&amp;UX designer (or rather &#8220;Bootstrap-devsigner&#8221;), and it would probably be better to be at least two persons for this kind of a project.</p>
<p>I&#8217;m rather pleased with the server side. ServiceStack makes it pretty easy to build a well structured, easy to maintain api. I&#8217;m using it together with the PetaPoco microorm, and legacy Poco&#8217;s. I would probably have choosen ServiceStack&#8217;s own MicroOrm if I was to redo it now. Since I ditched most of my legacy code anyways.</p>
<p>For the client side I&#8217;m pleased with the application, but the road there was a bit rocky. And the maintenance will be a bit harder than it should. So for the future I decided to find and choose a good client side framework.</p>
<p>Currently it looks like I&#8217;m going to choose AngularJs, which seems to get increasing popularity for each day, and I hear good things about it from pretty much everywhere. Plus I like the Angular team opinions and ideas. The main alternative is Backbone+Marionette. I already use some of backbone, and adding more of it + Marionette in iterations would probably be easier and more seamless than moving to Angular.</p>
<p>The client side language is Typescript, which I&#8217;m very happy with. The tools failed me a few times, nothing to write home about, but as it is still preview some problems was expected.</p>
<p>To be continued&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/3837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/3837/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=3837&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2013/05/02/some-thoughts-in-between-spa-projects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>
	</item>
		<item>
		<title>Trying out Azure Mobile Services to create a rest api and a Facebook authenticated web app</title>
		<link>http://joeriks.com/2013/02/26/trying-out-azure-mobile-services-to-create-a-web-app-with-facebook-auth-notes/</link>
		<comments>http://joeriks.com/2013/02/26/trying-out-azure-mobile-services-to-create-a-web-app-with-facebook-auth-notes/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 15:12:45 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>

		<guid isPermaLink="false">http://joeriks.com/?p=2564</guid>
		<description><![CDATA[I heard about Azure Mobile Services on Yet Another Podcast #86 where Jesse Liberty interviews Josh Twist. There was a few things to the service that made me very interested: Azure Mobile Services Inside the Azure portal Easy to setup a robust Restful API over an accompanying Azure SQL database With permissions per user, extensible [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=2564&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>I heard about Azure Mobile Services on <a href="http://jesseliberty.com/2012/12/19/yet-another-podcast-86azure-mobile-services-with-josh-twist/">Yet Another Podcast #86 where Jesse Liberty interviews Josh Twist</a>. There was a few things to the service that made me very interested:</strong></p>
<p><strong>Azure Mobile Services</strong></p>
<ul>
<li>Inside the Azure portal</li>
<li>Easy to setup a robust Restful API over an accompanying Azure SQL database
<ul>
<li>With permissions per user, extensible with the help of server side js scripts (on CRUD operations)</li>
<li>Using authentication from Facebook, Twitter, Google or Microsoft Live</li>
<li>The JSONs are stored in tables, with properties automapped to columns, and schema automatically created</li>
</ul>
</li>
<li>Scheduled tasks (as js scripts)</li>
<li>Addons like Pusher</li>
<li>Easy to customize mentioned scripts (just js that runs in a nodejs app)</li>
</ul>
<p>So I think Azure Mobile Services is a very promising service for greatly simplifying creation of the server side of a web application. Why should I need to create my own custom C# (or whatever) server code for a restful api over data, same stuff as has been done gazillions of times already on the web? Azure Mobile Services provides a portal where I can setup all I need, and overview, manage and edit it.</p>
<p>I wanted to try it out and put the pieces together to make a proof of concept HTML5 application that uses Facebook authentication and allows a per user CRUD of text items into my mobile service database. Plus it has some push functionality just for fun&#8230;</p>
<blockquote><p>Azure Mobile Services (a.k.a. &#8220;Zumo&#8221;, I&#8217;ll just use that shorter name further on) has a nice site and <a href="http://www.windowsazure.com/en-us/develop/mobile/">many high quality help pages with step by step tutorials</a>. It is in preview mode currently. Zumo is primarily targeted for mobile devices (and Windows 8 store apps) and has several examples and pre made help libraries for those. It even creates sample apps with credentials on the fly for you if you like. I used a few of those to learn some about the communication bits. Hopefully they&#8217;ll make the Rest bits just as easy eventually.</p></blockquote>
<p>I needed the following pieces for my experiment:</p>
<ul>
<li><strong>The Zumo service itself. Free to try out, in preview mode.</strong>
<ul>
<li>A data table in Zumo. Just with a name and with permissions set for all operations first to &#8220;Everyone&#8221; and later changed to &#8220;only Authenticated users&#8221;</li>
<li>Server side scripts on that table
<ul>
<li>insert: add userid to the stored items</li>
<li>read: only query for the items that has userid of the current user</li>
</ul>
</li>
</ul>
</li>
<li><strong>A web site that serves the files, and talks to the Zumo service</strong> (Zumo does not allow cross origin requests). One of the developers in the Azure team, Tom Rudick,  has made a small 43 locs <a href="https://github.com/tmrudick/zumo-proxy">&#8220;zumo-proxy&#8221; in node</a> which is does just that. Just fill in the Zumo identifications and url and get going. (Do not add protocol name to the forward url, i.e. &#8220;myzumo.azure-mobile.net&#8221;)
<ul>
<li>I opened my copy of zumo proxy in WebMatrix, created an empty web site within Azure and used its publish settings</li>
</ul>
</li>
<li><strong>A Facebook app</strong></li>
<li><strong>Some client side code</strong>
<ul>
<li>connect to the Facebook app and get a facebook token
<ul>
<li>using the Facebook js scripts, it asks automatically user to allow the application</li>
</ul>
</li>
<li>connect to the Zumo via the proxy to get a zumo token</li>
<li>add and get the items (with security token in the ajax header)</li>
</ul>
</li>
</ul>
<p>The things that took most time was go get the auth working, and to get the ajax requests right. After I learned to actually read the error messages :-p and add some logging to my node application, And &#8211; important <a href="http://rudickulous.com/post/31450464375/building-html5-apps-with-windows-azure-mobile-services">help from Tom Rudick</a>, I got the whole thing rolling late night.</p>
<p>I started out with allowing data operations for everyone so I could get going without the authentication bits:</p>
<pre class="brush: plain; title: ; notranslate">
// the zumo-proxy resends any request to /zumo/* straight to the zumo service url
// get items
$.ajax(&quot;/zumo/tables/todoitem&quot;, function (data) {
 console.log(data);
});

// post a new todoitem
$.ajax({
 url: &quot;/zumo/tables/todoitem&quot;,
 contentType: &quot;application/json&quot;,
 type: &quot;post&quot;,
 data: JSON.stringify({text:&quot;bar&quot;,complete:false})
});</pre>
<p>Then I added Facebook client side library, and login parts:</p>
<pre class="brush: plain; title: ; notranslate">&lt;script src=&quot;https://connect.facebook.net/en_US/all.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre>
<p>&nbsp;</p>
<pre class="brush: plain; title: ; notranslate">&lt;/span&gt;
&lt;pre&gt;function initFacebook() {
    FB.init({
        appId: '{{my facebook app id}}',
        cookie: true,
        xfbml: true,
        status: true
    });
    loginFacebook();
}
function loginFacebook() {

    FB.getLoginStatus(function (response) {
        if (response.authResponse) {
            facebookHandleAuthResponse(response.authResponse);
        } else {
            FB.login(function (response) {
                if (response.authResponse) {
                    facebookHandleAuthResponse(response.authResponse);
                }
            });
        }
    });

}

function facebookHandleAuthResponse(authResponse) {

    // get auth token from Zumo with the help of the auth'd facebook token
    // set the header x-zumo-auth with $.ajaxSetup (for all coming ajax requests)

    $.ajax({
        url: &quot;/zumo/login/facebook&quot;,
        type: &quot;post&quot;,
        data: JSON.stringify({ &quot;access_token&quot;: authResponse.accessToken }),
        contentType: &quot;application/json&quot;,
        success: function (response) {
            $.ajaxSetup({ headers: { &quot;x-zumo-auth&quot;: response.authenticationToken} });
        }
    });

}
</pre>
<p>When I first ran my application from Facebook, (apps.facebook.com/mycoolapp) I got a 405 error. Turns out Facebook does not handle a static file (index.html) as a start page, it does some test to post to the start page, and a static page does not allow posts. Simple workaround : rename index.html to default.aspx. And make default.aspx the start page in the Azure web site.</p>
<p><strong>The scripts that runs in Zumo on every insert/read operation</strong></p>
<p>For inserts: add userid to the item object:</p>
<pre class="brush: plain; title: ; notranslate">function insert(item, user, request) {

    item.userId = user.userId;
    request.execute();

}</pre>
<p>The first insert will create the necessary columns in the database table.</p>
<p>For reads: only get the items with userId == id of current user:</p>
<pre class="brush: plain; title: ; notranslate">function read(query, user, request) {

    query.where({ userId: user.userId });
    request.execute();

}</pre>
<p><strong>Push notifications</strong></p>
<p>I also added a Pusher service. It can be done as an addon, but it&#8217;s not available currently from my part of the world (only for US?). So I signed up for a accound directly on the Pusher site. Very easy to do. And added the 50 locs &#8220;Pusher&#8221; nodejs server side module directly within a Scheduled script (like a cron-job that executes js), and got it working.</p>
<p><strong>References</strong></p>
<p><a href="http://rudickulous.com/post/31450464375/building-html5-apps-with-windows-azure-mobile-services">Building HTML5 Apps with Windows Azure Mobile Services</a> by Tom Ruddick</p>
<p><a href="http://www.strathweb.com/2012/09/using-azure-mobile-services-in-your-web-apps-through-asp-net-web-api/">Using Azure Mobile Services in your web apps through ASP.NET Web API</a> by Filip Woj</p>
<p><a href="https://github.com/pusher/pusher-node-server">Pusher node server</a></p>
<p><a href="https://github.com/tmrudick/zumo-proxy">Zumo Proxy</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/2564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/2564/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=2564&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2013/02/26/trying-out-azure-mobile-services-to-create-a-web-app-with-facebook-auth-notes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>
	</item>
		<item>
		<title>Fun stuff : node.js in an Umbraco site</title>
		<link>http://joeriks.com/2013/02/18/fun-stuff-node-js-in-an-umbraco-site/</link>
		<comments>http://joeriks.com/2013/02/18/fun-stuff-node-js-in-an-umbraco-site/#comments</comments>
		<pubDate>Mon, 18 Feb 2013 11:52:17 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[Umbraco]]></category>

		<guid isPermaLink="false">http://joeriks.com/?p=2534</guid>
		<description><![CDATA[Did you ever want to run node.js within your umbraco site, and edit the server side javascript from within your Umbraco backend? Not? Well, it&#8217;s still a fun thing to do, don&#8217;t you think? Update 1: I actually think nodejs makes some sense within Umbraco. Why? Well, for example it makes it possible for js-peeps [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=2534&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Did you ever want to run node.js within your umbraco site, and edit the server side javascript from within your Umbraco backend? Not? Well, it&#8217;s still a fun thing to do, don&#8217;t you think?</strong></p>
<blockquote><p>Update 1: I actually think nodejs makes some sense within Umbraco. Why? Well, for example it makes it possible for js-peeps to stay js. And node has a _lot_ of nice plugins (as for example jade and express)</strong>.</p></blockquote>
<p>The setup only takes a few minutes to do:</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='588' height='361' src='http://www.youtube.com/embed/JEFmz4r5ZLc?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<ol>
<li>Install NodeJs and IISNode on your computer / server</li>
<li>Install an Umbraco site or use an existing, using WebMatrix</li>
<li>Add the NPM plugin from WebMatrix Gallery</li>
<li>Add the folders iisnode and node_modules for the NPM plugin to show up in the toolbar (and restart WebMatrix)</li>
<li>Add the Express Node module with the help of NPM. It puts the module in a new folder in your root &#8220;node_modules&#8221;</li>
<li>Add the following lines to your web.config, in the configuration section (at the bottom for example):</li>
</ol>
<pre class="brush: plain; title: ; notranslate">
...
    &lt;location path=&quot;scripts/node&quot;&gt;
        &lt;system.webServer&gt;
            &lt;handlers&gt;
                &lt;add name=&quot;iisnode&quot; path=&quot;server.js&quot; verb=&quot;*&quot; modules=&quot;iisnode&quot; /&gt;
            &lt;/handlers&gt;

            &lt;rewrite&gt;
                &lt;rules&gt;
                    &lt;rule name=&quot;express&quot;&gt;
                        &lt;match url=&quot;/*&quot; /&gt;
                        &lt;action type=&quot;Rewrite&quot; url=&quot;/scripts/node/server.js&quot; /&gt;
                    &lt;/rule&gt;
                &lt;/rules&gt;
            &lt;/rewrite&gt;

        &lt;/system.webServer&gt;

    &lt;/location&gt;
&lt;/configuration
</pre>
<li>Add a new .js file and save it as /scripts/node/server.js</li>
<pre class="brush: plain; title: ; notranslate">
var express = require('express');

var app = express.createServer();

app.get('/scripts/node/hello/:name', function (req, res) {
    
	
	res.send('Hello ' + req.params.name + ' from node! [express sample]');
	
	
});

app.listen(process.env.PORT);
</pre>
</ul>
<p>That&#8217;s it! Now you should be able to surf to /scripts/node/hello/world and you should get &#8220;Hello world from node!&#8221; back.</p>
<h4>Update 2: What about Umbraco content?</h4>
<p><strong>What if we like to do something a little bit more useful &#8211; make it possible to get and edit content? Well, our node application does not have the Umbraco contents available, so we need to get it in some other way : I guess http is the standard.</strong></p>
<p>Currently (in Umbraco 6.0) there is no available Restful Api out of the box (it will be added in 6.1 afaiu). However, the <a href="http://our.umbraco.org/projects/backoffice-extensions/urest">uRest package by Matt Brailsford</a> seems to play nicely with u6 (edit : <a href="http://our.umbraco.org/projects/backoffice-extensions/urest/bugs,-feedback-suggestions/38265-uRest-and-Umbraco-6?p=0#comment139477">perhaps not, I could not get it to accept posts</a>). Add that, and then change server.js and add a urest.js file as in <a href="https://gist.github.com/joeriks/4985143">this gist</a> to get contents with internal http requests.</p>
<pre class="brush: plain; title: ; notranslate">app.get('/scripts/node/urest/documents/:id',function(req,res){
    var documentId = req.params.id;
    requestGetWithToken(&quot;/documents/&quot; + documentId, function (response) {
        // just return the content to the response string:
        res.send(response);
    });
});</pre>
<p>Now you can browse to the url /scripts/node/documents/{nnnn} to get a particular document.</p>
<p>Check out the <a href="http://urest4umb.codeplex.com/wikipage?title=Documents&amp;referringTitle=Documentation">uRest documentation</a> for information about how to create and edit content.</p>
<h4>Update 3: content straight from umbraco context with the help of EdgeJs</h4>
<p>I just managed to get actual content directly from umbraco context into nodejs with the help of the brilliant EdgeJs <a href="https://github.com/tjanczuk/edge" rel="nofollow">https://github.com/tjanczuk/edge</a> by Tomasz Janczuk and <a href="https://github.com/sitereactor/umbraco-console-example" rel="nofollow">https://github.com/sitereactor/umbraco-console-example</a> by Morten Christensen. </p>
<p>I will blog more details about it, but what I needed to do was this:<br />
1. Add Edge with the help of NPM.<br />
2. Create a class libary dll that fires up the Umbraco context (copied from Umbraco console example), with functions returning async Task </p>
<pre class="brush: plain; title: ; notranslate">public async Task&lt;object&gt; ListNodes(object input)
{
  return &quot;nodes: &quot; + listNodes();
}
</pre>
<p>3. Setup functions inside my node app calling the .net functions</p>
<pre class="brush: plain; title: ; notranslate">var express = require('express');
var edge = require('edge');
var app = express.createServer();
var listNodes = edge.func({
    assemblyFile: 'C:/harcodedpath-to-my-website/bin/umbracocontext.dll',
    typeName: 'UmbracoContext.Startup',
    methodName: 'ListNodes'
}); 
 
app.get('/scripts/node/hello', function (req, res) {         
    listNodes(&quot;&quot;,function(error,data){
        res.send(data);        
    })
});
app.listen(process.env.PORT);</pre>
<p>4. Add a node.exe.config into my node.exe folder with just a connectionstring to begin with:</p>
<pre class="brush: plain; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; ?&gt;
&lt;configuration&gt;
    &lt;connectionStrings&gt;
        &lt;remove name=&quot;umbracoDbDSN&quot; /&gt;
        &lt;add name=&quot;umbracoDbDSN&quot;
             connectionString=&quot;Datasource=C:\harcodedpath-to-my-website\App_Data\Umbraco.sdf&quot;
             providerName=&quot;System.Data.SqlServerCe.4.0&quot; /&gt;
    &lt;/connectionStrings&gt;
&lt;/configuration&gt;
</pre>
<p>It&#8217;s obviously not ideal to set the connectionstring in the global node.exe folder. I think a good workaround to get local connectionstrings is to copy the node.exe to a folder within the site and set the iisnode handler to use the local node.exe instead. </p>
<pre class="brush: plain; title: ; notranslate">&lt;iisnode
      nodeProcessCommandLine=&quot;&amp;quot;c:\harcodedpath-to-my-website\nodejs\node.exe&amp;quot;&quot; 
</pre>
<p>Edge does not make it possible (<a href="https://github.com/tjanczuk/edge/issues/17">yet</a>) to use the already existing instance of the asp.net application (share memory), so if we run umbraco in asp.net and node in parallell we will have two context instances.</p>
<p>If you are concerned about the performance: <a href="http://tjanczuk.github.io/edge/#/22">read this</a>.</p>
<p><strong>GetTreeById</strong></p>
<pre class="brush: plain; title: ; notranslate">app.get('/scripts/node/getTreeById/:nodeId', function (req, res) {  
    getTreeById(Number(req.params.nodeId),function(error,data){
        res.send(data);        
    })
});</pre>
<pre class="brush: plain; title: ; notranslate">public async Task&lt;object&gt; GetTreeById(int nodeId)
{
    var node = contentService.GetById(nodeId);
    return nodeToDictionary(node, true);

}
private Dictionary&lt;string, object&gt; nodeToDictionary(IContent content, bool includeChildren)
{
    var nodeData = new Dictionary&lt;string, object&gt;();
    nodeData[&quot;Name&quot;] = content.Name;
    nodeData[&quot;Id&quot;] = content.Id;
            
    content.Properties.ForEach(p =&gt; { nodeData[p.Alias] = p.Value.ToString(); });
    if (includeChildren)
        nodeData[&quot;Children&quot;] = content.Children().Select(child =&gt; nodeToDictionary(child, true)).ToList();
    return nodeData;
}
</pre>
<p><a href="http://joeriks.files.wordpress.com/2013/02/tree.png"><img src="http://joeriks.files.wordpress.com/2013/02/tree.png?w=588&#038;h=400" alt="tree" width="588" height="400" class="alignleft size-full wp-image-3898" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/2534/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/2534/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=2534&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2013/02/18/fun-stuff-node-js-in-an-umbraco-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2013/02/tree.png" medium="image">
			<media:title type="html">tree</media:title>
		</media:content>
	</item>
		<item>
		<title>A first look at how to use AmplifyJs to simplify coding of server requests</title>
		<link>http://joeriks.com/2012/11/23/a-first-look-at-how-to-use-amplifyjs-to-simplify-coding-of-server-requests/</link>
		<comments>http://joeriks.com/2012/11/23/a-first-look-at-how-to-use-amplifyjs-to-simplify-coding-of-server-requests/#comments</comments>
		<pubDate>Fri, 23 Nov 2012 12:02:09 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[SPA]]></category>
		<category><![CDATA[TypeScript]]></category>

		<guid isPermaLink="false">http://joeriks.com/?p=2002</guid>
		<description><![CDATA[When coding web UI&#8217;s it&#8217;s not always we have the server API finished and ready to use. And even if we have it it&#8217;s nice to be able to mimic server behaviour so we can focus and test our UI code isolated. Therefore it&#8217;s nice to abstract server calling functions from actual Ajax code with [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=2002&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>When coding web UI&#8217;s it&#8217;s not always we have the server API finished and ready to use. And even if we have it it&#8217;s nice to be able to mimic server behaviour so we can focus and test our UI code isolated. Therefore it&#8217;s nice to abstract server calling functions from actual Ajax code with URLs.</p>
<p>This also makes it easier to adjust our code in case of future possible API changes. AmplifyJs helps us with this in a really nice way.</strong></p>
<p><i>Disclaimer: As most things I write about, I&#8217;m far from an expert, I write as I learn new stuff. But I only write about stuff I found to be working and useful, and I hope I can help others with similar needs as mine.</i></p>
<p><strong>Want to read an expert article about amplify.request? Check out Elijah Manors <a href="http://www.elijahmanor.com/2012/10/mocking-jquery-ajax-with-amplifyjs.html">Mocking jQuery AJAX with AmplifyJS Request</a>.</strong></p>
<p>Let&#8217;s say we need to save some data to the server from our UI. We know we need to write a Web Api. But we like to focus on the UI at first. We start by writing the Html and a stub to handle the functionality:</p>
<pre class="brush: plain; title: ; notranslate">Key: &lt;input type=&quot;text&quot; id=&quot;key&quot;/&gt;&lt;br /&gt;       
Value: &lt;input type=&quot;text&quot; id=&quot;value&quot; /&gt;&lt;br /&gt;

&lt;input type=&quot;button&quot; id=&quot;get&quot; value=&quot;get&quot;/&gt;
&lt;input type=&quot;button&quot; id=&quot;post&quot; value=&quot;post&quot;/&gt;</pre>
<pre class="brush: plain; title: ; notranslate">$(() =&gt;{
    $(&quot;#get&quot;).click(() =&gt;{
        var key = $(&quot;#key&quot;).val();
        // get data
    });
    $(&quot;#post&quot;).click(() =&gt;{
        var key = $(&quot;#key&quot;).val();
        var value = $(&quot;#value&quot;).val();
        // post data
    });
});</pre>
<p>Now, we could easily add jQuery ajax GET/POST functionality and get things done. But that would require us to write the server side code to be able to test the UI. If we instead add AmplifyJs we can define our request methods at application startup to stay on client side until we&#8217;re satisfied with that, and then easily switch to code that calls the server:</p>
<p><strong>The code using amplify.request:</strong></p>
<pre class="brush: plain; title: ; notranslate">$(() =&gt;{
    $(&quot;#get&quot;).click(() =&gt;{
        var key = $(&quot;#key&quot;).val();

        amplify.request(&quot;getSomeData&quot;,
            {
                type: &quot;someresource&quot;,
                key: key
            },
            function (data) {
                alert(&quot;Requested for key '&quot; + key + &quot;' and I got: &quot; + data.value);
            }
        );

    });
    $(&quot;#post&quot;).click(() =&gt;{
        var key = $(&quot;#key&quot;).val();
        var value = $(&quot;#value&quot;).val();

        amplify.request(&quot;postSomeData&quot;, {
            type: &quot;someresource&quot;,
            key: key,
            value: value
        }, function (data) {
            alert(&quot;Result &quot; + data);
        });

    });
});
</pre>
<p><strong>1. Configure amplify.request to use client side functions</strong><br />
To add amplify client side functionality we just need to add some code that can mimic get/post behavious. For example use client side storage, which conveniantely enough, is another thing amplify can help us with:</p>
<pre class="brush: plain; title: ; notranslate">amplify.request.define(&quot;postSomeData&quot;, (hash) =&gt; {
    if (hash.data.type == &quot;someresource&quot;) {
        amplify.store(hash.data.key, hash.data.value); /* store the key and the value */
        hash.success(true);
    };
});

amplify.request.define(&quot;getSomeData&quot;, (hash) =&gt; {
    if (hash.data.type == &quot;someresource&quot;) {
        hash.success({
            key: hash.data.key,
            value: amplify.store(hash.data.key) /* get the stored data with the key */
        });
    };
});</pre>
<p><strong>2. Later on &#8211; switch to server calls</strong><br />
When we are pleased with the UI then we switch out the client side code with actual server side code. Amplify handles ajax with the help of jQuery and the code looks like this:</p>
<pre class="brush: plain; title: ; notranslate">amplify.request.define(&quot;getSomeData&quot;, &quot;ajax&quot;, {
    url: &quot;/api/{type}/{key}&quot;,
    type: &quot;GET&quot;
});
amplify.request.define(&quot;postSomeData&quot;, &quot;ajax&quot;, {
    url: &quot;/api/{type}&quot;,
    type: &quot;POST&quot;
});
</pre>
<p><i>Naturally we could use an if condition to use server functionality only on a live server, or we could have ConfigureAmplifyToUseServer.ts vs ConfigureAmplifyToUseLocalStorage.ts configuration files.</i></p>
<p><strong>Lazy Typescripting</strong><br />
I wrote this with a lazy Typescript touch. Not many strong types. But I do use the &#8220;()=&gt;&#8221; symbol frequently (it simply translates to javascript &#8220;function()&#8221;.) And to get some (not complete) syntax help for amplify I wrote a quick definition file <a href="https://gist.github.com/3887503">which you can find here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/2002/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/2002/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=2002&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2012/11/23/a-first-look-at-how-to-use-amplifyjs-to-simplify-coding-of-server-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple client side routes and content loading in Umbraco</title>
		<link>http://joeriks.com/2012/09/21/simple-client-side-routes-and-content-loading-in-umbraco/</link>
		<comments>http://joeriks.com/2012/09/21/simple-client-side-routes-and-content-loading-in-umbraco/#comments</comments>
		<pubDate>Fri, 21 Sep 2012 12:46:00 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[Umbraco]]></category>

		<guid isPermaLink="false">http://joeriks.com/?p=1784</guid>
		<description><![CDATA[Do you like your users to be able to load subcontent on a page dynamically and support client side url&#8217;s (which in turn enables browser history, url-copy&#38;pasting and bookmarks)? In this post I will show how you can do it in a very flexible way with the help of Backbone.js and a minimum of code. [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=1784&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Do you like your users to be able to load subcontent on a page dynamically and support client side url&#8217;s (which in turn enables browser history, url-copy&amp;pasting and bookmarks)? In this post I will show how you can do it in a very flexible way with the help of Backbone.js and a minimum of code.</strong></p>
<p>This is what I&#8217;m trying to achieve, I have an ordinary page with links to &#8220;sub content&#8221;, which is supposed to appear on the same page when the user clicks on a link:</p>
<p><a href="http://joeriks.files.wordpress.com/2012/09/some-post.png"><img class="size-medium wp-image-1785" title="Some page in the site" src="http://joeriks.files.wordpress.com/2012/09/some-post.png?w=300&#038;h=230" alt="" width="300" height="230" /></a></p>
<p>When the user clicks &#8220;sub content 2&#8243; the url becomes <strong>some-page#subcontent-2</strong> and the corresponding content shows:</p>
<p><a href="http://joeriks.files.wordpress.com/2012/09/subcontent2.png"><img class="size-medium wp-image-1786" title="Show subcontent 2" src="http://joeriks.files.wordpress.com/2012/09/subcontent2.png?w=300&#038;h=234" alt="" width="300" height="234" /></a></p>
<p><strong>To use &#8220;client side routes&#8221; we need to add some library that handles such. The most commonly used one is probably Backbone.js.</strong> </p>
<p>To use it you will need to add script reference to <strong>Backbone.js</strong> and to <strong>Underscore.js</strong> which it has as a dependency. </p>
<p>Download the files or add reference to CDN-paths, for example:</p>
<pre class="brush: plain; title: ; notranslate">http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js

http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js</pre>
<p><strong>Defining our routes and load the asked for content.</strong></p>
<p>A client side route url part is everything in the url that comes after a hash, i.e. &#8220;/some-post#myview/whatever&#8221; =&gt; &#8220;myview/whatever&#8221;.</p>
<p>Backbone is using a straight forward syntax to setup routes, where every route has a url matching route string and a function to call. It handles parameter url parts aswell. The syntax to use a parameter is adding a colon before its name i.e. &#8220;myview/:id&#8221;</p>
<p>In our case we&#8217;ll use &#8220;:urlName&#8221; to match every url (&#8220;#subcontent-1&#8243; and &#8220;#subcontent-2&#8243;) and pass the url string as the parameter urlName to the route function subContentsRoute.</p>
<p><strong>The subContentsRoute route function</strong> does only one thing : it loads the html content and puts it in the #subcontents div. It&#8217;s simply using a fixed url part + the urlName parameter value (i.e. &#8220;/some-post/subcontents-2&#8243;).</p>
<pre class="brush: plain; title: ; notranslate">var SubContentRouter = Backbone.Router.extend({
        routes: {
		&quot;:urlName&quot; : &quot;subContentsRoute&quot;,
           	&quot;*actions&quot;: &quot;defaultRoute&quot; // matches http://example.com/#anything-here
        	},
        subContentsRoute: function(urlName) {
		$.get(&quot;/some-post/&quot; + urlName, function(data){
			$(&quot;#subcontents&quot;).html(data);
		});
	},
        defaultRoute: function( actions ){
            this.subContentsRoute('subcontent-1');
        }
    });

$(function(){
    var app_router = new SubContentRouter;
    // Start Backbone history a neccesary step for bookmarkable URL's
	Backbone.history.start();
});</pre>
<p>Save it as subcontentBackboneApp.js and add a script reference into the html template :</p>
<pre class="brush: plain; title: ; notranslate">&lt;script type=&quot;text/javascript&quot; src=&quot;/scripts/subcontentBackboneApp.js&quot;&gt;&lt;/script&gt;</pre>
<p><strong>The rest is common Umbraco usage &#8211; create documenttype, template, content and a small macro</strong></p>
<p>How to make /some-post/subcontent-2 return the right content html? Let&#8217;s setup Umbraco with a simple document type with only <strong>bodyText</strong> as a richtext property, and a template that only returns the html we like to add into our div tag.</p>
<p><a href="http://joeriks.files.wordpress.com/2012/09/contentviewdoctype.png"><img src="http://joeriks.files.wordpress.com/2012/09/contentviewdoctype.png?w=300&#038;h=189" alt="" title="Document type" width="300" height="189" class="size-medium wp-image-1813" /></a></p>
<p><a href="http://joeriks.files.wordpress.com/2012/09/umbraco-template.png"><img src="http://joeriks.files.wordpress.com/2012/09/umbraco-template.png?w=300&#038;h=114" alt="" title="Subcontent template" width="300" height="114" class="size-medium wp-image-1796" /></a></p>
<p>With a document and a template in place we add our documents:</p>
<p><a href="http://joeriks.files.wordpress.com/2012/09/umbraco-tree.png"><img src="http://joeriks.files.wordpress.com/2012/09/umbraco-tree.png?w=300&#038;h=141" alt="" title="Umbraco content tree" width="300" height="141" class="size-medium wp-image-1795" /></a></p>
<p>Lastly we need to create those client side links and add a container for our dynamic content. We do that by adding this macro code in the original textpage template:</p>
<pre class="brush: plain; title: ; notranslate">      &lt;umbraco:Macro runat=&quot;server&quot; language=&quot;cshtml&quot;&gt;
      @foreach(var child in Model.Children)
	     {
	        &lt;a href=&quot;#@child.UrlName&quot;&gt;@child.Name&lt;/a&gt; 
	     }
	  &lt;div id=&quot;subcontents&quot;&gt;&lt;/div&gt;</pre>
<p><strong>Can I haz images and macros?</strong><br />
Our subcontent can be anything, styles, tags, images. But if you use a macro, the macro context will be the subcontent node and not the containing node.</p>
<p><strong>Flexibility</strong><br />
I find this simple approach with a router to be suitable for several similar usages to what I&#8217;m showing in this example.<br />
* Loading &#8220;more content&#8221;,<br />
* Loading &#8220;next sub article&#8221;,<br />
* Being the base for a small single page application within an Umbraco site</p>
<p><strong>Sample with Backbone model and view</strong></p>
<pre class="brush: plain; title: ; notranslate">$(function(){

	var subContentModel = new Backbone.Model({
		htmlContent:&quot;&quot;
	});
	
	var SubContentView = Backbone.View.extend({
		initialize : function () {
			this.model.on(&quot;change&quot;,function () {
				this.render();
			}, this);
		},
		render : function() {
			this.$el.html(this.model.get(&quot;htmlContent&quot;));
			return this;
		}});

	var subContentView = new SubContentView({
		model: subContentModel, 
		el:'#subcontents',
		});
	
	var SubContentRouter = Backbone.Router.extend({
        routes: {
			&quot;:urlName&quot; : &quot;subcontents&quot;,
            &quot;*actions&quot;: &quot;defaultRoute&quot; 
        },
		subcontents: function(urlName) {
			$.get(&quot;/some-post/&quot; + urlName, function(data){
				subContentModel.set({htmlContent:data});
			});
		},
        defaultRoute: function( actions ){
            this.subcontents('subcontent-1');
        }
    });
	
	
	var subContentRouter = new SubContentRouter;
	Backbone.history.start();

});</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/1784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/1784/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=1784&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2012/09/21/simple-client-side-routes-and-content-loading-in-umbraco/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2012/09/some-post.png?w=300" medium="image">
			<media:title type="html">Some page in the site</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2012/09/subcontent2.png?w=300" medium="image">
			<media:title type="html">Show subcontent 2</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2012/09/contentviewdoctype.png?w=300" medium="image">
			<media:title type="html">Document type</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2012/09/umbraco-template.png?w=300" medium="image">
			<media:title type="html">Subcontent template</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2012/09/umbraco-tree.png?w=300" medium="image">
			<media:title type="html">Umbraco content tree</media:title>
		</media:content>
	</item>
		<item>
		<title>A first look at SlickGrid with read, insert and update (in WebMatrix)</title>
		<link>http://joeriks.com/2011/07/03/a-first-look-at-slickgrid-with-read-and-update-in-webmatrix/</link>
		<comments>http://joeriks.com/2011/07/03/a-first-look-at-slickgrid-with-read-and-update-in-webmatrix/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 07:54:26 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebMatrix]]></category>

		<guid isPermaLink="false">http://joeriks.wordpress.com/?p=1111</guid>
		<description><![CDATA[A few days ago I had a quick look at Flexigrid and I planned to continue with it to enable updates. However when I searched a bit further I got recommendations to use SlickGrid instead and so I did. As with my Flexigrid post this post is based on what I learned in a short [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=1111&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>A few days ago I had a quick look at Flexigrid and I planned to continue with it to enable updates. However when I searched a bit further I got recommendations to use <a href="https://github.com/mleibman/SlickGrid">SlickGrid</a> instead and so I did. As with my Flexigrid post this post is based on what I learned in a short time &#8211; if you are looking for a in depth / best practices sample you should indeed look at the library authors samples instead.</strong></p>
<p>My first impression with SlickGrid is that it is a very good looking grid and it was easy to get a read and update solution up and running with very little code.</p>
<p><i>Another strength of SlickGrid is that it is possible to use a DataView model to handle the data. However that&#8217;s not something I have experimented with yet, and this sample is using a straight serverdata &#8211; grid path.</i></p>
<p><a href="http://joeriks.files.wordpress.com/2011/07/slickgrid1.png"><img src="http://joeriks.files.wordpress.com/2011/07/slickgrid1.png?w=588" alt="" title="slickgrid"   class="alignnone size-full wp-image-1544" /></a></p>
<p><strong>Edit (2011-12-12) to make it use the latest SlickGrid I updated my code pretty much + now also allows insert (so full CRUD, yay&#8230;)</strong></p>
<p><strong>Here&#8217;s how I did it:</strong></p>
<p>The data comes from a simple Sql CE 4 db-table with the columns id (pk+identity), name (nvarchar 100), description (nvarchar 100) and somevalue (int). To create the SlickGrid I initialize it with a <strong>columns</strong> array, an <strong>options</strong> object, a <strong>data.cshtml</strong> that returns a Json object from a database read and takes post data for database insert or update.</p>
<p><a href="http://joeriks.files.wordpress.com/2011/07/slickgrid2.png"><img src="http://joeriks.files.wordpress.com/2011/07/slickgrid2.png?w=588" alt="" title="slickgrid"   class="alignnone size-full wp-image-1546" /></a></p>
<p>Here&#8217;s how the html &amp; javascript code looks like for my page (default.cshtml):</p>
<pre class="brush: plain; title: ; notranslate">@{
    Layout = &quot;/_layout.cshtml&quot;;
}        
        &lt;script type=&quot;text/javascript&quot;&gt;
    $(document).ready(function () {
        var grid;
        var columns = [
		
            { id: &quot;name&quot;, name: &quot;Name&quot;, field: &quot;name&quot;, editor:TextCellEditor, resizable:false, width:100, rerenderOnResize:true},
            { id: &quot;description&quot;, name: &quot;Description&quot;, field: &quot;description&quot;, editor:TextCellEditor, resizable:false, width:100, rerenderOnResize:true},
            { id: &quot;somevalue&quot;, name: &quot;Some value&quot;, field: &quot;somevalue&quot;, editor:IntegerCellEditor, resizable:false, width:100, rerenderOnResize:true},
            { id:&quot;id&quot;, name:&quot;Delete&quot;, field:&quot;id&quot;,
              formatter: function (r,c,id,def,datactx) {
                  return &quot;&lt;a href='#' onclick='removeClick(&quot; + id + &quot;,&quot; + r + &quot;)'&gt;x&lt;/a&gt;&quot;;
            }}
	    ];

        function removeClick(databaseId, gridRow) {

            $.post(&quot;data/delete/&quot;,{id:databaseId});
            var data = grid.getData(); 
            data.splice(gridRow, 1);
            grid.setData(data);
            grid.render();

        }


        var options = {
            editable: true,
			enableAddRow: true,
			enableCellNavigation: true,
			asyncEditorLoading: false,
            enableColumnReorder: false,                        
            autoHeight: true
        };


        // Handle updates

        $.getJSON(&quot;/mygrid/data&quot;, success = function (data) {
            grid = new Slick.Grid(&quot;#myGrid&quot;, data, columns, options);            
            grid.onCellChange.subscribe(function (e, args) {
                if (typeof(args.item.id)=='undefined')
                    $.post(&quot;/mygrid/data/insert&quot;, args.item);
                else
                    $.post(&quot;/mygrid/data/update&quot;, args.item);
            });
            
            // Handle new row
            
            grid.onAddNewRow.subscribe(function(e, args) {
                var item = args.item;
                var column = args.column;
                grid.invalidateRow(data.length);
                data.push(item);
                grid.updateRowCount();
                grid.render();
            });
            
            $(&quot;#myGrid&quot;).show();
        });
        
    });
&lt;/script&gt;

&lt;div id=&quot;myGrid&quot; style=&quot;width:400px;display:none;&quot;&gt;&lt;/div&gt;</pre>
<p>The page is using jQuery and SlickGrid with editors plugin (included in the library samples). The css is both from jQuery UI and from SlickGrid.</p>
<p><strong>Here are all the js- and css- files that&#8217;s included in the header section of the page (_layout.cshtml):</strong></p>
<pre class="brush: plain; title: ; notranslate">&lt;!DOCTYPE html&gt;

&lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;utf-8&quot; /&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;/js/slickgrid/slick.grid.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; charset=&quot;utf-8&quot; /&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;/css/smoothness/jquery-ui-1.8.16.custom.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; charset=&quot;utf-8&quot; /&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;/css/examples.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; charset=&quot;utf-8&quot; /&gt;
        
		&lt;script src=&quot;/js/jquery-1.7.min.js&quot;&gt;&lt;/script&gt;
		&lt;script src=&quot;/js/jquery.event.drag-2.0.min.js&quot;&gt;&lt;/script&gt;

        &lt;script src=&quot;/js/slickgrid/slick.core.js&quot;&gt;&lt;/script&gt;
        &lt;script src=&quot;/js/slickgrid/slick.editors.js&quot;&gt;&lt;/script&gt;
        &lt;script src=&quot;/js/slickgrid/slick.grid.js&quot;&gt;&lt;/script&gt;

        
    &lt;/head&gt;
    &lt;body&gt;
        @RenderBody()
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p><strong>The data.cshtml:</strong></p>
<pre class="brush: plain; title: ; notranslate">@{
    
    // Get Action from Url f ex /mygrid/data/insert /mygrid/data/update
    var action = UrlData[0];
    var db = Database.Open(&quot;data&quot;);
    
    if (IsPost)
    {        
        
        // Get Post data (convert null's to string or integer)
        
        var id = Request[&quot;id&quot;];
        var name = Request[&quot;name&quot;]??&quot;&quot;;
        var description = Request[&quot;description&quot;]??&quot;&quot;;
        var somevalue = Request[&quot;somevalue&quot;].AsInt();
        
        if (action==&quot;insert&quot;)
        {                        
            var sql = @&quot;INSERT INTO item (name, description, somevalue) VALUES (@0,@1,@2)&quot;;
            db.Execute(sql,name,description,somevalue);
            var newId = db.GetLastInsertId();
            Response.Write(Json.Encode(newId));
        }
        else if (action==&quot;update&quot;)
        {
            var sql = @&quot;UPDATE item set name=@1, description=@2, somevalue=@3 WHERE id=@0&quot;;
            var data = db.Execute(sql,id,name,description,somevalue);
        }                
        else if (action==&quot;delete&quot;)
        {
            var sql = @&quot;DELETE FROM item WHERE id=@0&quot;;
            var data = db.Execute(sql,id);
        }                
    }
    else
    {        
        var sql = &quot;SELECT id, name, description, somevalue FROM item&quot;;
        var data = db.Query(sql).ToList();        
        Response.Write(Json.Encode(data));
    }       

}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/1111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=1111&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2011/07/03/a-first-look-at-slickgrid-with-read-and-update-in-webmatrix/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/07/slickgrid1.png" medium="image">
			<media:title type="html">slickgrid</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/07/slickgrid2.png" medium="image">
			<media:title type="html">slickgrid</media:title>
		</media:content>
	</item>
		<item>
		<title>A first look at Flexigrid (using it with WebMatrix and Json)</title>
		<link>http://joeriks.com/2011/07/01/a-first-look-at-flexigrid-using-it-with-webmatrix-and-json/</link>
		<comments>http://joeriks.com/2011/07/01/a-first-look-at-flexigrid-using-it-with-webmatrix-and-json/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 14:51:21 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[WebMatrix]]></category>

		<guid isPermaLink="false">http://joeriks.wordpress.com/?p=1087</guid>
		<description><![CDATA[We all need a nice grid to display data from time to time. I found out at Stackoverflow that Flexigrid has some popularity and the images I saw of Flexigrid in action impressed me. This is a brief introduction post describing a way to use it for basic display of data in a WebMatrix (WebPages) [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=1087&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>We all need a nice grid to display data from time to time. I found out at Stackoverflow that <a href="http://flexigrid.info/">Flexigrid</a> has some popularity and the images I saw of Flexigrid in action impressed me.</strong></p>
<p>This is a brief introduction post describing a way to use it for basic display of data in a WebMatrix (WebPages) web site. (I soon need the update functionality, and I hope I&#8217;ll be able to try that out and blog about in a few days.)</p>
<p>Let&#8217;s say we have a datatable with the columns <strong>id</strong>, <strong>name </strong>and <strong>address</strong>. We can retrieve the data with the standard WebMatrix.Data library and display it with the WebGrid helper this way:</p>
<pre class="brush: plain; title: ; notranslate">@{
  var db = Database.Open(&quot;myConnection&quot;);
  var sql = @&quot;SELECT * FROM items&quot;;
  var data = db.Query(sql)
  var grid = new WebGrid(data);
}
@grid.GetHtml()</pre>
<p>It works, but is not exactly pretty:</p>
<p><a href="http://joeriks.files.wordpress.com/2011/07/webgrid.png"><img src="http://joeriks.files.wordpress.com/2011/07/webgrid.png?w=588" alt="" title="webgrid"   class="alignnone size-full wp-image-1088" /></a></p>
<p><strong>If we use Flexigrid instead we get a much nicer result:</strong><br />
<a href="http://joeriks.files.wordpress.com/2011/07/flexigrid.png"><img src="http://joeriks.files.wordpress.com/2011/07/flexigrid.png?w=588" alt="" title="flexigrid"   class="alignnone size-full wp-image-1089" /></a></p>
<p>To accomplish this we need the flexigrid js and css files from the <a href="http://flexigrid.info/">Flexigrid site</a>. </p>
<p>I downloaded the zip, and extracted the two folders <strong>js</strong> and <strong>css</strong> to the root of my web site. Then I added reference to them and to jQuery in my demo page:</p>
<pre class="brush: plain; title: ; notranslate">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;Flexigrid demo&lt;/title&gt;
    &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;js/flexigrid.pack.js&quot;&gt;&lt;/script&gt;
    &lt;link href=&quot;css/flexigrid.pack.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;/head&gt;
</pre>
<p>The Flexigrid needs an empty <strong>table</strong> tag and is initialized with a javascript function &#8211; together with information about the data columns, behaviours and looks of the grid.</p>
<p>I used a Flexigrid-sample and only changed it slightly to handle my columns (id, name, address) + the url for fetching the data.</p>
<p>The code is pretty self explanatory:</p>
<pre class="brush: plain; title: ; notranslate">&lt;table id=&quot;flex1&quot; style=&quot;display: none&quot;&gt;
&lt;/table&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    $(&quot;#flex1&quot;).flexigrid({
        url: '/data.cshtml',
        dataType: 'json',
        colModel: [
            { display: 'id', name: 'id', width: 40, sortable: true, align: 'center' },
            { display: 'name', name: 'name', width: 180, sortable: true, align: 'left' },
            { display: 'address', name: 'address', width: 120, sortable: true, align: 'left' }
            ],
        searchitems: [
            { display: 'name', name: 'name' },
            { display: 'address', name: 'address', isdefault: true }
            ],
        sortname: &quot;name&quot;,
        sortorder: &quot;asc&quot;,
        usepager: true,
        title: 'Names',
        useRp: true,
        rp: 15,
        showTableToggleBtn: true,
        width: 700,            
        height: 200
    });</pre>
<p><strong>Fetching the data from</strong><br />
The data is being fetched with the help of an Ajax call, the url is specified in a parameter as in the code above. The data format is prefferably Json.</p>
<p>The Json needs to follow this format:</p>
<pre class="brush: plain; title: ; notranslate">{total:1,page:1,rows:[{id:1,cell:{id:1,name:&quot;c#&quot;,address:&quot;test&quot;},{id:2,cell:{id:2,name:&quot;vb.net&quot;,address:&quot;test2&quot;}]}</pre>
<p>To construct the actual Json I added a data.cshtml file, where I&#8217;m using the WebMatrix Json helper over an anonymous object like this (the code here also includes paging functionality):</p>
<pre class="brush: plain; title: ; notranslate">@{
    var db = Database.Open(&quot;myConnection&quot;);
    var sql = @&quot;SELECT * FROM items&quot;;
    var data = db.Query(sql).ToList();

    var pageLength = 15;
    var page = Request[&quot;page&quot;].AsInt();   
    if (page==0) {page = 1;}

    var flexiGridItems = new
    {
        total = data.Count,
        page = page,
        rows = from n in data.Skip(pageLength*(page-1)).Take(pageLength)
               select new { id = n.id, cell = n }
    };

    var json = Json.Encode(flexiGridItems);
    Response.Write(json);
}</pre>
<p><strong>That is all that&#8217;s necessary to get the nice Flexigrid up&#8217;n running in a WebMatrix (WebPages) web site.</strong></p>
<p>In my next blog post I will explore the data updating functionality of Flexigrid.</p>
<p>Happy coding!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/1087/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/1087/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=1087&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2011/07/01/a-first-look-at-flexigrid-using-it-with-webmatrix-and-json/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/07/webgrid.png" medium="image">
			<media:title type="html">webgrid</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/07/flexigrid.png" medium="image">
			<media:title type="html">flexigrid</media:title>
		</media:content>
	</item>
		<item>
		<title>Fun with CoffeeScript, jStorage and jQuery</title>
		<link>http://joeriks.com/2011/03/13/fun-with-coffeescript-jstorage-and-jquery/</link>
		<comments>http://joeriks.com/2011/03/13/fun-with-coffeescript-jstorage-and-jquery/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 09:36:03 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://joeriks.wordpress.com/?p=863</guid>
		<description><![CDATA[Local storage I wanted to try browser local storage possibilities. I found that jStorage is a no brainer to use and works in most browsers (IE6+ (bigger storage size in IE8+), Chrome 4+. Firefox 2+, Safari 4+, Opera 10.50+): Set value Get value And yes it preserves data over browser restarts, and deleted cookies. CoffeeScript [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=863&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='588' height='361' src='http://www.youtube.com/embed/hAppwUrWDl8?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<h2>Local storage</h2>
<p>I wanted to try browser local storage possibilities. I found that <a href="http://www.jstorage.info/">jStorage</a> is a no brainer to use and works in most browsers (IE6+ (bigger storage size in IE8+), Chrome 4+. Firefox 2+, Safari 4+, Opera 10.50+):</p>
<p><strong>Set value</strong></p>
<pre class="brush: plain; title: ; notranslate">$.jStorage.set(key, value)</pre>
<p><strong>Get value</strong></p>
<pre class="brush: plain; title: ; notranslate">alert ($.jStorage.get(key)</pre>
<p>And yes it preserves data over browser restarts, and deleted cookies.</p>
<h2>CoffeeScript</h2>
<p>I also wanted to try out <a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a>, with which JavaScript syntax is made very nice. </p>
<p>Be sure to have a look at this videos: <a href="http://goo.gl/ZOIcg">Creator Jeremy Ashkenas about CoffeScript</a> <a href="http://goo.gl/JEybu">Ashkenas at jsconf</a></p>
<p>Here&#8217;s a complete &#8220;text editor&#8221; (ehrm&#8230;) CoffeScript code:</p>
<pre class="brush: plain; title: ; notranslate">&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;http://jashkenas.github.com/coffee-script/extras/coffee-script.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/scripts/js/jstorage.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/coffeescript&quot;&gt;
     
      $('#clear').click -&gt;
        $('#filecontents').val('')
        
      $('#save').click -&gt;
        key = $('#filename').val()
        value = $('#filecontents').val()
        $.jStorage.set(key,value)
        alert &quot;Saved&quot;
        
      $('#load').click -&gt;
        key = $('#filename').val()
        value = $.jStorage.get(key)
        $('#filecontents').val(value)
        alert &quot;Loaded&quot;
     
&lt;/script&gt;</pre>
<h3>&#8230; and with a little polishing&#8230;</h3>
<pre class="brush: plain; title: ; notranslate">&lt;script type=&quot;text/coffeescript&quot;&gt;

  ### Define functions ###

  showStatus = (message) -&gt;
    $('#status').show().html(message).fadeOut(2222)
  
  clearValue = -&gt;
    $('#filecontents').val('')
    showStatus 'Cleared'        
    
  saveValue = -&gt;
    key = $('#filename').val()
    value = $('#filecontents').val()
    $.jStorage.set(key,value)
    showStatus 'Saved'
  
  loadValue = -&gt;
    key = $('#filename').val()
    value = $.jStorage.get(key)
    $('#filecontents').val(value)
    showStatus 'Loaded'       
    
  ### Hook functions to events ###

  $('#clear').click -&gt; clearValue()       
  $('#save').click -&gt; saveValue()         
  $('#load').click -&gt; loadValue()    

&lt;/script&gt;
</pre>
<p>The markup:</p>
<pre class="brush: plain; title: ; notranslate">  &lt;h1&gt;CoffeeScript, jStorage and jQuery&lt;span id=&quot;message&quot;&gt;&lt;/span&gt;&lt;/h1&gt;
  &quot;Filename&quot;:&lt;input type=&quot;text&quot; id=&quot;filename&quot; value=&quot;default&quot;/&gt;
  &lt;a href=&quot;#&quot; id=&quot;clear&quot;&gt;Clear&lt;/a&gt;
  &lt;a href=&quot;#&quot; id=&quot;save&quot;&gt;Save&lt;/a&gt;
  &lt;a href=&quot;#&quot; id=&quot;load&quot;&gt;Load&lt;/a&gt;&lt;br/&gt;
  &lt;span id=&quot;status&quot;&gt;&lt;/span&gt;
  &lt;br/&gt;
  &lt;textarea id=&quot;filecontents&quot; rows=3 cols=40&gt;&lt;/textarea&gt;</pre>
<h2>Conclusion</h2>
<p>I think I found two new toys, err&#8230; tools for my dev toolbox today.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/863/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/863/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=863&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2011/03/13/fun-with-coffeescript-jstorage-and-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>
	</item>
		<item>
		<title>5 min: WebMatrix + jQuery UI + json + jQuery templates</title>
		<link>http://joeriks.com/2011/01/30/5-min-webmatrix-jquery-ui-json-jquery-templates/</link>
		<comments>http://joeriks.com/2011/01/30/5-min-webmatrix-jquery-ui-json-jquery-templates/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 13:10:55 +0000</pubDate>
		<dc:creator>joeriks</dc:creator>
				<category><![CDATA[AspNet]]></category>
		<category><![CDATA[Javascript / Html5]]></category>
		<category><![CDATA[WebMatrix]]></category>

		<guid isPermaLink="false">http://joeriks.wordpress.com/?p=718</guid>
		<description><![CDATA[Lets use WebMatrix to create a very basic db &#8211; &#62; ui sample using jQuery goodness. We&#8217;ll get from 0 to a working sample in five minutes (more or less). 1. The WebMatrix site First create a new site in WebMatrix using the Empty site template. 2. Database Next define a Sql CE database file, [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=718&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Lets use WebMatrix to create a very basic db &#8211; &gt; ui sample using jQuery goodness. We&#8217;ll get from 0 to a working sample in five minutes (more or less).</strong></p>
<h2>1. The WebMatrix site</h2>
<p>First create a new site in WebMatrix using the Empty site template.</p>
<h2>2. Database</h2>
<p>Next define a Sql CE database file, call it <strong>db</strong> and add a table named <strong>product</strong>:</p>
<p><a href="http://joeriks.files.wordpress.com/2011/01/wmjq_db2.png"><img src="http://joeriks.files.wordpress.com/2011/01/wmjq_db2.png?w=600&#038;h=358" alt="" title="Define database" width="600" height="358" class="alignnone size-medium wp-image-723" /></a></p>
<p>Also add some demo data in the table, so you have something to show later on.</p>
<h2>3. Json</h2>
<p>Now it&#8217;s time to create a file that will serve a list of records from the database using json. For that reason, go create <strong>products.cshtml</strong> with this simple code:</p>
<pre class="brush: plain; title: ; notranslate">@{
    var db = Database.Open(&quot;db&quot;);
    var sql = @&quot;SELECT * FROM product&quot;;
    var data = db.Query(sql);
    Json.Write(data, Response.Output);
}</pre>
<p>So now we have data and some code to read it. So try it out by running products.cshtml. You should see a json representation of all your products in your browser window:</p>
<pre class="brush: plain; title: ; notranslate">[{&quot;id&quot;:1,&quot;name&quot;:&quot;a product&quot;},{&quot;id&quot;:2,&quot;name&quot;:&quot;another one&quot;},{&quot;id&quot;:3,&quot;name&quot;:&quot;a third&quot;}]</pre>
<h2>4. The master template with references to js libraries</h2>
<p>We are going to use some jQuery libraries in this sample. The <strong>jQuery</strong> base, <strong>jQuery UI</strong> for some nice looking UI and <strong>jQuery templates</strong> to render json data to html in a very conveniant template fashion.</p>
<p>All the libraries are available online, and there&#8217;s no need to use local copies at this point.</p>
<p>So, create a <strong>_layout.cshtml</strong> with the following content:</p>
<pre class="brush: plain; title: ; notranslate">&lt;!DOCTYPE html&gt;

&lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;utf-8&quot; /&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/ui-lightness/jquery-ui.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; /&gt;
		&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
		&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;        
        &lt;script src=&quot;http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

    &lt;/head&gt;
    &lt;body&gt;
        @RenderBody()
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<h2>5. The main page</h2>
<p>The last file in our WebMatrix project is the <strong>index.cshtml</strong> which only includes a jQuery UI styled button and a div placeholder for the result to begin with. On the click of that button we make a ajax call to get the data from our table, and render it to Html. </p>
<p><a href="http://joeriks.files.wordpress.com/2011/01/wbjs_1.png"><img src="http://joeriks.files.wordpress.com/2011/01/wbjs_1.png?w=600&#038;h=333" alt="" title="Index page" width="600" height="333" class="alignnone size-medium wp-image-733" /></a></p>
<p>Here&#8217;s the complete code:</p>
<pre class="brush: plain; title: ; notranslate">@{
    Layout=&quot;~/_Layout.cshtml&quot;;
}

&lt;script&gt;

    function renderResult(data)
    {
        // define the jQuery template with html code and
        // proper property names from the data query result       
        var markup = &quot;&lt;li&gt;${id} : ${name}&lt;/li&gt;&quot;;                                        
        $.template(&quot;listTemplate&quot;, markup);                    
        
        // render the template using the json data
        // iterates automatically over each each item in data        
        $.tmpl(&quot;listTemplate&quot;, data ).appendTo( &quot;#result&quot; );        
        
    }
	$(function() {
    
        // create good looking jQuery UI button element        
        $( &quot;.button&quot; ).button();
        
        // hook a getJSON to the click event of the button        
        $( &quot;#getdata&quot; ).click(function() { 
            $.getJSON('products.cshtml',renderResult);
        });
	});
&lt;/script&gt;

&lt;div id=&quot;getdata&quot; class=&quot;button&quot;&gt;Get data&lt;/div&gt;
&lt;ul id=&quot;result&quot;&gt;&lt;/ul&gt;
</pre>
<p>Here&#8217;s what your complete project in WebMatrix should look like, only three cshtml files and one database:<br />
<a href="http://joeriks.files.wordpress.com/2011/01/wbcomplete.png"><img src="http://joeriks.files.wordpress.com/2011/01/wbcomplete.png?w=600&#038;h=364" alt="" title="WebMatrix" width="600" height="364" class="alignnone size-medium wp-image-737" /></a></p>
<h2>Result</h2>
<p><a href="http://joeriks.files.wordpress.com/2011/01/wbjs_2.png"><img src="http://joeriks.files.wordpress.com/2011/01/wbjs_2.png?w=600&#038;h=333" alt="" title="Result" width="600" height="333" class="alignnone size-medium wp-image-732" /></a></p>
<h2>Conclusion</h2>
<p>Simple as that! Fun eh? And really useful I&#8217;d say.</p>
<blockquote><p>Supporting non-js browsers when developing webapps sometimes feels like keeping support for Windows 3.11 when developing Windows apps. <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joeriks.wordpress.com/718/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joeriks.wordpress.com/718/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joeriks.com&#038;blog=11388728&#038;post=718&#038;subd=joeriks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joeriks.com/2011/01/30/5-min-webmatrix-jquery-ui-json-jquery-templates/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5c1ed377a5b2e96c567e4184a7be70b1?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">joeriks</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/01/wmjq_db2.png?w=600" medium="image">
			<media:title type="html">Define database</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/01/wbjs_1.png?w=600" medium="image">
			<media:title type="html">Index page</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/01/wbcomplete.png?w=600" medium="image">
			<media:title type="html">WebMatrix</media:title>
		</media:content>

		<media:content url="http://joeriks.files.wordpress.com/2011/01/wbjs_2.png?w=600" medium="image">
			<media:title type="html">Result</media:title>
		</media:content>
	</item>
	</channel>
</rss>
