<?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>Jonathan Channon&#039;s Blog</title> <atom:link href="http://blog.jonathanchannon.com/feed/rss2" rel="self" type="application/rss+xml" /><link>http://blog.jonathanchannon.com</link> <description>Thoughts, Findings, Recommendations &#38; Questions</description> <lastBuildDate>Tue, 30 Apr 2013 12:53:48 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.4.2</generator> <item><title>Mocking HttpContext with Fake It Easy</title><link>http://blog.jonathanchannon.com/2013/04/30/mocking-httpcontext-with-fake-it-easy/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-httpcontext-with-fake-it-easy</link> <comments>http://blog.jonathanchannon.com/2013/04/30/mocking-httpcontext-with-fake-it-easy/#comments</comments> <pubDate>Tue, 30 Apr 2013 12:48:09 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[.net]]></category> <category><![CDATA[asp.net mvc]]></category> <category><![CDATA[fake it easy]]></category> <category><![CDATA[nancyfx]]></category> <category><![CDATA[oss]]></category> <category><![CDATA[unit testing]]></category> <category><![CDATA[xunit]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=620</guid> <description><![CDATA[<p>Lets start with the conclusion first and say &#8220;use Nancy for your web applications and APIs&#8221; as its brilliant!</p><p>If you want to continue reading lets crack on.</p><p>I&#8217;m currently working on a ASP.Net MVC project and one of the controller methods writes directly to the Response, <em>eg. Response.Write(&#8220;How will I mock thee?&#8221;);</em></p><p>Having moved over to xUnit and FakeItEasy recently I wanted to write a unit or integration test depending how you see it to assert against the Http Response.</p><p>Doing this is no easy feat with MVC (with Nancy its all done for you) and you have to mock a lot of things.  I&#8217;m hoping that in later releases this will be fixed because I know that ASP.Net Web API has made things a bit easier for testing (and wrote a testing library for it) so I assume the two projects will use bits of each other or their roadmap will merge.</p><p>I found that there a quite a lot of samples with Moq but nothing for Fake It Easy(FIE) so I checked in at the FIE [<a
href="http://blog.jonathanchannon.com/2013/04/30/mocking-httpcontext-with-fake-it-easy/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>Lets start with the conclusion first and say &#8220;use <a
href="http://nancyfx.org">Nancy</a> for your web applications and APIs&#8221; as its brilliant!</p><p>If you want to continue reading lets crack on.</p><p>I&#8217;m currently working on a ASP.Net MVC project and one of the controller methods writes directly to the Response, <em>eg. Response.Write(&#8220;How will I mock thee?&#8221;);</em></p><p>Having moved over to <a
href="http://xunit.codeplex.com/">xUnit</a> and <a
href="https://github.com/FakeItEasy/FakeItEasy">FakeItEasy</a> recently I wanted to write a unit or integration test depending how you see it to assert against the Http Response.</p><p>Doing this is no easy feat with MVC (with Nancy its all <a
href="https://github.com/NancyFx/Nancy/wiki/Testing-your-application">done for you</a>) and you have to mock a lot of things.  I&#8217;m hoping that in later releases this will be fixed because I know that ASP.Net Web API has made things a bit easier for testing (and wrote a testing library for it) so I assume the two projects will use bits of each other or their roadmap will merge.</p><p>I found that there a quite a lot of samples with Moq but nothing for Fake It Easy(FIE) so I checked in at the <a
href="https://jabbr.net/#/rooms/fakeiteasy">FIE Jabbr room</a> and got some help and worked through some ideas and below is the result.</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:455px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/>27<br
/>28<br
/>29<br
/>30<br
/>31<br
/>32<br
/>33<br
/>34<br
/>35<br
/>36<br
/>37<br
/>38<br
/>39<br
/>40<br
/>41<br
/>42<br
/>43<br
/>44<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #008080; font-style: italic;">//Controller</span><br
/> <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">class</span> MyController <span
style="color: #008000;">:</span> Controller<br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#91;</span>HttpPost<span
style="color: #008000;">&#93;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">void</span> Index<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; Response<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Write</span><span
style="color: #008000;">&#40;</span><span
style="color: #666666;">&quot;This is fiddly&quot;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; Response<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Flush</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <span
style="color: #008000;">&#125;</span><br
/> <br
/> <span
style="color: #008080; font-style: italic;">//Unit Test</span><br
/> <span
style="color: #008000;">&#91;</span>Fact<span
style="color: #008000;">&#93;</span><br
/> <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">void</span> Should_contain_fiddly_in_response<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; var sb <span
style="color: #008000;">=</span> <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> StringBuilder<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; var formCollection <span
style="color: #008000;">=</span> <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> NameValueCollection<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; formCollection<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Add</span><span
style="color: #008000;">&#40;</span><span
style="color: #666666;">&quot;MyPostedData&quot;</span>,<span
style="color: #666666;">&quot;Boo&quot;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; var request <span
style="color: #008000;">=</span> A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Fake</span><span
style="color: #008000;">&lt;</span>HttpRequestBase<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">CallTo</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> request<span
style="color: #008000;">.</span><span
style="color: #0000FF;">HttpMethod</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">Returns</span><span
style="color: #008000;">&#40;</span><span
style="color: #666666;">&quot;POST&quot;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">CallTo</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> request<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Headers</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">Returns</span><span
style="color: #008000;">&#40;</span><a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> NameValueCollection<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">CallTo</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> request<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Form</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">Returns</span><span
style="color: #008000;">&#40;</span>formCollection<span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">CallTo</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> request<span
style="color: #008000;">.</span><span
style="color: #0000FF;">QueryString</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">Returns</span><span
style="color: #008000;">&#40;</span><a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> NameValueCollection<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; var response <span
style="color: #008000;">=</span> A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Fake</span><span
style="color: #008000;">&lt;</span>HttpResponseBase<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">CallTo</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> response<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Write</span><span
style="color: #008000;">&#40;</span>A<span
style="color: #008000;">&lt;</span><span
style="color: #6666cc; font-weight: bold;">string</span><span
style="color: #008000;">&gt;.</span><span
style="color: #0000FF;">Ignored</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">Invokes</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #6666cc; font-weight: bold;">string</span> x<span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> sb<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Append</span><span
style="color: #008000;">&#40;</span>x<span
style="color: #008000;">&#41;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; var mockHttpContext <span
style="color: #008000;">=</span> A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Fake</span><span
style="color: #008000;">&lt;</span>HttpContextBase<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">CallTo</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> mockHttpContext<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Request</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">Returns</span><span
style="color: #008000;">&#40;</span>request<span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">CallTo</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #008000;">=&gt;</span> mockHttpContext<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Response</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">Returns</span><span
style="color: #008000;">&#40;</span>response<span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; var controllerContext <span
style="color: #008000;">=</span> <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> ControllerContext<span
style="color: #008000;">&#40;</span>mockHttpContext, <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> RouteData<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span>, A<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Fake</span><span
style="color: #008000;">&lt;</span>ControllerBase<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; var myController <span
style="color: #008000;">=</span> <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> MyController<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ControllerContext <span
style="color: #008000;">=</span> controllerContext<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><span
style="color: #008000;">;</span><br
/> &nbsp; <br
/> &nbsp; &nbsp; myController<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Index</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; Assert<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Contains</span><span
style="color: #008000;">&#40;</span><span
style="color: #666666;">&quot;fiddly&quot;</span>, sb<span
style="color: #008000;">.</span><span
style="color: #0000FF;">ToString</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <span
style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>As we are not running this against a live web server we need to mock everything from base controller types to requests, responses and everything in between.  My example initiates a bit more than is required ie. querystring and headers but hopefully it demonstrates what&#8217;s needed.</p><p>Firstly we create an instance of a StringBuilderthat will store the response information that we can assert against.  We setup a NameValueCollection to add keys/values for posted data, we could do the same for headers etc if we wanted.</p><p>We then create an instance of a fake HttpRequestBase using FIE and setup all the relevant request properties.</p><p>We then create an instance of a fake HttpResponseBase and configure a callback that is invoked when our controller calls Response.Write.  We also configure it to watch any calls to Response.Write with any string using the FIE syntax of A<string>.Ignored, you could change it so it only looks for specific argument contents if you wanted.  When the method is called it then takes the argument and adds it to the StringBuilder.</p><p>We then create a fake instance of HttpContextBase and assign the properties of Request and Response to the previously setup fakes.</p><p>We then have to create a ControllerContext and pass the fake Http context, a route collection and a fake ControllerBase which the controller under test inherits off.  We then assign the controller context to an instance of the controller class we are testing.</p><p>We can now finally call the method under test and assert the results.</p><p>I would obviously recommend you put the fake setup in a factory method if you have multiple tests you class to prevent duplication. You can obviously then add header, querystring, form method arguments if you want the context populated with that kind of information.</p><p>Hope this helps anyone in a similar situation and provides a point of reference for the Fake It Easy project.</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/04/30/mocking-httpcontext-with-fake-it-easy/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Up &amp; Running with TypeScript and WebStorm</title><link>http://blog.jonathanchannon.com/2013/04/17/up-running-with-typescript-and-webstorm/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=up-running-with-typescript-and-webstorm</link> <comments>http://blog.jonathanchannon.com/2013/04/17/up-running-with-typescript-and-webstorm/#comments</comments> <pubDate>Wed, 17 Apr 2013 11:00:07 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[oss]]></category> <category><![CDATA[osx]]></category> <category><![CDATA[typescript]]></category> <category><![CDATA[webstorm]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=597</guid> <description><![CDATA[<p>I love my iMac and I&#8217;m on a mission to find a language I enjoy that I can use my Mac for (no Windows fan boy jokes please).  There&#8217;s something in my mind I associate with work and my Windows laptop.  Therefore, I don&#8217;t feel to excited about getting my laptop out of my bag in the evenings/weekends to play with other stuff.</p><p>As I want to broaden my knowledge I wanted to find something ideally statically typed (although I&#8217;m currently looking into Python) that would work on OS X.  I&#8217;ve said previously that JavaScript seems the way to go in my current situation so I thought I&#8217;d take a look at TypeScript and also use WebStorm from Jetbrains as my IDE seeing as I&#8217;ve heard so many great things about them and their products (don&#8217;t worry I use ReSharper).</p> TypeScript<p>So I went over to TypeScript&#8217;s website and followed the Hello World type code examples under the &#8220;Learn&#8221; tab.</p><p>After understanding the basics of it and wanting to learn more I spotted this demo [<a
href="http://blog.jonathanchannon.com/2013/04/17/up-running-with-typescript-and-webstorm/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>I love my iMac and I&#8217;m on a mission to find a language I enjoy that I can use my Mac for (no Windows fan boy jokes please).  There&#8217;s something in my mind I associate with work and my Windows laptop.  Therefore, I don&#8217;t feel to excited about getting my laptop out of my bag in the evenings/weekends to play with other stuff.</p><p>As I want to broaden my knowledge I wanted to find something ideally statically typed (although I&#8217;m currently looking into Python) that would work on OS X.  I&#8217;ve said previously that JavaScript seems the way to go in my current situation so I thought I&#8217;d take a look at TypeScript and also use <a
href="http://www.jetbrains.com/webstorm/">WebStorm</a> from Jetbrains as my IDE seeing as I&#8217;ve heard so many great things about them and their products (don&#8217;t worry I use ReSharper).</p><h2>TypeScript</h2><p>So I went over to TypeScript&#8217;s <a
href="http://www.typescriptlang.org/">website</a> and followed the Hello World type code examples under the &#8220;Learn&#8221; tab.</p><p>After understanding the basics of it and wanting to learn more I spotted this demo code:</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #003366; font-weight: bold;">class</span> Student <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; fullname <span
style="color: #339933;">:</span> string<span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; constructor<span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">public</span> firstname<span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">public</span> middleinitial<span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">public</span> lastname<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">this</span>.<span
style="color: #660066;">fullname</span> <span
style="color: #339933;">=</span> firstname <span
style="color: #339933;">+</span> <span
style="color: #3366CC;">&quot; &quot;</span> <span
style="color: #339933;">+</span> middleinitial <span
style="color: #339933;">+</span> <span
style="color: #3366CC;">&quot; &quot;</span> <span
style="color: #339933;">+</span> lastname<span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><br
/> <span
style="color: #009900;">&#125;</span><br
/> <br
/> interface Person <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; firstname<span
style="color: #339933;">:</span> string<span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; lastname<span
style="color: #339933;">:</span> string<span
style="color: #339933;">;</span><br
/> <span
style="color: #009900;">&#125;</span><br
/> <br
/> <span
style="color: #003366; font-weight: bold;">function</span> greeter<span
style="color: #009900;">&#40;</span>person <span
style="color: #339933;">:</span> Person<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">return</span> <span
style="color: #3366CC;">&quot;Hello, &quot;</span> <span
style="color: #339933;">+</span> person.<span
style="color: #660066;">firstname</span> <span
style="color: #339933;">+</span> <span
style="color: #3366CC;">&quot; &quot;</span> <span
style="color: #339933;">+</span> person.<span
style="color: #660066;">lastname</span><span
style="color: #339933;">;</span><br
/> <span
style="color: #009900;">&#125;</span><br
/> <br
/> <span
style="color: #003366; font-weight: bold;">var</span> user <span
style="color: #339933;">=</span> <span
style="color: #003366; font-weight: bold;">new</span> Student<span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">&quot;Jane&quot;</span><span
style="color: #339933;">,</span> <span
style="color: #3366CC;">&quot;M.&quot;</span><span
style="color: #339933;">,</span> <span
style="color: #3366CC;">&quot;User&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> document.<span
style="color: #660066;">body</span>.<span
style="color: #660066;">innerHTML</span> <span
style="color: #339933;">=</span> greeter<span
style="color: #009900;">&#40;</span>user<span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></td></tr></tbody></table></div><p>There is an interface that expects public properties of firstname and lastname. There is a Student class that has a constructor with arguments for firstname, middleinitial and lastname.  TypeScript constructor arguments are shorthand for making the arguments properties on the object itself without having to code all that yourself.  We then have a function called greeter that takes a method argument of our Person interface and uses the properties of it to return a string.  We then create a new instance of the Student class and then call the greeter function with our instance. Woooaaahhh! I want static typing, WTF is going on here?  Essentially TypeScript allows for <a
href="http://en.wikipedia.org/wiki/Duck_typing">Duck Typing</a> where any object that makes calls to or uses properties that match another type, it will allow.  Now technically you can do this in C# by using the &#8220;dynamic&#8221; keyword but I would still keep and one to one mapping even if it was dynamic/duck typed so not to confuse future users.</p><p>It may be this example that I don&#8217;t like where properties are being un-used or the fact that the keyword &#8220;interface&#8221; is being used and the class is not statically implementing it but I guess this is how TypeScript uses interfaces ie/duck typing.  I can&#8217;t think of a reason why you&#8217;d have an object that exposes properties that are not used when they have been shoe horned/duck typed into another with fewer properties etc, however, this might just be my statically typed mind not liking it and it should chill and get with the dynamic nature of things!</p><h2>Webstorm</h2><p>So in my quest to develop something on my Mac I chose Webstorm as my IDE. I haven&#8217;t heard one bad thing against Jetbrains so it must be good right? Must be simple and intuitive? Ummm, no.  Webstorm has support for TypeScript and uses the computer&#8217;s version of TypeScript to compile *.ts files into *.js files.  So I installed Node.js and TypeScript (<em>npm install -g typescript</em>) and fired up Webstorm.  Webstorm offers various project types to create new projects from but unfortunately no TypeScript one so I created an empty application.  I added a *.ts file and Webstorm spots that this is a TypeScript file and wants to add a File Watcher.  This means every time a change is made it recompiles in the background to produce *.js, that seems a bit keen for my liking and would prefer it on every time the file is saved but each to their own.  I copied the above code into my my *.ts file.  I then created a html file and referenced the JavaScript file that was created from the TypeScript compilation in the head section. (I&#8217;m using the term compilation but I guess it should be transpiling but you know what I&#8217;m getting at).</p><p>Ok, we&#8217;re ready to go and debug and here comes the confusion!</p><p>Being used to Visual Studio I thought it would start a web server instance up, open my system browser and away we go but unfortunately not.  I was baffled but that doesn&#8217;t take much!  I found the Run menu item and it had a Debug option and it then popped up window with a Edit Configuration option so I clicked that and it came up with the below:</p><p><a
href="http://blog.jonathanchannon.com/wp-content/uploads/2013/04/WebstormDebug.png"><img
src="http://blog.jonathanchannon.com/wp-content/uploads/2013/04/WebstormDebug-620x390.png" alt="WebStorm Debug" title="Webstorm Debug" width="620" height="390" class="aligncenter size-medium wp-image-608" /></a></p><p>As there was no TypeScript option I assumed JavaScript was the option to go with.  Under this option it has Local and Remote menu items to choose from.  If you look at Local it autofills a path to the index.html on the filesystem, under the Remote option it shows the project structure and asks for a URL.  I tried localhost:1234 but nothing so I went back to the filesystem option and clicked Apply but that doesn&#8217;t actually do anything.</p><p>So went through it all again and saw the + icon in the screenshot and it gives you an option to add a JavaScript configuration and is a complete mirror of the above screenshot. Very odd.  Clicked Apply, then tried to Debug again and it gave me the configuration I chose and then opened Chrome. Nothing. WTF!</p><p>Had a rummage around WebStorm and saw a note about needing a Jetbrains Chrome extension to debug. So off to the Chrome Store I went and installed the plugin.  Tried to Debug and it now opened a browser with my page so things are looking good.  Went to the TypeScript file and put a breakpoint on a line but it wasn&#8217;t getting hit. I then put a breakpoint on a line in the JavaScript file and it got hit.  Yay!  But no content was in my page.  Baffled again.  WebStorm was saying that it couldn&#8217;t set the innerHTML property of null.  Eh? How can a body be null?  After a while of head scratching I sussed it.  Those of you that are eagle eyed will have spotted that I put the script declaration in my head and therefore when it got executed there was no such thing as a body tag in the DOM yet.  So I moved the script declaration to the body and bingo we have content.  I should know better and should have put the script tag at the bottom of the body anyway but this was just a &#8220;Hello World&#8221; app so wasn&#8217;t too worried.</p><p>Anyway it now works and either I&#8217;m too entrenched in Visual Studio and should learn to adapt to new IDE&#8217;s or I expect things to just work without having to <a
href="http://en.wikipedia.org/wiki/RTFM">RTFM</a>.  I must say if I have to read the manual on how to use pieces of software you have an instant fail however, as I&#8217;ve heard such great things about Jetbrains and Webstorm I&#8217;m willing to give it another go but it has me on the back foot already.  I&#8217;m also still unsure how to set WebStorm up to use a local web server because debugging by pointing to a file system seems wrong, if anyone knows please let me know.</p><p>I hope this helps someone else to get up and running with TypeScript and WebStorm, if not I&#8217;m sure there are plenty who&#8217;ve had a laugh from this article asking how one person can be so dumb <img
src='http://blog.jonathanchannon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/04/17/up-running-with-typescript-and-webstorm/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Using DateTime in C# and SQL</title><link>http://blog.jonathanchannon.com/2013/04/08/using-datetime-in-c-and-sql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-datetime-in-c-and-sql</link> <comments>http://blog.jonathanchannon.com/2013/04/08/using-datetime-in-c-and-sql/#comments</comments> <pubDate>Mon, 08 Apr 2013 15:06:58 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=589</guid> <description><![CDATA[<p>I&#8217;m sure there are millions of blog posts out there that already discuss this but I think its worth noting down even if its just something for me to remember.</p><p>Store your datetimes in UTC format into the database.  Unfortunately this mean executing something like:</p><p>myObject.ExpiryDate = TimeZoneInfo.ConvertTimeToUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById(&#8220;timezoneid of users&#8221;)</p><p>In every central place where you update/insert DateTime values on your objects you will need the above.</p><p>When you display any DateTime information it must display as a local datetime value. You can do this by using myObject.ExpiryDate.ToLocalTime()</p><p>An example of this is if you stored a DateTime that was converted to UTC and it stored April 8th 14:30 2013, when a user in USA was given the date in their web application they would see it as April 8th 10:30 but UK users would read it as April 8th 15:30</p><p>If you have an existing application you need to make sure the server side logic regarding date storage converts to UTC and that your view layer converts to local time.</p><p>Its risky but if your users are all [<a
href="http://blog.jonathanchannon.com/2013/04/08/using-datetime-in-c-and-sql/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>I&#8217;m sure there are millions of blog posts out there that already discuss this but I think its worth noting down even if its just something for me to remember.</p><p>Store your datetimes in UTC format into the database.  Unfortunately this mean executing something like:</p><p>myObject.ExpiryDate = TimeZoneInfo.ConvertTimeToUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById(&#8220;timezoneid of users&#8221;)</p><p>In every central place where you update/insert DateTime values on your objects you will need the above.</p><p>When you display any DateTime information it must display as a local datetime value. You can do this by using myObject.ExpiryDate.ToLocalTime()</p><p>An example of this is if you stored a DateTime that was converted to UTC and it stored April 8th 14:30 2013, when a user in USA was given the date in their web application they would see it as April 8th 10:30 but UK users would read it as April 8th 15:30</p><p>If you have an existing application you need to make sure the server side logic regarding date storage converts to UTC and that your view layer converts to local time.</p><p>Its risky but if your users are all on the same time zone and the database server and all operating systems are the same you don&#8217;t need to worry about this and can use DateTime.Now for everything.</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/04/08/using-datetime-in-c-and-sql/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Using a Markdown ViewEngine with Nancy</title><link>http://blog.jonathanchannon.com/2013/04/08/using-a-markdown-viewengine-with-nancy/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-a-markdown-viewengine-with-nancy</link> <comments>http://blog.jonathanchannon.com/2013/04/08/using-a-markdown-viewengine-with-nancy/#comments</comments> <pubDate>Mon, 08 Apr 2013 09:00:13 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[.net]]></category> <category><![CDATA[community]]></category> <category><![CDATA[markdown]]></category> <category><![CDATA[nancyfx]]></category> <category><![CDATA[oss]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=540</guid> <description><![CDATA[<p>Whilst using stackoverflow.com and Github gists I&#8217;ve become a frequent user of Markdown.</p><p>For those of you that don&#8217;t know what Markdown is, its essentially a shorter/cleaner syntax that can be parsed to produce HTML.    Below are a few examples:</p><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#Hello World!<br
/> ##You're awesome! <br
/> The quick brown fox jumped over the lazy coder<br
/> What the **hell** is this? <br
/> This is an [example link](http://example.com/)</div></td></tr></tbody></table></div><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #009900;">&#60;<span
style="color: #000000; font-weight: bold;">h1</span>&#62;</span>Hello World!<span
style="color: #009900;">&#60;<span
style="color: #66cc66;">/</span><span
style="color: #000000; font-weight: bold;">h1</span>&#62;</span><br
/> <span
style="color: #009900;">&#60;<span
style="color: #000000; font-weight: bold;">h2</span>&#62;</span>You're awesome!<span
style="color: #009900;">&#60;<span
style="color: #66cc66;">/</span><span
style="color: #000000; font-weight: bold;">h2</span>&#62;</span><br
/> <span
style="color: #009900;">&#60;<span
style="color: #000000; font-weight: bold;">p</span>&#62;</span>The quick brown fox jumped over the lazy coder<span
style="color: [<a href="http://blog.jonathanchannon.com/2013/04/08/using-a-markdown-viewengine-with-nancy/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>Whilst using <a
href="http://stackoverflow.com">stackoverflow.com</a> and <a
href="https://gist.github.com/">Github gists</a> I&#8217;ve become a frequent user of <a
href="http://daringfireball.net/projects/markdown/">Markdown</a>.</p><p>For those of you that don&#8217;t know what Markdown is, its essentially a shorter/cleaner syntax that can be parsed to produce HTML.    Below are a few examples:</p><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#Hello World!<br
/> ##You're awesome! <br
/> The quick brown fox jumped over the lazy coder<br
/> What the **hell** is this? <br
/> This is an [example link](http://example.com/)</div></td></tr></tbody></table></div><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/h1.html"><span
style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span>Hello World!<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/h1.html"><span
style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span><br
/> <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/h2.html"><span
style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span>You're awesome!<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/h2.html"><span
style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span><br
/> <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/p.html"><span
style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>The quick brown fox jumped over the lazy coder<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/p.html"><span
style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br
/> What the <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/strong.html"><span
style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>hell<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/strong.html"><span
style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> is this?<br
/> This is an <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/a.html"><span
style="color: #000000; font-weight: bold;">a</span></a> <span
style="color: #000066;">href</span><span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;http://example.com/&quot;</span>&gt;</span> example link<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/a.html"><span
style="color: #000000; font-weight: bold;">a</span></a>&gt;</span></div></td></tr></tbody></table></div><p>You can see more examples in the earlier link.</p><p>When you&#8217;re writing a blog post or a lengthy page in your web app with lots of HTML it maybe easier to use Markdown as your preferred syntax.  I currently use WordPress for my blog, it&#8217;s ok but its quite bloated for probably what I need.  I looked into <a
href="http://calepin.co/">Calepin</a> and <a
href="http://scriptogr.am/">Scriptogr.am</a> as alternative blogging platforms but I felt it didn&#8217;t quite offer what I wanted but the approach was a good idea.  It meant you could write a blog post and simply put the file in dropbox and it would appear on your blog.</p><p>As you may be aware, I&#8217;m a great fan of <a
href="http://nancyfx.org/">Nancy</a> and the team/contributors were discussing having something similar to Calepin for Nancy so users could upload a Markdown file as a pull request or community article or have something that uses Markdown that can be plugged into Jekyll.</p><p>I initially looked into how hard it would be to convert Markdown into HTML with Nancy and as ever, very easy!  I found some libraries that already provided the conversion process and decided upon <a
href="http://nuget.org/packages/MarkdownSharp/">MarkdownSharp</a>.</p><p>I then thought we could use Markdown as a proper view engine like Razor.  I was unsure of how to provide the ability of model binding, master pages etc but after speaking to <a
href="http://twitter.com/grumpydev">@grumpydev</a> he suggested using the built in view engine with Nancy to provide that functionality.</p><p>What this means is that you can have a Nancy route such as:</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">class</span> SampleModule <span
style="color: #008000;">:</span> NancyModule<br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> SampleModule<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; Get<span
style="color: #008000;">&#91;</span><span
style="color: #666666;">&quot;/&quot;</span><span
style="color: #008000;">&#93;</span> <span
style="color: #008000;">=</span> _ <span
style="color: #008000;">=&gt;</span> <br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var model <span
style="color: #008000;">=</span> <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> UserModel<span
style="color: #008000;">&#123;</span>FirstName <span
style="color: #008000;">=</span> <span
style="color: #666666;">&quot;John&quot;</span>, LastName <span
style="color: #008000;">=</span> <span
style="color: #666666;">&quot;Smith&quot;</span><span
style="color: #008000;">&#125;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">return</span> View<span
style="color: #008000;">&#91;</span><span
style="color: #666666;">&quot;Home&quot;</span>, model<span
style="color: #008000;">&#93;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <span
style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>This will find a Markdown file called Home.md or Home.markdown, translate the model and convert the markdown into a HTML view.  You could define you markdown view like so:</p><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#Hi there!<br
/> <br
/> My first name is @Model.FirstName and my last name is @Model.LastName</div></td></tr></tbody></table></div><p>This would then translate to:</p><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/h1.html"><span
style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span>Hi there!<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/h1.html"><span
style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span><br
/> <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/p.html"><span
style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>My first name is John and my last name is Smith<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/p.html"><span
style="color: #000000; font-weight: bold;">p</span></a>&gt;</span></div></td></tr></tbody></table></div><p>This is a simple demonstration but what it means is that you can now use the Markdown view engine instead of Razor if you so wished.  Things like model binding, master pages, partials, model iteration etc etc can all be handled by the view engine and your views can be written in Markdown.  To use features like partials and master pages you use the Super Simple View Engine syntax that as it sounds is super simple.</p><p>When I wrote the Markdown view engine I also wrote a demo with the easy approach of Calepin in mind.</p><p>I wrote a HTML view to act as my master page and then a route in Nancy that would take anything after the URL and find the view for it:</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Get<span
style="color: #008000;">&#91;</span><span
style="color: #666666;">&quot;/{viewname}&quot;</span><span
style="color: #008000;">&#93;</span> <span
style="color: #008000;">=</span> parameters <span
style="color: #008000;">=&gt;</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; var popularposts <span
style="color: #008000;">=</span> GetModel<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; dynamic postModel <span
style="color: #008000;">=</span> <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
style="color: #008000;">new</span></a> ExpandoObject<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; postModel<span
style="color: #008000;">.</span><span
style="color: #0000FF;">PopularPosts</span> <span
style="color: #008000;">=</span> popularposts<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; postModel<span
style="color: #008000;">.</span><span
style="color: #0000FF;">MetaData</span> <span
style="color: #008000;">=</span> popularposts<span
style="color: #008000;">.</span><span
style="color: #0000FF;">FirstOrDefault</span><span
style="color: #008000;">&#40;</span>x <span
style="color: #008000;">=&gt;</span> x<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Slug</span> <span
style="color: #008000;">==</span> parameters<span
style="color: #008000;">.</span><span
style="color: #0000FF;">viewname</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">return</span> View<span
style="color: #008000;">&#91;</span><span
style="color: #666666;">&quot;Posts/&quot;</span> <span
style="color: #008000;">+</span> parameters<span
style="color: #008000;">.</span><span
style="color: #0000FF;">viewname</span>, postModel<span
style="color: #008000;">&#93;</span><span
style="color: #008000;">;</span><br
/> <span
style="color: #008000;">&#125;</span><span
style="color: #008000;">;</span></div></td></tr></tbody></table></div><p>In the Markdown file I would reference the master page, some partials, the model and a custom section for meta data about the blog post:</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:455px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/>27<br
/>28<br
/>29<br
/>30<br
/>31<br
/>32<br
/>33<br
/>34<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Master<span
style="color: #008000;">&#91;</span><span
style="color: #666666;">'master'</span><span
style="color: #008000;">&#93;</span><br
/> <br
/> @Tags<br
/> Date<span
style="color: #008000;">:</span> <span
style="color: #FF0000;">15</span><span
style="color: #008000;">/</span>03<span
style="color: #008000;">/</span><span
style="color: #FF0000;">2013</span><br
/> Title<span
style="color: #008000;">:</span> Readme<br
/> Tags<span
style="color: #008000;">:</span> Nancy,Runtime<br
/> @EndTags<br
/> <br
/> @Section<span
style="color: #008000;">&#91;</span><span
style="color: #666666;">'Content'</span><span
style="color: #008000;">&#93;</span><br
/> <br
/> @<span
style="color: #0600FF; font-weight: bold;">Partial</span><span
style="color: #008000;">&#91;</span><span
style="color: #666666;">'blogheader'</span>, Model<span
style="color: #008000;">.</span><span
style="color: #0000FF;">MetaData</span><span
style="color: #008000;">&#93;</span><span
style="color: #008000;">;</span><br
/> <br
/> <span
style="color: #008080;"># Readme!</span><br
/> <br
/> <span
style="color: #008080;">## Markdown Viewengine</span><br
/> <br
/> <span
style="color: #0600FF; font-weight: bold;">This</span> Markdown Viewengine allows views to be written <span
style="color: #0600FF; font-weight: bold;">in</span> Markdown<span
style="color: #008000;">.</span><br
/> <br
/> <span
style="color: #008000;">*</span> Full Model support<br
/> <span
style="color: #008000;">*</span> Master page support<br
/> <span
style="color: #008000;">*</span> Supports HTML within any MD content<br
/> <span
style="color: #008000;">*</span> Simple call `<span
style="color: #0600FF; font-weight: bold;">return</span> View<span
style="color: #008000;">&#91;</span><span
style="color: #666666;">&quot;Home&quot;</span><span
style="color: #008000;">&#93;</span>` <span
style="color: #0600FF; font-weight: bold;">for</span> Nancy to render your MD file<br
/> <br
/> <span
style="color: #008080;">## Markdown Blog Demo</span><br
/> <br
/> <span
style="color: #008000;">*</span> Allows <span
style="color: #0600FF; font-weight: bold;">for</span> dropping <span
style="color: #0600FF; font-weight: bold;">in</span> Markdown files into a directory<br
/> <span
style="color: #008000;">*</span> Allows <span
style="color: #0600FF; font-weight: bold;">for</span> future post scheduling<br
/> <span
style="color: #008000;">*</span> Generates slug automatically<br
/> <span
style="color: #008000;">*</span> Uses meta data <span
style="color: #0600FF; font-weight: bold;">for</span> date, tags, title<br
/> <span
style="color: #008000;">*</span> Allows <span
style="color: #0600FF; font-weight: bold;">for</span> custom HTML design<br
/> <br
/> @<span
style="color: #0600FF; font-weight: bold;">Partial</span><span
style="color: #008000;">&#91;</span><span
style="color: #666666;">'blogfooter'</span>, Model<span
style="color: #008000;">.</span><span
style="color: #0000FF;">MetaData</span><span
style="color: #008000;">&#93;</span><span
style="color: #008000;">;</span><br
/> <br
/> @EndSection</div></td></tr></tbody></table></div><p>This would then render a good looking website with all the features expected in a view engine.  It would use the meta data provided to write out things like page titles and tags for a blog post.  It would also mean you could use the approach to write your own blog using Nancy and Markdown.</p><p>The GetModel() method used above checks a &#8220;Posts&#8221; directory for Markdown files and reads the metadata for them.  What this means is that you can easily write a blog post using Nancy, Markdown and SuperSimpleViewEngine by just uploading the markdown file to a specific directory.</p><p>Ideally I would like to get rid of the master page and partial references so you could just have Markdown content in your file but I haven&#8217;t figured that out yet.  Hopefully someone else can use the Markdown view engine or take the demo app and find an alternative way to provide the simplicity that Calepin offers.</p><p>Overall it&#8217;s another nice feature that Nancy can offer its users and I have enjoyed contributing to the OSS project and tinkering with code!</p><p>The Markdown View Engine and Demo will be released in 0.17 of Nancy due very soon!</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/04/08/using-a-markdown-viewengine-with-nancy/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>The Old Development Language Switcheroo</title><link>http://blog.jonathanchannon.com/2013/04/02/the-old-development-language-switcheroo/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-old-development-language-switcheroo</link> <comments>http://blog.jonathanchannon.com/2013/04/02/the-old-development-language-switcheroo/#comments</comments> <pubDate>Tue, 02 Apr 2013 09:00:27 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[c#]]></category> <category><![CDATA[career]]></category> <category><![CDATA[Dart]]></category> <category><![CDATA[F#]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[Kotlin]]></category> <category><![CDATA[node.js]]></category> <category><![CDATA[typescript]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=571</guid> <description><![CDATA[<p>As a C# developer I think I&#8217;m pretty safe in saying that its not going away any time soon however, its my opinion that to not become irrelevant you need to have options.  Some may argue that its better to be master of one than a Jack of all trades but lets just say you can&#8217;t find your next job in the primary language you want to work in. What do you do?</p><p>I have looked more into JavaScript recently and with that comes server and client scope for the use of the language so with some understanding of JS that would be probably the best bet for me however, as much as this may annoy some I think I actually prefer statically typed languages.</p><p>Now I know there is TypeScript that I could use for my node.js apps but I see that as more of a workaround and not a core feature of JavaScript.  I&#8217;m not saying anything bad against JS and the libraries/frameworks that I have used I like but I just find [<a
href="http://blog.jonathanchannon.com/2013/04/02/the-old-development-language-switcheroo/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>As a C# developer I think I&#8217;m pretty safe in saying that its not going away any time soon however, its my opinion that to not become irrelevant you need to have options.  Some may argue that its better to be master of one than a Jack of all trades but lets just say you can&#8217;t find your next job in the primary language you want to work in. What do you do?</p><p>I have looked more into JavaScript recently and with that comes server and client scope for the use of the language so with some understanding of JS that would be probably the best bet for me however, as much as this may annoy some I think I actually prefer statically typed languages.</p><p>Now I know there is <a
href="http://www.typescriptlang.org/">TypeScript</a> that I could use for my <a
href="http://nodejs.org/">node.js</a> apps but I see that as more of a workaround and not a core feature of JavaScript.  I&#8217;m not saying anything bad against JS and the libraries/frameworks that I have used I like but I just find it easier to learn and know what I can do when I have decent intellisense showing me what I can do with my code.</p><p>You could argue that I could go back to my first language of Delphi or I could look into C++, Java or PHP but my counter argument might just be laughter.  In my opinion these are old languages.  They may be reliable for some needs but I feel its looking back to the past somewhat and not looking forward.</p><p>I have recently come across <a
href="http://www.dartlang.org/">Dart</a> and want to spend some time looking into it to see if we suit each other.</p><blockquote
style="width:90%"><p>Dart is a class-based, object-oriented language with lexical scoping, closures, and optional static typing. Dart helps you build structured modern web apps and is easy to learn for a wide range of developers.</p></blockquote><p>There is also <a
href="http://www.tryfsharp.org/">F#</a> which I need to look at as my Twitter timeline has more and more references to this language.</p><p>There is also <a
href="http://kotlin.jetbrains.org/">Kotlin</a> but that&#8217;s as much as I know about it, the name.  I think its based on Java and that rings alarm bells for me but I&#8217;m sure <a
href="http://twitter.com/Cranialstrain">@Cranialstrain</a> will put me right.</p><p>There is also <a
href="http://golang.org/">Go</a> and I&#8217;m sure there are many others that I could find but rightly or wrongly when deciding upon a language which lets be honest is a big investment you need to know there are the available jobs out there in that language.  Its nice to learn new stuff and I highly recommend it but for me a whole new investment in a language has to warrant some pay offs such as employment.</p><p>I can&#8217;t say for sure what language I&#8217;d use if I had to change, at present most likely JavaScript but probably that is based out of limited options on my part, maybe I need to become a better polyglot programmer and learn some more languages to have better &#8220;options&#8221; and not become irrelevant.</p><p>Let me know what your primary language is and what you&#8217;d jump to if you had to.</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/04/02/the-old-development-language-switcheroo/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>The many approaches to Entity Framework</title><link>http://blog.jonathanchannon.com/2013/03/04/the-many-approaches-to-entity-framework/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-many-approaches-to-entity-framework</link> <comments>http://blog.jonathanchannon.com/2013/03/04/the-many-approaches-to-entity-framework/#comments</comments> <pubDate>Mon, 04 Mar 2013 10:44:07 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[.net]]></category> <category><![CDATA[asp.net mvc]]></category> <category><![CDATA[c#]]></category> <category><![CDATA[entity framework]]></category> <category><![CDATA[github]]></category> <category><![CDATA[SRP]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=529</guid> <description><![CDATA[<p>I recently had a need to look into using Entity Framework (EF) for a ASP.NET MVC project.  In the past I have always used PetaPoco as my ORM of choice and with hearing nothing but bad things about EF I was a little sceptical.  There are various ways to use EF, Code First being one of them and the easiest from what I can gather and luckily the approach I needed to get up to speed on.  This means you can define your model in code and EF will turn that into tables in your database.</p><p>The way I was going to see how EF could be architected in an application was to create a MVC application that provided CRUD capabilities for Customers, Orders and Products. Nothing complicated but something enough to see how EF could be fitted in with a MVC application.  I would also like to use a unit of work pattern such as instantiate a model class, set some properties and call a save method.  I would also [<a
href="http://blog.jonathanchannon.com/2013/03/04/the-many-approaches-to-entity-framework/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>I recently had a need to look into using <a
href="http://www.asp.net/entity-framework">Entity Framework (EF)</a> for a <a
href="http://www.asp.net/mvc">ASP.NET MVC</a> project.  In the past I have always used <a
href="http://www.toptensoftware.com/petapoco/">PetaPoco</a> as my ORM of choice and with hearing nothing but bad things about EF I was a little sceptical.  There are various ways to use EF, Code First being one of them and the easiest from what I can gather and luckily the approach I needed to get up to speed on.  This means you can define your model in code and EF will turn that into tables in your database.</p><p>The way I was going to see how EF could be architected in an application was to create a MVC application that provided CRUD capabilities for Customers, Orders and Products. Nothing complicated but something enough to see how EF could be fitted in with a MVC application.  I would also like to use a unit of work pattern such as instantiate a model class, set some properties and call a save method.  I would also like to keep the architecture well enough abstracted so that another ORM could take its place easily enough if needs be.</p><p>I will list the various approaches I took investigating the how EF could be integrated. They are not in any chronological order.</p><h2>Approach One</h2><p>My first approach was to create a generic repository, IRepository<T>, to handle the the various crud methods needed and also a Save method.  The implementation, Repository<T> would take a IUnitOfWork constructor dependency which would expose the model type ie.Customer,Order, Product and allow EF to perform the CRUD actions.</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:455px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/>27<br
/>28<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">class</span> Repository<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> <span
style="color: #008000;">:</span> IRepository<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> <span
style="color: #0600FF; font-weight: bold;">where</span> T <span
style="color: #008000;">:</span> <span
style="color: #6666cc; font-weight: bold;">class</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">private</span> <span
style="color: #0600FF; font-weight: bold;">readonly</span> IUnitOfWork unitOfWork<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">private</span> <span
style="color: #0600FF; font-weight: bold;">readonly</span> DbSet<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> entitySet<span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> Repository<span
style="color: #008000;">&#40;</span>IUnitOfWork unitOfWork<span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">unitOfWork</span> <span
style="color: #008000;">=</span> unitOfWork<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; entitySet <span
style="color: #008000;">=</span> unitOfWork<span
style="color: #008000;">.</span><span
style="color: #0000FF;">GetEntitySet</span><span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">void</span> Add<span
style="color: #008000;">&#40;</span>T entity<span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; entitySet<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Add</span><span
style="color: #008000;">&#40;</span>entity<span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <span
style="color: #008000;">&#125;</span><br
/> <br
/> <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">interface</span> IUnitOfWork<br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #6666cc; font-weight: bold;">void</span> SaveChanges<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; DbSet<span
style="color: #008000;">&lt;</span>TEntity<span
style="color: #008000;">&gt;</span> GetEntitySet<span
style="color: #008000;">&lt;</span>TEntity<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span> <span
style="color: #0600FF; font-weight: bold;">where</span> TEntity <span
style="color: #008000;">:</span> <span
style="color: #6666cc; font-weight: bold;">class</span><span
style="color: #008000;">;</span><br
/> <span
style="color: #008000;">&#125;</span><br
/> <br
/> <span
style="color: #0600FF; font-weight: bold;">public</span> OrderController<span
style="color: #008000;">&#40;</span>IRepository<span
style="color: #008000;">&lt;</span>Order<span
style="color: #008000;">&gt;</span> orderRepository, IRepository<span
style="color: #008000;">&lt;</span>Customer<span
style="color: #008000;">&gt;</span> customerRepository <span
style="color: #008000;">&#41;</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">orderRepository</span> <span
style="color: #008000;">=</span> orderRepository<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">customerRepository</span> <span
style="color: #008000;">=</span> customerRepository<span
style="color: #008000;">;</span><br
/> <span
style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>There are two issues with this approach. Firstly, the IUnitOfWork exposes EF specific members which I don&#8217;t really want and secondly when a repository calls Save it will in turn call the SaveChanges method on EF which will save any changes across all repositories.  This is because we have one EF database context per request as handled by our IOC container. To get around the saving issue we could have a new context per instance of the repository but I don&#8217;t think that is a good approach.</p><p>The full code can be seen on the master branch at Github &#8211; <a
href="https://github.com/jchannon/EntityFrameworkMVC/tree/master/EntityFrameworkMVC">https://github.com/jchannon/EntityFrameworkMVC/tree/master/EntityFrameworkMVC</a></p><h2>Approach Two</h2><p>Another approach we could use is to remove IUnitOfWork altogether, keep the Save method on the repository but inject the context into the repository and still inject the repositories into the controllers.</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">class</span> Repository<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> <span
style="color: #008000;">:</span> IRepository<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> <span
style="color: #0600FF; font-weight: bold;">where</span> T <span
style="color: #008000;">:</span> <span
style="color: #6666cc; font-weight: bold;">class</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">private</span> <span
style="color: #0600FF; font-weight: bold;">readonly</span> EntityFrameworkMvcDbContext context<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">private</span> <span
style="color: #0600FF; font-weight: bold;">readonly</span> DbSet<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> entitySet<span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> Repository<span
style="color: #008000;">&#40;</span>EntityFrameworkMvcDbContext context<span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">context</span> <span
style="color: #008000;">=</span> context<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; entitySet <span
style="color: #008000;">=</span> context<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Set</span><span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">void</span> Add<span
style="color: #008000;">&#40;</span>T entity<span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; entitySet<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Add</span><span
style="color: #008000;">&#40;</span>entity<span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <span
style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>This approach is pretty close to what I think we would want and reasonably straight forward but I still don&#8217;t like the fact that the SaveChanges would effect every modified/added model across all repositories.  If you&#8217;re happy to live with that then I think you&#8217;re good to go.</p><p>The full code can be seen on the LessAbstraction branch at Github &#8211; <a
href="https://github.com/jchannon/EntityFrameworkMVC/tree/LessAbstraction/EntityFrameworkMVC">https://github.com/jchannon/EntityFrameworkMVC/tree/LessAbstraction/EntityFrameworkMVC</a></p><h2>Approach Three</h2><p>Another approach for using EF and building on from approach one was to remove the GetEntitySet from the IUnitOfWork interface but keep the Save method, inject the context into the repositories to allow them to add/update and inject the IUnitOfWork and repositories into the controllers so the repositories could use Add and then IUnitOfWork could be used to call SaveChanges.</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:455px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">interface</span> IUnitOfWork<br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #6666cc; font-weight: bold;">void</span> SaveChanges<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> <span
style="color: #008000;">&#125;</span><br
/> <br
/> <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">class</span> Repository<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> <span
style="color: #008000;">:</span> IRepository<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> <span
style="color: #0600FF; font-weight: bold;">where</span> T <span
style="color: #008000;">:</span> <span
style="color: #6666cc; font-weight: bold;">class</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">private</span> <span
style="color: #0600FF; font-weight: bold;">readonly</span> DbSet<span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span> entitySet<span
style="color: #008000;">;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> Repository<span
style="color: #008000;">&#40;</span>EntityFrameworkMvcDbContext context<span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; entitySet <span
style="color: #008000;">=</span> context<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Set</span><span
style="color: #008000;">&lt;</span>T<span
style="color: #008000;">&gt;</span><span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">void</span> Add<span
style="color: #008000;">&#40;</span>T entity<span
style="color: #008000;">&#41;</span><br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; entitySet<span
style="color: #008000;">.</span><span
style="color: #0000FF;">Add</span><span
style="color: #008000;">&#40;</span>entity<span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span> &nbsp; <br
/> &nbsp; &nbsp; <span
style="color: #008000;">&#125;</span><br
/> <span
style="color: #008000;">&#125;</span><br
/> <br
/> <span
style="color: #0600FF; font-weight: bold;">public</span> OrderController<span
style="color: #008000;">&#40;</span>IUnitOfWork unitOfWork, IRepository<span
style="color: #008000;">&lt;</span>Order<span
style="color: #008000;">&gt;</span> orderRepository, IRepository<span
style="color: #008000;">&lt;</span>Customer<span
style="color: #008000;">&gt;</span> customerRepository<span
style="color: #008000;">&#41;</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">unitOfWork</span> <span
style="color: #008000;">=</span> unitOfWork<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">orderRepository</span> <span
style="color: #008000;">=</span> orderRepository<span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">customerRepository</span> <span
style="color: #008000;">=</span> customerRepository<span
style="color: #008000;">;</span><br
/> <span
style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>I think that&#8217;s a decent approach however, I think it could be tidied up so that the controllers don&#8217;t get cluttered with too many constructor dependencies.</p><p>The full code can be seen on the GenericContextPerRequest branch at Github &#8211; <a
href="https://github.com/jchannon/EntityFrameworkMVC/tree/GenericContextPerRequest/EntityFrameworkMVC">https://github.com/jchannon/EntityFrameworkMVC/tree/GenericContextPerRequest/EntityFrameworkMVC</a></p><h2>Approach Four</h2><p>This below was another approach and the approach I decided I would use.  It uses all the understandings gained from the above samples and hopes to provide a decent way of using EF.</p><p>The repositories are now exposed on the IUnitOfWork interface, the implementation takes the DBContext as a constructor dependency, the repository implementation also takes the DBContext to provide the CRUD features with EF and the controllers only have the IUnitOfWork injected into them.</p><div
class="codecolorer-container csharp railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/></div></td><td><div
class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #0600FF; font-weight: bold;">public</span> <span
style="color: #6666cc; font-weight: bold;">interface</span> IUnitOfWork<br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #6666cc; font-weight: bold;">void</span> SaveChanges<span
style="color: #008000;">&#40;</span><span
style="color: #008000;">&#41;</span><span
style="color: #008000;">;</span><br
/> &nbsp; &nbsp; IRepository<span
style="color: #008000;">&lt;</span>Customer<span
style="color: #008000;">&gt;</span> CustomerRepository <span
style="color: #008000;">&#123;</span> get<span
style="color: #008000;">;</span> <span
style="color: #008000;">&#125;</span><br
/> &nbsp; &nbsp; IRepository<span
style="color: #008000;">&lt;</span>Product<span
style="color: #008000;">&gt;</span> ProductRepository <span
style="color: #008000;">&#123;</span> get<span
style="color: #008000;">;</span> <span
style="color: #008000;">&#125;</span><br
/> &nbsp; &nbsp; IRepository<span
style="color: #008000;">&lt;</span>Order<span
style="color: #008000;">&gt;</span> OrderRepository <span
style="color: #008000;">&#123;</span> get<span
style="color: #008000;">;</span> <span
style="color: #008000;">&#125;</span><br
/> <span
style="color: #008000;">&#125;</span><br
/> <br
/> <span
style="color: #0600FF; font-weight: bold;">public</span> OrderController<span
style="color: #008000;">&#40;</span>IUnitOfWork unitOfWork <span
style="color: #008000;">&#41;</span><br
/> <span
style="color: #008000;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #0600FF; font-weight: bold;">this</span><span
style="color: #008000;">.</span><span
style="color: #0000FF;">unitOfWork</span> <span
style="color: #008000;">=</span> unitOfWork<span
style="color: #008000;">;</span><br
/> <span
style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>This means we can keep our one database context per request, the controllers are tidied up so we no longer have numerous constructor dependencies and the repositories can be used to do CRUD and then SaveChanges can be called by IUnitOfWork when we have finished with the repositories.</p><p>The only possible downside could be in time that we have numerous constructor dependencies on the IUnitOfWork implementation as well as numerous exposed repository properties on the IUnitOfWork interface.  I don&#8217;t see this as a big issue as its only one class in your solution.  If you didn&#8217;t want this to get too polluted you could create a repository factory and inject that into the controller and just have the SaveChanges on the IUnitOfWork interface.  You wouldn&#8217;t want the IUnitOfWork to expose the repositories because as <a
href="https://twitter.com/daanleduc">@daanleduc</a> pointed out to me that would violate the SRP.</p><h2>Conclusion</h2><p>I only touched the basics of implementing EF into a MVC app but these approaches did take quite a while to discover and after hearing all the negativity towards EF and drawing from my experience I think I could see why people have issues with it in a much larger application as I&#8217;m sure there are other caveats to using EF.  The biggest issue I saw with it was not exposing a Save method for the model type you were using, instead you have to commit all changes across the database context.  I feel this made it quite difficult to create a decent architecture.  I can see in a large enterprise application why EF could be helpful in allowing it to query the database for you and create/modify the tables for you based on your model changes but I think for now in smaller applications I think I still prefer micro-ORMs like PetaPoco, Dapper or Simple.Data due to the control it gives you and also the ease in which you can abstract the database communication.  As <a
href="https://twitter.com/Cranialstrain">@Cranialstrain</a> says &#8220;the database is an implementation detail&#8221;.  I&#8217;d also like to thank all those on Jabbr that helped me with the above approaches and long discussions had about Entity Framework!</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/03/04/the-many-approaches-to-entity-framework/feed/</wfw:commentRss> <slash:comments>25</slash:comments> </item> <item><title>Evaluating KnockoutJS and AngularJS – Part 2</title><link>http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=evaluating-knockoutjs-and-angularjs-part-2</link> <comments>http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-2/#comments</comments> <pubDate>Tue, 05 Feb 2013 14:39:48 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[backbonejs]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[knockoutjs]]></category> <category><![CDATA[oss]]></category> <category><![CDATA[sammyjs]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=513</guid> <description><![CDATA[<p>In Part 1, I described how I was using the demo tutorial from SammyJS to get a better understanding of AngularJS and KnockoutJS.</p><p>In this blog post I will focus on what I found when using KnockoutJS.</p><p>Again if you just want to get to the code then feel free to take a look here for Angular and here for Knockout.</p> Knockout<p>Firstly Knockout should be commended on their documentation and online tutorials.  Their website tutorials follows a step by step approach and once you have completed each step you can click through to the next section.  You can also leave the tutorial and come back to it and it will remember where you left.  There is great support in the KnockoutJS room in Jabbr and I&#8217;d like to thank David Spörri for answering my newbie JS and Knockout questions.</p><p>Knockout also uses HTML elements and attributes like Angular to determine behaviour but Knockout&#8217;s approach is more HTML5 in that the most common usage is using the data-bind attribute. For example, the below will use the artist [<a
href="http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-2/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>In <a
href="http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-1/" title="Evaluating KnockoutJS and AngularJS – Part 1">Part 1</a>, I described how I was using the demo tutorial from <a
href="http://sammyjs.org/">SammyJS</a> to get a better understanding of <a
href="http://angularjs.org/">AngularJS</a> and <a
href="http://knockoutjs.com/">KnockoutJS</a>.</p><p>In this blog post I will focus on what I found when using KnockoutJS.</p><p>Again if you just want to get to the code then feel free to take a look here for <a
href="https://github.com/jchannon/AngularShopping">Angular</a> and here for <a
href="https://github.com/jchannon/KnockoutShopping">Knockout</a>.</p><h2>Knockout</h2><p>Firstly Knockout should be commended on their documentation and online tutorials.  Their website tutorials follows a step by step approach and once you have completed each step you can click through to the next section.  You can also leave the tutorial and come back to it and it will remember where you left.  There is great support in the KnockoutJS room in Jabbr and I&#8217;d like to thank <a
href="https://twitter.com/davepermen">David Spörri</a> for answering my newbie JS and Knockout questions.</p><p>Knockout also uses HTML elements and attributes like Angular to determine behaviour but Knockout&#8217;s approach is more HTML5 in that the most common usage is using the data-bind attribute. For example, the below will use the artist property on the viewmodel to render the div contents.</p><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a> data-bind<span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;text: artist&quot;</span>&gt;&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></td></tr></tbody></table></div><p>As this was going to be a SPA (Single Page Application), the main screen would have an area that would display either a list of products or more information about a single product.  The way Knockout handles this is via a &#8216;with&#8217; keyword.</p><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a> <span
style="color: #000066;">id</span><span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;main&quot;</span>&gt;</span><br
/> &nbsp; <span
style="color: #808080; font-style: italic;">&lt;!-- Main Page --&gt;</span><br
/> &nbsp; <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a> data-bind<span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;with: items&quot;</span>&gt;&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br
/> &nbsp; <span
style="color: #808080; font-style: italic;">&lt;!-- Detail Page --&gt;</span><br
/> &nbsp; <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a> data-bind<span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;with: chosenProduct&quot;</span>&gt;&lt;<a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br
/> <span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/div.html"><span
style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></td></tr></tbody></table></div><p>So when the viewmodel&#8217;s &#8216;items&#8217; property is populated it would show the first div and when the &#8216;chosenProduct&#8217; property was populated it would show the second div. Nice and simple. You can then populate your markup with properties from the items and chosenProduct within those div&#8217;s.</p><p>As we have 2 pages/sections in our app we need to handle routing so that when we&#8217;re going to the root it populates our view model with all our products and when the user clicks a specific product it finds that product and populates our view model.  KnockoutJS does not have this built into it like Angular does and uses SammyJS to handle this.</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #003366; font-weight: bold;">var</span> app <span
style="color: #339933;">=</span> Sammy<span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">this</span>.<span
style="color: #660066;">get</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'#/:id'</span><span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>context<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; self.<span
style="color: #660066;">items</span><span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">null</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #003366; font-weight: bold;">var</span> product <span
style="color: #339933;">=</span> data.<span
style="color: #660066;">filter</span><span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>x<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">return</span> x.<span
style="color: #660066;">id</span> <span
style="color: #339933;">===</span> parseInt<span
style="color: #009900;">&#40;</span>context.<span
style="color: #660066;">params</span>.<span
style="color: #660066;">id</span><span
style="color: #339933;">,</span> <span
style="color: #CC0000;">10</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#91;</span><span
style="color: #CC0000;">0</span><span
style="color: #009900;">&#93;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; product.<span
style="color: #660066;">quantity</span> <span
style="color: #339933;">=</span> <span
style="color: #CC0000;">1</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; self.<span
style="color: #660066;">chosenProduct</span><span
style="color: #009900;">&#40;</span>product<span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">this</span>.<span
style="color: #660066;">get</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'/'</span><span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; self.<span
style="color: #660066;">items</span><span
style="color: #009900;">&#40;</span>data<span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; self.<span
style="color: #660066;">chosenProduct</span><span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">null</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> jQuery<span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; console.<span
style="color: #660066;">log</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'rn'</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; app.<span
style="color: #660066;">run</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></td></tr></tbody></table></div><p>Compared to Angular&#8217;s separation approach, here we have most things all in one class/viewmodel i.e/ the routing to assign the items property for all products and single item for the selected product as well as other viewmodel properties.  We also have functions to add items and determine quantity as well as saving the information to <a
href="https://developer.mozilla.org/en-US/docs/DOM/Storage">localStorage</a>. I guess it keeps it altogether in one place but this may get tricky once the app becomes quite large however, I&#8217;ve been told things can get separated by using <a
href="http://requirejs.org/">RequireJS</a> although I think in this demo that would require a large refactor.</p><p>Again in this demo outside of the main area that gets changed with markup we have a shopping cart that needs to update when we add something. I already had a function that calculated my quantities but it did not update the div with the new value.  Even though the viewmodel property was a computed function it was not re-run every time something was added to the basket, for that to happen the computed function needs to have an observable property within it.</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;self.<span
style="color: #660066;">basketItemCount</span> <span
style="color: #339933;">=</span> ko.<span
style="color: #660066;">computed</span><span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #003366; font-weight: bold;">var</span> count <span
style="color: #339933;">=</span> <span
style="color: #CC0000;">0</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #003366; font-weight: bold;">var</span> items <span
style="color: #339933;">=</span> self.<span
style="color: #660066;">basketItems</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">for</span> <span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">var</span> i <span
style="color: #339933;">=</span> <span
style="color: #CC0000;">0</span><span
style="color: #339933;">;</span> i <span
style="color: #339933;">&lt;</span> items.<span
style="color: #660066;">length</span><span
style="color: #339933;">;</span> i<span
style="color: #339933;">++</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count <span
style="color: #339933;">+=</span> items<span
style="color: #009900;">&#91;</span>i<span
style="color: #009900;">&#93;</span>.<span
style="color: #660066;">quantity</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span> <span
style="color: #006600; font-style: italic;">//Given that the quantity is an observable property</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">return</span> count<span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></td></tr></tbody></table></div><p>We also wanted to do some animation when the item was added to the basket and because SammyJS has a dependency on jQuery it was very easy to add this to the viewmodel&#8217;s addItem function however like Angular this is not recommended as it ties the view to the viewmodel.  What Knockout recommend is custom bindings.</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ko.<span
style="color: #660066;">bindingHandlers</span>.<span
style="color: #660066;">animateCart</span> <span
style="color: #339933;">=</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; update<span
style="color: #339933;">:</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>element<span
style="color: #339933;">,</span> valueAccessor<span
style="color: #339933;">,</span> allBindingsAccessor<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; $<span
style="color: #009900;">&#40;</span>element<span
style="color: #009900;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span
style="color: #660066;">animate</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#123;</span> paddingTop<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'30px'</span> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span
style="color: #660066;">animate</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#123;</span> paddingTop<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'10px'</span> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><br
/> <span
style="color: #009900;">&#125;</span><span
style="color: #339933;">;</span><br
/> <br
/> <span
style="color: #006600; font-style: italic;">//Usage</span><br
/> <span
style="color: #339933;">&lt;</span>div <span
style="color: #003366; font-weight: bold;">class</span><span
style="color: #339933;">=</span><span
style="color: #3366CC;">&quot;cart-info&quot;</span> data<span
style="color: #339933;">-</span>bind<span
style="color: #339933;">=</span><span
style="color: #3366CC;">&quot;animateCart: basketAsJson()&quot;</span><span
style="color: #339933;">&gt;&lt;/</span>div<span
style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div><p>Now this is where the data binding gets confusing/interesting.  My HTML could not use the basketItems viewmodel property as this is an observable array and so would only react when the array changed NOT when properties of objects within the array changed. I was a bit disappointed with that so I had to create a new function that obviously used an observeable within it ie/the basketItems but that was not enough to handle the properties changing within that array.  So the recommendation from <a
href="https://twitter.com/robwesterlund">Robert Westerlund</a> (a Javascript, Knockout and Regex wizard) was to use ko.toJSON(basketItems()).  (The reason we use ko.toJSON and not JSON.stringify is because that would not pick up the observeable property values)   This would mean that it would have to traverse all the objects to find the values and because they are observeable this function would fire every time, genius! This also meant when it changed I could also store the changes in localStorage for when the user came back to the website!</p><h2>Conclusion</h2><p>I found Knockout easier to comprehend and get going with compared to Angular which is always a winner when starting something new but Knockout is pretty much just about binding so for things like routing and separation of concerns you need to use other libraries whereas Angular has all that built in. I think for smaller projects without too much logic required Knockout is ideal and I really like the simplicity of SammyJS however, for larger applications that requires dependency injection, a clear separation of concerns and the easy ability to unit test logic Angular is a winner.  In this case the term &#8220;pick the right tool for the job&#8221; certainly applies.</p><p>I guess I should go ahead and investigate Backbone now just to fully educate myself and I&#8217;ve been recommend this link &#8211; <a
href="https://github.com/kjbekkelund/writings/blob/master/published/understanding-backbone.md">&#8220;Step by step from jQuery to Backbone&#8221;</a> so here goes&#8230;</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-2/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Evaluating KnockoutJS and AngularJS &#8211; Part 1</title><link>http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-1/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=evaluating-knockoutjs-and-angularjs-part-1</link> <comments>http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-1/#comments</comments> <pubDate>Tue, 05 Feb 2013 09:06:13 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[angularjs]]></category> <category><![CDATA[handlebars]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[knockoutjs]]></category> <category><![CDATA[learning]]></category> <category><![CDATA[oss]]></category> <category><![CDATA[sammyjs]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=485</guid> <description><![CDATA[<p>As I stated in my earlier post &#8220;JavaScript is the future&#8230;maybe&#8221; so with that in mind I had to brush up my JS skills and get more involved with the language&#8217;s core concepts so after watching some videos and reading some articles I was ready to look at KnockoutJS and AngularJS.</p><p>Before I actually looked at these two I spent some time with SammyJS but realised afterwards it was mainly MVC based and not around 2-way binding that Knockout and Angular offer.  However, I really liked it and it seemed very familiar and easy to use, the reason being it was inspired by Sinatra which we all know Nancy was also inspired by and we also know how much I like Nancy!</p><p>Getting to grips with any language or frameworks is tricky and the best way to do it is to write an application using it.  The next difficult thing to overcome is an idea for writing an application.  ToDo list&#8217;s are very common with JavaScript frameworks and there is a whole website for [<a
href="http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-1/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>As I stated in my earlier post <a
href="http://blog.jonathanchannon.com/2013/01/09/javascript-is-the-future-maybe/" title="JavaScript is the future…maybe!">&#8220;JavaScript is the future&#8230;maybe&#8221;</a> so with that in mind I had to brush up my JS skills and get more involved with the language&#8217;s core concepts so after watching some videos and reading some articles I was ready to look at <a
href="http://knockoutjs.com">KnockoutJS</a> and <a
href="http://angularjs.org/">AngularJS</a>.</p><p>Before I actually looked at these two I spent some time with <a
href="http://sammyjs.org/">SammyJS</a> but realised afterwards it was mainly MVC based and not around 2-way binding that Knockout and Angular offer.  However, I really liked it and it seemed very familiar and easy to use, the reason being it was inspired by <a
href="http://www.sinatrarb.com/">Sinatra</a> which we all know <a
href="http://nancyfx.org/">Nancy</a> was also inspired by and we also know how much <a
href="http://nancyfx.org/mvm.html">I like Nancy</a>!</p><p>Getting to grips with any language or frameworks is tricky and the best way to do it is to write an application using it.  The next difficult thing to overcome is an idea for writing an application.  ToDo list&#8217;s are very common with JavaScript frameworks and there is a whole website for you to peruse but after looking at SammyJS and their docs they walk through writing a simple shopping basket so I thought I&#8217;d use that.</p><p>If you just want to get to the code then feel free to take a look here for <a
href="https://github.com/jchannon/AngularShopping">Angular</a> and here for <a
href="https://github.com/jchannon/KnockoutShopping">Knockout</a>.</p><p>This blog post will concentrate on AngularJS.</p><h2>Angular</h2><p>After looking at the demos on the the home page and reading the docs I was ready to start. (I also got recommended <a
href="http://egghead.io/">egghead.io</a> for lots of free AngularJS videos)</p><p>I liked the look of Angular&#8217;s data bindings, it was very similar to <a
href="http://handlebarsjs.com/">HandlebarsJS</a> which is a templating engine for JS and their specific attributes for doing certain things in the DOM seemed clean.</p><div
class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/></div></td><td><div
class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/input.html"><span
style="color: #000000; font-weight: bold;">input</span></a> <span
style="color: #000066;">type</span><span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;text&quot;</span> ng-model<span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;yourName&quot;</span> placeholder<span
style="color: #66cc66;">=</span><span
style="color: #ff0000;">&quot;Enter a name here&quot;</span>&gt;</span><br
/> <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/hr.html"><span
style="color: #000000; font-weight: bold;">hr</span></a>&gt;</span><br
/> <span
style="color: #009900;">&lt;<a
href="http://december.com/html/4/element/h1.html"><span
style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span>Hello {{yourName}}!<span
style="color: #009900;">&lt;<span
style="color: #66cc66;">/</span><a
href="http://december.com/html/4/element/h1.html"><span
style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span></div></td></tr></tbody></table></div><p>Angular uses &#8220;Controllers&#8221; and coming from experience with MVC it turned out they&#8217;re not really controllers but more ViewModels.  Each Controller handles its own specific task for example, in CRUD you&#8217;d probably have a controller for each create, read, update and delete. I believe its possible to make it have one controller but that&#8217;s a bit more advanced!</p><p>The data in the SammyJS demo comes via a data.json file so I duly created my file and followed the examples of wiring up a backend. This is when things start getting a bit more in depth with Angular opposed to a simple Hello World app.  Angular provides its own internal IOC container which is recommended you use to keep things nicely separated and of course it then injects your controller&#8217;s dependencies for you.  See <a
href="http://docs.angularjs.org/guide/di">here</a> for more about the subject.  There is a bit of configuration needed in various places and one missing item or spelling mistake and you&#8217;ll be stuck due to the nature of dynamic nature of JavaScript.</p><p>I finally managed to get my wirings all sorted and my products displayed on screen but when I wanted to retrieve a single item this is when I started scratching my head.  The recommended way of doing it was to use an abstracted class called $resource which allows you to query REST based services and obviously with just having one file this wasn&#8217;t going to work.  $resource sits on top of $http but after playing with that I discarded my file and stuck it in memory for ease. If you&#8217;d like to see a $http demo with Nancy as a REST service see <a
href="http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/Javascript/angularjs">this blog post</a> from <a
href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&#038;author=7">Christiaan Baes</a></p><p>So now with a bit of filtering I could find my product and display it on screen. I had a controller for listing my products and another controller for displaying one item and a dependency injected into both to allow me to retrieve the data I wanted to display and I also had my routing setup.</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:455px;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/>21<br
/>22<br
/>23<br
/>24<br
/>25<br
/>26<br
/>27<br
/>28<br
/>29<br
/>30<br
/>31<br
/>32<br
/>33<br
/>34<br
/>35<br
/>36<br
/>37<br
/>38<br
/>39<br
/>40<br
/>41<br
/>42<br
/>43<br
/>44<br
/>45<br
/>46<br
/>47<br
/>48<br
/>49<br
/>50<br
/>51<br
/>52<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span
style="color: #006600; font-style: italic;">//Routing</span><br
/> <span
style="color: #003366; font-weight: bold;">var</span> app <span
style="color: #339933;">=</span> angular.<span
style="color: #660066;">module</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'myCart'</span><span
style="color: #339933;">,</span> <span
style="color: #009900;">&#91;</span><span
style="color: #009900;">&#93;</span><span
style="color: #009900;">&#41;</span>.<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #660066;">config</span><span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>$routeProvider<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $routeProvider.<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #660066;">when</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'/'</span><span
style="color: #339933;">,</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'IndexController'</span><span
style="color: #339933;">,</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; templateUrl<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'/templates/list.html'</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span>.<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #660066;">when</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'/:id'</span><span
style="color: #339933;">,</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'DetailController'</span><span
style="color: #339933;">,</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; templateUrl<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'/templates/detail.html'</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span>.<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #660066;">otherwise</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#123;</span> redirectTo<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'/'</span> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> <span
style="color: #006600; font-style: italic;">//Repository</span><br
/> app.<span
style="color: #660066;">factory</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'shoppingItemsService'</span><span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #003366; font-weight: bold;">var</span> data <span
style="color: #339933;">=</span> <span
style="color: #009900;">&#91;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #006600; font-style: italic;">//My data is here!</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#93;</span><span
style="color: #339933;">;</span><br
/> <br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">return</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getItems<span
style="color: #339933;">:</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span> <span
style="color: #000066; font-weight: bold;">return</span> data<span
style="color: #339933;">;</span> <span
style="color: #009900;">&#125;</span><span
style="color: #339933;">,</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getItem<span
style="color: #339933;">:</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>itemId<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">return</span> data.<span
style="color: #660066;">filter</span><span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>x<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span> <span
style="color: #000066; font-weight: bold;">return</span> x.<span
style="color: #660066;">id</span> <span
style="color: #339933;">===</span> parseInt<span
style="color: #009900;">&#40;</span>itemId<span
style="color: #339933;">,</span> <span
style="color: #CC0000;">10</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#91;</span><span
style="color: #CC0000;">0</span><span
style="color: #009900;">&#93;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> <span
style="color: #006600; font-style: italic;">//List Controller</span><br
/> app.<span
style="color: #660066;">controller</span><span
style="color: #009900;">&#40;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #3366CC;">'IndexController'</span><span
style="color: #339933;">,</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>$scope<span
style="color: #339933;">,</span> shoppingItemsService<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.<span
style="color: #660066;">items</span> <span
style="color: #339933;">=</span> shoppingItemsService.<span
style="color: #660066;">getItems</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> <span
style="color: #006600; font-style: italic;">//Detail Controller</span><br
/> app.<span
style="color: #660066;">controller</span><span
style="color: #009900;">&#40;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #3366CC;">'DetailController'</span><span
style="color: #339933;">,</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>$scope<span
style="color: #339933;">,</span> $routeParams<span
style="color: #339933;">,</span> basketService<span
style="color: #339933;">,</span> shoppingItemsService<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> <br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.<span
style="color: #000066; font-weight: bold;">item</span> <span
style="color: #339933;">=</span> shoppingItemsService.<span
style="color: #660066;">getItem</span><span
style="color: #009900;">&#40;</span>$routeParams.<span
style="color: #660066;">id</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.<span
style="color: #000066; font-weight: bold;">item</span>.<span
style="color: #660066;">quantity</span> <span
style="color: #339933;">=</span> <span
style="color: #CC0000;">1</span><span
style="color: #339933;">;</span><br
/> <br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.<span
style="color: #000066; font-weight: bold;">item</span>.<span
style="color: #660066;">basketCount</span> <span
style="color: #339933;">=</span> basketService.<span
style="color: #660066;">getCount</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.<span
style="color: #660066;">addItem</span> <span
style="color: #339933;">=</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; basketService.<span
style="color: #660066;">addItem</span><span
style="color: #009900;">&#40;</span>$scope.<span
style="color: #000066; font-weight: bold;">item</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></td></tr></tbody></table></div><p>As the app is based around a single page application (SPA) the main area on the screen would show the products or product selected however I had an area, the cart info, that showed the quantity of items in my basket outside this main area.  After a lot of head scratching I realised I could create a new controller that was responsible for setting a viewmodel property of the quantity and only update that specific area on screen.  This meant when I was viewing a product and added a certain quantity to the basket this area would update automatically. Finally in the demo for a bit of added pizazz the cart info area animates.</p><p>This is the one bit that took me a long time to get my head around. Angular recommends that no DOM manipulation occurs in controllers but in separate classes/functions called Directives.  I understand why, to keep your viewmodel not dependent on your view but man it was hard work!  Directives allow you to determine your own HTML elements that Angular understands, again you can pass in dependencies and they will get resolved for you. Put whatever logic you want in your directive and then manipulate the DOM.</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/>7<br
/>8<br
/>9<br
/>10<br
/>11<br
/>12<br
/>13<br
/>14<br
/>15<br
/>16<br
/>17<br
/>18<br
/>19<br
/>20<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">app.<span
style="color: #660066;">directive</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'quantity'</span><span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #003366; font-weight: bold;">var</span> linker <span
style="color: #339933;">=</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>scope<span
style="color: #339933;">,</span> element<span
style="color: #339933;">,</span> attrs<span
style="color: #339933;">,</span> basketService<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope.$watch<span
style="color: #009900;">&#40;</span>attrs.<span
style="color: #660066;">quantity</span><span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">function</span> <span
style="color: #009900;">&#40;</span>value<span
style="color: #339933;">,</span> oldValue<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">if</span> <span
style="color: #009900;">&#40;</span>value <span
style="color: #339933;">&gt;</span> oldValue<span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element<br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span
style="color: #660066;">animate</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#123;</span> paddingTop<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'30px'</span> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span
style="color: #660066;">animate</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#123;</span> paddingTop<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'10px'</span> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #339933;">,</span> <span
style="color: #003366; font-weight: bold;">true</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #339933;">;</span><br
/> <br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000066; font-weight: bold;">return</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restrict<span
style="color: #339933;">:</span> <span
style="color: #3366CC;">'A'</span><span
style="color: #339933;">,</span><br
/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link<span
style="color: #339933;">:</span> linker<br
/> &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #339933;">;</span><br
/> &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span><br
/> <br
/> <span
style="color: #006600; font-style: italic;">//Usage</span><br
/> <span
style="color: #339933;">&lt;</span>div <span
style="color: #003366; font-weight: bold;">class</span><span
style="color: #339933;">=</span><span
style="color: #3366CC;">&quot;cart-info&quot;</span> ng<span
style="color: #339933;">-</span>controller<span
style="color: #339933;">=</span><span
style="color: #3366CC;">&quot;CartController&quot;</span> quantity<span
style="color: #339933;">=</span><span
style="color: #3366CC;">&quot;basketCount()&quot;</span><span
style="color: #339933;">&gt;&lt;/</span>div<span
style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div><p>One of the biggest things I found with Angular was that it was quite fiddly to setup however the documentation was very good and I had help from <a
href="http://twitter.com/philjones88">Phil Jones</a>.  Once I had solved whatever issue I was having at the time it was glaringly obvious what was needed and I why I was having problems but that&#8217;s all part of the learning curve I guess but mighty frustrating.  Angular is certainly all encompassing by handling routing, model binding, separation of concerns and much more and so for a large app I can certainly see its potential but for small apps it may be a bit over the top but overall I did like it.  There are also library extensions such as <a
href="http://angular-ui.github.com/">AngularUI</a> which provide custom directives for DOM manipulation, ranked <a
href="http://ngmodules.org/">popular modules</a> and specific <a
href="https://github.com/angular/angularjs-batarang">Angular debuggers</a> so this framework certainly has a lot behind it and I definitely think it will be sticking around for a while.</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/02/05/evaluating-knockoutjs-and-angularjs-part-1/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Using AngularJS/BackboneJS in Windows 8 JavaScript app</title><link>http://blog.jonathanchannon.com/2013/01/24/using-angularjsbackbonejs-in-windows-8-javascript-app/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-angularjsbackbonejs-in-windows-8-javascript-app</link> <comments>http://blog.jonathanchannon.com/2013/01/24/using-angularjsbackbonejs-in-windows-8-javascript-app/#comments</comments> <pubDate>Thu, 24 Jan 2013 16:19:09 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[angularjs]]></category> <category><![CDATA[backbonejs]]></category> <category><![CDATA[github]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[knockoutjs]]></category> <category><![CDATA[oss]]></category> <category><![CDATA[windows 8]]></category> <category><![CDATA[winjs]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=467</guid> <description><![CDATA[<p>To help me expand my JavaScript knowledge as I said I would in my previous post I thought I&#8217;d write a Windows 8 application using JavaScript.</p><p>After following a few &#8220;Hello World&#8221; tutorials from Microsoft I thought I&#8217;d take a look at the ToDo list demos shown at TodoMVC.com.</p><p>This website/Github repository takes the ToDo demo and implements it in all the various JS frameworks and libraries out there.  As I said previously its a minefield.</p><p>Anyhow, I thought I&#8217;d start with Backbone, copy the files, add the references to WinJS and hit F5 and bingo.  However, I got the below error:</p><p><img
alt="Unhandled Exception" src="http://i.stack.imgur.com/DOQl1.png" title="Unhandled Exception" class="aligncenter" width="1561" height="843" /></p><p>A bit confused, I googled the issue and found that Microsoft have implemented security principles in WinJS applications to prevent un-sanitized markup to your page.  So any time you might dynamically add some HTML to your page your application will throw an exception.</p><p>To get around this issue you can wrap your dynamic content calls with <em>execUnsafeLocalFunction</em></p><p>You can read more about that from Microsoft&#8217;s documentation here</p><p>Here [<a
href="http://blog.jonathanchannon.com/2013/01/24/using-angularjsbackbonejs-in-windows-8-javascript-app/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>To help me expand my JavaScript knowledge as I said I would in my <a
href="http://blog.jonathanchannon.com/2013/01/09/javascript-is-the-future-maybe/" title="JavaScript is the future…maybe!">previous post</a> I thought I&#8217;d write a Windows 8 application using JavaScript.</p><p>After following a few &#8220;Hello World&#8221; tutorials from Microsoft I thought I&#8217;d take a look at the ToDo list demos shown at <a
href="http://TodoMVC.com">TodoMVC.com</a>.</p><p>This website/Github repository takes the ToDo demo and implements it in all the various JS frameworks and libraries out there.  As I said previously its a minefield.</p><p>Anyhow, I thought I&#8217;d start with Backbone, copy the files, add the references to WinJS and hit F5 and bingo.  However, I got the below error:</p><p><a
href="http://i.stack.imgur.com/DOQl1.png"><img
alt="Unhandled Exception" src="http://i.stack.imgur.com/DOQl1.png" title="Unhandled Exception" class="aligncenter" width="1561" height="843" /></a></p><p>A bit confused, I googled the issue and found that Microsoft have implemented security principles in WinJS applications to prevent un-sanitized markup to your page.  So any time you might dynamically add some HTML to your page your application will throw an exception.</p><p>To get around this issue you can wrap your dynamic content calls with <em>execUnsafeLocalFunction</em></p><p>You can read more about that from Microsoft&#8217;s documentation <a
href="http://msdn.microsoft.com/en-gb/library/windows/apps/hh767331.aspx">here</a></p><p>Here is how you would execute the function around dynamically adding HTML:</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MSApp.<span
style="color: #660066;">execUnsafeLocalFunction</span><span
style="color: #009900;">&#40;</span><span
style="color: #003366; font-weight: bold;">function</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span><br
/> &nbsp; <span
style="color: #003366; font-weight: bold;">var</span> body <span
style="color: #339933;">=</span> document.<span
style="color: #660066;">getElementsByTagName</span><span
style="color: #009900;">&#40;</span><span
style="color: #3366CC;">'body'</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#91;</span><span
style="color: #CC0000;">0</span><span
style="color: #009900;">&#93;</span><span
style="color: #339933;">;</span><br
/> &nbsp; body.<span
style="color: #660066;">innerHTML</span> <span
style="color: #339933;">=</span> <span
style="color: #3366CC;">'&lt;div style=&quot;color:'</span> <span
style="color: #339933;">+</span> textColor <span
style="color: #339933;">+</span> <span
style="color: #3366CC;">'&quot;&gt;example&lt;/div&gt;'</span><span
style="color: #339933;">;</span><br
/> <span
style="color: #009900;">&#125;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></td></tr></tbody></table></div><p>I moved onto Angular to see if that would help but again I hit the same bug.  I did find in the Angular code that if you have a reference to jQuery in your page it would use that, so I downloaded the latest version, referenced it and still the same issue.</p><p>What I didn&#8217;t realise is that Backbone and Angular both ship jQuery or a subset of it within them and I didn&#8217;t fancy modifying those libraries.</p><p>I found <a
href="https://github.com/appendto/jquery-win8">jquery-win8</a> from appendTo which I thought was the answer so referenced that and still the same issue.</p><p>I then gave KnockoutJS a go and it worked perfectly, the reason being no dependency on jQuery.</p><p>I put a question on StackOverflow and after a couple of days <a
href="https://twitter.com/elijahmanor">Elijah Manor</a> of AppendTo answered me.</p><blockquote
style="width:90%"><p> appendTo&#8217;s version removes errors that occur when running jQuery at load time. You still may have code that violates the security model Microsoft put in place. Microsoft is trying to make you aware that there is a risk adding un-sanitized markup to your page.</p><p>If you are confident that is not the case you can try setting jQuery.isUnsafe to true after the appendTo library is included. That should wrap all possible unsafe calls with MSApp.execUnsafeLocalFunction so that Microsoft doesn&#8217;t complain.</p><p>Note: This flag is turned off by default</p></blockquote><div
style="clear:both"></div><p>So I setup the Angular version of the TodoMVC demo in my Win8 application, referenced the WinJS libs, jquery-win8 and set jQuery.isUnsafe to true and the app started perfectly and all functionality was present. Yay!</p><div
class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table
cellspacing="0" cellpadding="0"><tbody><tr><td
style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br
/>2<br
/>3<br
/>4<br
/>5<br
/>6<br
/></div></td><td><div
class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &lt;script src=&quot;//Microsoft.WinJS.1.0/js/base.js&quot;&gt;&lt;/script&gt;<br
/> &nbsp; &lt;script src=&quot;/js/default.js&quot;&gt;&lt;/script&gt;<br
/> &nbsp; &lt;script src=&quot;js/jquery-1.8.2-win8-1.0.min.js&quot;&gt;&lt;/script&gt;<br
/> &nbsp; <span
style="color: #339933;">&lt;</span>script type<span
style="color: #339933;">=</span><span
style="color: #3366CC;">&quot;text/javascript&quot;</span><span
style="color: #339933;">&gt;</span><br
/> &nbsp; &nbsp; &nbsp; jQuery.<span
style="color: #660066;">isUnsafe</span> <span
style="color: #339933;">=</span> <span
style="color: #003366; font-weight: bold;">true</span><span
style="color: #339933;">;</span><br
/> &nbsp; <span
style="color: #339933;">&lt;/</span>script<span
style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div><p>One thing to note is that jquery-win8 runs off of jQuery 1.8.2 but appendTo are working on a 1.9 version.</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/01/24/using-angularjsbackbonejs-in-windows-8-javascript-app/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>JavaScript is the future&#8230;maybe!</title><link>http://blog.jonathanchannon.com/2013/01/09/javascript-is-the-future-maybe/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=javascript-is-the-future-maybe</link> <comments>http://blog.jonathanchannon.com/2013/01/09/javascript-is-the-future-maybe/#comments</comments> <pubDate>Wed, 09 Jan 2013 11:28:40 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[.net]]></category> <category><![CDATA[angularjs]]></category> <category><![CDATA[backbonejs]]></category> <category><![CDATA[c#]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[knockoutjs]]></category> <category><![CDATA[learning]]></category> <category><![CDATA[node.js]]></category> <category><![CDATA[oss]]></category> <category><![CDATA[typescript]]></category> <guid
isPermaLink="false">http://blog.jonathanchannon.com/?p=440</guid> <description><![CDATA[<p>I&#8217;m not one for New Years resolutions but I thought it was time I looked at JavaScript more in depth.</p><p>I looked at Node.js a while back and found it very interesting and I probably need to go back to it.  Over the last month or so there has been a large discussion about async in .Net frameworks and there appears to be a lot of misunderstanding about it (and lets leave it at that, I don&#8217;t want to start another flame war) but the thing we can definitely say with Node.js, well JavaScript to be fair is that it is perfectly asynchronous and non-blocking.</p><p>As a web developer I have used JavaScript from the early days of Response.Write moving onto frameworks such as script.aculo.us and MooTools and finally ending up with jQuery which has come pretty much a standard these days so my JavaScript skills are not completely new.</p><p>However, there has been a large push to use JS more and more for rich user friendly applications with things like KnockoutJS, AngularJS and BackboneJS on the [<a
href="http://blog.jonathanchannon.com/2013/01/09/javascript-is-the-future-maybe/"">...Read More...</a>]]]></description> <content:encoded><![CDATA[<p>I&#8217;m not one for New Years resolutions but I thought it was time I looked at JavaScript more in depth.</p><p>I looked at <a
href="http://blog.jonathanchannon.com/2012/10/08/node-js-express-hello-world-formula-1-style/" title="Node.js, Express, Hello World Formula 1 Style">Node.js a while back</a> and found it very interesting and I probably need to go back to it.  Over the last month or so there has been a large discussion about async in .Net frameworks and there appears to be a lot of misunderstanding about it (and lets leave it at that, I don&#8217;t want to start another flame war) but the thing we can definitely say with <a
href="http://nodejs.org/">Node.js</a>, well JavaScript to be fair is that it is perfectly asynchronous and non-blocking.</p><p>As a web developer I have used JavaScript from the early days of Response.Write moving onto frameworks such as <a
href="http://script.aculo.us/">script.aculo.us</a> and <a
href="http://mootools.net/">MooTools</a> and finally ending up with <a
href="http://jquery.com/">jQuery</a> which has come pretty much a standard these days so my JavaScript skills are not completely new.</p><p>However, there has been a large push to use JS more and more for rich user friendly applications with things like <a
href="http://knockoutjs.com/">KnockoutJS</a>, <a
href="http://angularjs.org/">AngularJS</a> and <a
href="http://backbonejs.org/">BackboneJS</a> on the client and Node.js on the server.  Microsoft has even taken a prominent role in helping bring Node.js to a Windows environment as it started out on *nix based platforms.  They have also started contributing to and including scripts in their Visual Studio project templates for jQuery.</p><p>JavaScript can now promote one language to develop on the server and the client which makes things easier and that is one argument I understand and I&#8217;m sure there are many others which either I have forgotten or I&#8217;m unaware of but the truth is JavaScript seems to be picking up pace in the development world.</p><p>We now have things like <a
href="http://coffeescript.org/">CoffeeScript</a> that abstracts JS and allows users to type arguably neater code which then compiles into JS.</p><p>The big news is that Microsoft have developed <a
href="http://www.typescriptlang.org/">Typescript</a> which is an attempt to coerce or make the transition easier for C# developers to use JavaScript.  Their mantra for TypeScript is</p><blockquote><p>TypeScript is a language for application-scale JavaScript development. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open Source.</p></blockquote><p>With this development along with the fact that Windows 8 applications can be written in C#/XAML <strong>or</strong> JavaScript/HTML proves that MS and therefore probably the mainstream of developers are taking JS as a serious platform to start writing large production systems.</p><p>I am off to a <a
href="http://www.dotnetdevnet.com/Meetings/tabid/54/EntryID/73/Default.aspx">talk</a> about TypeScript with <a
href="http://twitter.com/markrendle">@markrendle</a> on Jan 22nd so it&#8217;ll be interesting to see the language and the points put forward about it and JS as a whole.</p><p>As I mentioned earlier there is a large focus on rich content applications which use JS to make the application quick and easy to use and frameworks like KnockoutJS, AngularJS and Backbone have popped up allowing you to create these types of applications.</p><p>My biggest concern is which one do you use? I&#8217;ve had recommendations for each one which hasn&#8217;t helped. KnockoutJS is developed by <a
href="http://twitter.com/stevensanderson">@stevensanderson</a> who works for MS so you could say it might be best to use that as it maybe more main stream and a more common requirement for employers to see you know it.  I&#8217;ve heard arguments that AngularJS &#038; BackBone provide the ability to write a more larger scale JS application where Knockout only provides JS type data binding and validation so again this is beneficial.  I also discussed with someone that they and their company evaluated all of them and decided that the learning curve was too high for each and they went with jQuery and various plugins and <a
href="http://mustache.github.com/">Mustache</a> for binding scenarios.</p><p>Obviously there are arguments that you should use a library/framework that supplies the solution to your requirements but generally developers want to learn something that will have long lasting benefits to them, yes they learn things for the sake of it but mostly it will benefit them in the long run, for example, you don&#8217;t hear of much use for the <a
href="http://compsoc.dur.ac.uk/whitespace/">Whitespace</a> programming language do you? So whilst there is an obvious push for more intensive use of JavaScript there still seems to be some sort of barrier to entry in deciding upon the correct path to take. Maybe over time the strongest will survive but whilst you&#8217;re waiting 1-2 years for something to mature you&#8217;re possibly losing business.</p> ]]></content:encoded> <wfw:commentRss>http://blog.jonathanchannon.com/2013/01/09/javascript-is-the-future-maybe/feed/</wfw:commentRss> <slash:comments>24</slash:comments> </item> </channel> </rss>