<?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>Rob Searles</title>
	<atom:link href="http://www.robsearles.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robsearles.com</link>
	<description>Struggles of a self-taught programmer</description>
	<lastBuildDate>Tue, 21 May 2013 20:59:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Backbone Reactive – Adding in Events</title>
		<link>http://www.robsearles.com/2013/05/adding-in-events/</link>
		<comments>http://www.robsearles.com/2013/05/adding-in-events/#comments</comments>
		<pubDate>Tue, 21 May 2013 13:18:29 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Backbone Reactive]]></category>
		<category><![CDATA[backbone]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[reactive]]></category>
		<category><![CDATA[requirejs]]></category>
		<category><![CDATA[zepto]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=879</guid>
		<description><![CDATA[So far our simple Backbone application doesn't actually do anything. We will change this with Events.]]></description>
				<content:encoded><![CDATA[<div class="alert-help">
This is Part Three of the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Backbone Reactive Tutorial</a>. For all sections, please view the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Table of Contents</a>.
</div>
<p>So far the List page doesn&#8217;t do anything apart from list the messages we have. This means that it is <strong>less</strong> functional than the <a href="http://www.robsearles.com/2013/05/backbone-reactive-fallback-version/">static html fallback site</a> &#8211; at least with that one you could view the message contents. So in this post we are going to build on <a href="http://www.robsearles.com/2013/05/overlay-a-simple-backbonejs-application/">our simple Backbone app</a>, adding in events so you can actually view the messages.</p>
<p>Luckily adding <a href="http://backbonejs.org/#Events" target="_blank">Events in Backbone</a> is simple. Within the <code>ListRowView</code> all you have to do is add:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  events<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;open&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  open<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;clicked: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>Now when you reload the page and click on a row you should see the console output the ID of the model you clicked on. So now we know we can click on a row, we need to load in the data and display it.</p>
<div class="alert-info">
Note: You can find all the source code to this project on <a href="https://github.com/robsearles/backbone-reactive/" target="_blank">GitHub</a>, this chapter has been <a href="https://github.com/robsearles/backbone-reactive/tree/ch3-events" target="_blank">tagged ch3-events</a>
</div>
<p>Let&#8217;s tackle this in reverse order and create a new view to display the message contents, we&#8217;ll call it <code>MessageView</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> MessageView <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'message'</span><span style="color: #339933;">,</span>
&nbsp;
  render<span style="color: #339933;">:</span> <span style="color: #000066; 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;">var</span> date <span style="color: #339933;">=</span> DateFunctions.<span style="color: #660066;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'date'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> dateNice <span style="color: #339933;">=</span> DateFunctions.<span style="color: #660066;">getDateAndTimeNice</span><span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;p&gt;&lt;label&gt;From&lt;/label&gt; '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'from'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/p&gt;'</span> <span style="color: #339933;">+</span>
      <span style="color: #3366CC;">'&lt;p&gt;&lt;label&gt;Date&lt;/label&gt; '</span> <span style="color: #339933;">+</span> dateNice <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/p&gt;'</span> <span style="color: #339933;">+</span>
      <span style="color: #3366CC;">'&lt;p&gt;&lt;label&gt;Subject&lt;/label&gt; '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'subject'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/p&gt;'</span> <span style="color: #339933;">+</span>
      <span style="color: #3366CC;">'&lt;p&gt;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.$el.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// if the message view is not already attached to the page, simply</span>
    <span style="color: #006600; font-style: italic;">// append it into the body</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.$el<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There is nothing too special going on here. All this view currently does is to render a bunch of HTML, using the properties of its model for the values.</p>
<p>You may spot a new object called <code>DateFunctions</code>. This was a simple utility object created as now we are displaying the data in two views (the <code>ListRowView</code> and the <code>MessageView</code>). </p>
<p>The big questions about this view are:<br />
 &#8211; how does it get the data to display? and<br />
 &#8211; where is it called from?</p>
<p>When working through this initially I started to initiate and load the <code>MessageView</code> directly in  <code>ListRowView</code> when the event was clicked (see /57e9e5f/). However, this lead to the two undesirable outcomes. </p>
<p>Firstly <code>MessageView</code> would have been repeatedly initialised every time someone tries to view a message. This isn&#8217;t too big a deal, but it is not efficient when we could initialise once and be done with it.</p>
<p>Secondly, as the <code>MessageView</code> would be initialised every time we want to view the message, when the view renders and injectes itself into the DOM we needed to add logic to stop it adding itself multiple times to the page.</p>
<p>There are a couple of ways to solve this. One method (although I didn&#8217;t try it) would possibly be to create a singleton class for the Message viewer, this would have stopped the reinitialisation issue.</p>
<p>The method I went for in the end was to &#8220;bubble&#8221; the event up, from the List Row to the main List view. As all the <code>ListRowViews</code> would bubble up the event, we would only need to call the <code>MessageView</code> once and then listen for the &#8220;view message&#8221; event to re-render the message viewer.</p>
<p>We use the Backbone <strong>trigger</strong> functionality to bubble up the event. (Recall that the <code>open</code> function is called when a row click event is detected).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  open<span style="color: #339933;">:</span> <span style="color: #000066; 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;">this</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;viewMessage&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now, in the <code>ListRowView</code> we make two changes. Within the <code>initialize</code> function we initialize <code>MessageView</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  initialize<span style="color: #339933;">:</span> <span style="color: #000066; 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;">this</span>.<span style="color: #660066;">messageView</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MessageView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>And then during the rendering process, when looping through the <code>listItems</code> we add a listener to the <code>ListRowView</code> instance and when a <em>viewMessage</em> event is detected, we instruct the <code>MessageView</code> instance to load the message based on the model passed with the event.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    self.<span style="color: #660066;">collection</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>listItem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> ListRowView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>model<span style="color: #339933;">:</span> listItem<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      v.<span style="color: #660066;">render</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;">// listen for the &quot;viewMessage&quot; event, loading the message whenever it is triggered</span>
      v.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'viewMessage'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>model<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        self.<span style="color: #660066;">messageView</span>.<span style="color: #660066;">loadMessage</span><span style="color: #009900;">&#40;</span>model<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: #339933;">;</span>
&nbsp;
      self.$el.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>v.<span style="color: #660066;">el</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: #339933;">;</span></pre></td></tr></table></div>

<p>The method to load the message within <code>MessageView</code> is very simple, all it does is to tell the model passed to (re)fetch it&#8217;s data and then renders the view.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> MessageView <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'message'</span><span style="color: #339933;">,</span>
&nbsp;
  loadMessage<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>model<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    self.<span style="color: #660066;">model</span> <span style="color: #339933;">=</span> model<span style="color: #339933;">;</span>
    self.<span style="color: #660066;">model</span>.<span style="color: #660066;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>success<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      self.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Finally, to finish this all off we must update the <code>ListItem</code> model ever so slightly, informing it where it should load in the data, by setting the URL. This is slightly more complicated than the <code>ListCollection</code> URL as we need to specify which message we want to load in, so have to add the model&#8217;s ID to the URL.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> ListItem <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">Model</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  url<span style="color: #339933;">:</span> <span style="color: #000066; 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> <span style="color: #3366CC;">'/mock-data/message.'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'.json'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>An example of the mock data we will load in is:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="json" style="font-family:monospace;">{
  &quot;id&quot;: 1,
  &quot;date&quot;: &quot;1357643063000&quot;, 
  &quot;subject&quot;: &quot;Testing the Backbone Reactive List Page&quot;, 
  &quot;from&quot;: &quot;Rob&quot; ,
  &quot;body&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque ornare, ligula a tincidunt cursus, lorem magna ullamcorper enim, ut interdum nisi quam vel lorem. Aliquam aliquam sem eu ipsum pretium ut bibendum quam blandit. Duis vel blandit nibh. Vestibulum mattis sapien eros, ut iaculis quam. Suspendisse potenti. Ut eleifend massa a libero adipiscing porttitor in ut nunc. Sed lobortis pretium consectetur. Morbi convallis, velit sit amet placerat vehicula, dolor lorem sollicitudin enim, at convallis tortor ipsum vel lectus. Fusce eget tellus sit amet nisl interdum volutpat. Etiam congue nibh id quam scelerisque posuere.&quot;
}</pre></td></tr></table></div>

<h2>Conclusion</h2>
<p>We are really starting to get to the meat of Backbone JS now. The ability to automatically get fresh data from the backend (or in our case the mock json data) and then the views to be updated without having to worry about JQuery calls or complex logic is really powerful stuff. </p>
<div class="alert-info">
<strong>Reminder: you can find all the source code to this project on <a href="https://github.com/robsearles/backbone-reactive/" target="_blank">GitHub</a>, this chapter has been <a href="https://github.com/robsearles/backbone-reactive/tree/ch3-events" target="_blank">tagged ch3-events</a>.</strong>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/05/adding-in-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overlay a Simple BackboneJS Application</title>
		<link>http://www.robsearles.com/2013/05/overlay-a-simple-backbonejs-application/</link>
		<comments>http://www.robsearles.com/2013/05/overlay-a-simple-backbonejs-application/#comments</comments>
		<pubDate>Wed, 15 May 2013 12:12:18 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Backbone Reactive]]></category>
		<category><![CDATA[backbonejs]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[reactive]]></category>
		<category><![CDATA[requirejs]]></category>
		<category><![CDATA[zepto]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=841</guid>
		<description><![CDATA[Now the static fallback content has been created and is working, we can start to lay a basic Backbone application over the top.]]></description>
				<content:encoded><![CDATA[<div class="alert-help">This is Part Two of the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Backbone Reactive Tutorial</a>. For all sections, please view the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Table of Contents</a>.</div>
<p>Now the static fallback content has been created and is working, we start overlaying a basic Backbone application over the top.</p>
<p>We will leave the homepage for the moment, coming back to that later on in this project. For now we will concentrate on the List page. The functionality of the List page is very simple: all it does is replace the static content on the List page with some dynamically generated content from JSON data retrieved by an Ajax call.</p>
<div class="alert-info">Note: You can find all the source code to this project on <a href="https://github.com/robsearles/backbone-reactive/" target="_blank">GitHub</a>, this chapter has been <a href="https://github.com/robsearles/backbone-reactive/tree/ch2-simple" target="_blank">tagged ch2-simple</a>.</div>
<h2>Dependencies</h2>
<p>In the <strong>list.html</strong> page we simply need to load in the required Javascript files for our application. Obviously there is Backbone, but we also need to include its dependencies, underscore and either jQuery or Zepto. In our case we will use jQuery for the time being, as it is the framework most people are familiar with. (As we progress with developing for mobile devices we will be investigating Zepto as a replacement).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery-1.8.3.min.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;/js/underscore-min.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;/js/backbone-min.js&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>Once we&#8217;ve loaded in our dependencies, we need to load in our actual application file:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;/js/app/list.js&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>It is this file, <strong>list.js</strong> that does all the work. There is quite a lot going on in this file, even for a simple app such as this, so let&#8217;s brake it down piece by piece.</p>
<h2>JSLint</h2>
<p>It is a good idea to lint your Javascript code. The main reasons for this is to give all your code a consistency so it is easier to come back to later. It also catches any silly typos or bugs along the way.</p>
<p>For linting this project, I am using the <a href="https://github.com/reid/node-jslint" target="_blank">SLint NodeJS</a> module, the lint command used is:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">jslint <span style="color: #660033;">-indent</span>=<span style="color: #000000;">2</span> <span style="color: #660033;">--plusplus</span> <span style="color: #660033;">--vars</span> .<span style="color: #000000; font-weight: bold;">/</span>js<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>list.js</pre></td></tr></table></div>

<p>JSLint can be a bit bossy, so you&#8217;ll notice that the first few lines in the code are simply there to keep there to keep JSLint happy.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #3366CC;">'use strict'</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> Backbone<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// hello jslint</span>
  <span style="color: #000066; font-weight: bold;">var</span> $<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// hello jslint</span></pre></td></tr></table></div>

<p>Now we have the tedium of keeping code tidy out the way, we can move onto the actual meat and potatoes.</p>
<h2>Models</h2>
<p>Reading through the rest of the <strong>list.js</strong> file, you&#8217;ll see that it follows a pattern of building up each needed component one at a time.</p>
<p>Firstly a <code>ListItem</code> Model is defined. This very simply extends the base <a href="http://backbonejs.org/#Model" target="_blank">Backbone Model</a>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> ListItem <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">Model</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This line is so simple because Backbone Models are so powerful. But why do we need a model in the first place?</p>
<p>The way I think about it, models are the basic units of data within a Backbone application. They are the smallest piece of complete information within context. For example, in our project, the smallest piece of complete information is an item of the list. This item itself has smaller pieces of data, such as a <em>date</em> or <em>subject</em>, but without the wider context of the item itself, these tiny pieces of data make no sense &#8211; they are not complete.</p>
<p>Models don&#8217;t just contain data, they also have logic handling that data: validation, access control etc as well as events bound to that data. For example, if the UI changes a property of the model, this can trigger an event so other parts of the UI can react. This is why they are so powerful.</p>
<p>However, for our purposes we are going to explore none of that right now. Instead we&#8217;ll move onto Collections</p>
<h2>Collections</h2>
<p>Whilst models provide the individual bits of data, <a href="http://backbonejs.org/#Collection" target="_blank">Backbone Collections</a>, as their name suggests, collate these individual bits of data into one group, or collection. The important thing to remember about collection are that the models within them are ordered. This allows us, for example, to load in ordered data and have this order honoured within the collection.</p>
<p>When defining a collection we want to start using dynamic data. Backbone has built in functionality for this, which greatly reduces the amount of code needed to be written for ajax calls etc.</p>
<p>To start using dynamic data in a collection we must specify a URL. This URL is for the ajax request when we call the collection&#8217;s <code>fetch()</code> method. (For the purposes of this tutorial we are simply using mock JSON data, not a dynamic backend). As we want the data to load in straight away we call the <code>fetch()</code> method in the initialize function, passing it any callback options.</p>
<p>Backbone has another built in method &#8211; <code>parse()</code> &#8211; to interpret the data received within the response from the ajax request. In our case, we simply want to return exactly the data that we fetched from the mock JSON file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> ListCollection <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">Collection</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
model<span style="color: #339933;">:</span> ListItem<span style="color: #339933;">,</span>
&nbsp;
initialize<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>models<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fetch</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// As we don't really care for the backend in this demo, we have</span>
<span style="color: #006600; font-style: italic;">// hard-coded some JSON data to use in the application</span>
url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/mock-data/list.getList.json'</span><span style="color: #339933;">,</span>
&nbsp;
parse<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">return</span> response<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As with the Models, Collections are simple, but powerful.</p>
<p>For reference, the format of the JSON data returned is as follows:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="json" style="font-family:monospace;">[
{&quot;id&quot;: 5, &quot;date&quot;: &quot;1357643063000&quot;, &quot;subject&quot;: &quot;Testing the Backbone Reactive List Page&quot;, &quot;from&quot;: &quot;Rob&quot; },
{&quot;id&quot;: 4, &quot;date&quot;: &quot;1357642823000&quot;, &quot;subject&quot;: &quot;Another exciting subject line&quot;, &quot;from&quot;: &quot;John&quot; },
{&quot;id&quot;: 3, &quot;date&quot;: &quot;1357641983000&quot;, &quot;subject&quot;: &quot;I just love dancing&quot;, &quot;from&quot;: &quot;Billy&quot; },
{&quot;id&quot;: 2, &quot;date&quot;: &quot;1357565783000&quot;, &quot;subject&quot;: &quot;Possible expansion plans&quot;, &quot;from&quot;: &quot;Wilhelm&quot; },
{&quot;id&quot;: 1, &quot;date&quot;: &quot;1357563263000&quot;, &quot;subject&quot;: &quot;Can't think of anything witty&quot;, &quot;from&quot;: &quot;Susan&quot; }
]</pre></td></tr></table></div>

<h2>Views</h2>
<p><a href="http://backbonejs.org/#View" target="_blank">Backbone Views</a> are where the action really gets going.</p>
<p>Now we have defined our data and how it is organised, we need to start using that data. What this means for us is that we need to start displaying this data within the browser.</p>
<p>The way I like to think about views is that they represent individual components or groups of components within the user interface. In our current list page we have broken it down into two views:<br />
- a view for the complete list, in our UI this is a table<br />
- a view for the individual list elements, or table rows in our case</p>
<p>This relates very closely to Collections (for the complete list) and Models (for the individual rows). In fact, Backbone allow us to attach both models and collections directly to views during initialization. something we will look at in a moment.</p>
<p>First we define the view for the table row, or list item:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> ListRowView <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
tagName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;tr&quot;</span><span style="color: #339933;">,</span>
className<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;list-row&quot;</span><span style="color: #339933;">,</span>
&nbsp;
....
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Setting the <strong>tagName</strong> property to <em>tr</em> means that when inserted into the DOM, this view will be a table row element. We also add a class name, thinking about it I&#8217;m not sure why, possibly just because we can.</p>
<p>Right now, this view is a little useless, so we need to implement the <code>render()</code> method:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">render<span style="color: #339933;">:</span> <span style="color: #000066; 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;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// I am not spending too much time on the date formatting or</span>
<span style="color: #006600; font-style: italic;">// making it look nice, I just need enough so it works</span>
<span style="color: #000066; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> <span style="">Date</span><span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'date'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> timeNice <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">padTime</span><span style="color: #009900;">&#40;</span>date.<span style="color: #660066;">getHours</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">':'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">padTime</span><span style="color: #009900;">&#40;</span>date.<span style="color: #660066;">getMinutes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> dateNice <span style="color: #339933;">=</span> date.<span style="color: #660066;">toLocaleDateString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> subject <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;subject&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> from <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;from&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Later I'll be converting this to a template, but for the time</span>
<span style="color: #006600; font-style: italic;">// being it will suffice</span>
<span style="color: #000066; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> timeNice <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> dateNice <span style="color: #339933;">+</span> <span style="color: #3366CC;">'
&nbsp;
'</span> <span style="color: #339933;">+</span>
<span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> subject <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> from <span style="color: #339933;">+</span> <span style="color: #3366CC;">'
&nbsp;
'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">this</span>.$el.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>The first thing to notice is that we are associating the ID of this view instance with that of the view&#8217;s model. This is important when it comes to triggering and responding to events later.</p>
<p>However, we have neatly glossed over where the model comes from. How does the View contain a model from which it can use it&#8217;s ID?</p>
<p>As mentioned above, views can have collections and models attached to them during initialization. Jumping ahead of ourselves slightly, in the <code>ListView</code> view, we can see that when we loop through the collection of items, we initialize the <code>ListRowView</code> passing it a <code>ListItem</code> model instance.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">self.<span style="color: #660066;">collection</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>listItem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> ListRowView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>model<span style="color: #339933;">:</span> listItem<span style="color: #009900;">&#125;</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: #339933;">;</span></pre></td></tr></table></div>

<p>The rest of the <code>render()</code> method is pretty self explanatory (if not incredibly quick and dirty, we will fix this in a later post).</p>
<p>We first clean up some of the model&#8217;s properties so they are suitable to be displayed to the general public. Next we build the HTML for the table row and then append this HTML to the table row element the view represents. Finally, we return ourselves as is convention.</p>
<p>So that is the individual table rows sorted. Next we need to look at the complete list as a whole.</p>
<p>As ever, we simply extend the base Backbone View:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> ListView <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>However, unlike the <code>ListRowView</code> we are going to define an initialize method:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">initialize<span style="color: #339933;">:</span> <span style="color: #000066; 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;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
self.<span style="color: #660066;">collection</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> ListCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// the success function is used for the fetch callback</span>
success<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
self.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>This is doing three very important things so let&#8217;s pay close attention to them.</p>
<p>Firstly it is initializing the ListCollection. As described above, the act of initializing this collection will fetch the data from the mock JSON we have and assigning the results to itself.</p>
<p>Secondly we are assigning this ListCollection to be the current collection for this view. This will allow us to access the Collection data and contained Models far easier later on.</p>
<p>Finally, once the data has been loaded and the view&#8217;s collection populated we render the view.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">render<span style="color: #339933;">:</span> <span style="color: #000066; 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;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> <span style="color: #3366CC;">' [ edited for brevity ] '</span><span style="color: #339933;">;</span>
self.$el.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
self.<span style="color: #660066;">collection</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>listItem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> ListRowView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>model<span style="color: #339933;">:</span> listItem<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
v.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
self.$el.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>v.<span style="color: #660066;">el</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: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">return</span> self<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>As we have already dealt with the most important part of this render method above, the rest of it should be pretty self-explanatory. However, to quickly summarize we are first creating a bunch of HTML represented by this view (yes yes, I know! We will move it to a template in a later post) and then injecting it into the DOM. Next we loop through each model in the collection, initializing a new view for each model and then appending that to the table element we have just injected into the DOM. Finally, we return the view itself as is convention.</p>
<h2>Running the App</h2>
<p>Finally, now all the Models, Collections and Views have been defined, we can run the app. This is simply a case of initiating the ListView, applying it to a specific DOM element. In our case it is <strong>#content</strong>.</p>
<p>That is it. To test it out, fire up the <strong>_server.js</strong> NodeJS HTTP server and navigate to http://localhost:7777/list.html. You should see a table of list items, which has been entirely populated by your Backbone application.</p>
<p>Congratulations. The first step is complete. On to events.</p>
<div class="alert-info">Reminder: you can find all the source code to this project on <a href="https://github.com/robsearles/backbone-reactive/" target="_blank">GitHub</a>, this chapter has been <a href="https://github.com/robsearles/backbone-reactive/tree/ch2-simple" target="_blank">tagged ch2-simple</a>.</div>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/05/overlay-a-simple-backbonejs-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bank Holiday Reading 6 May 2013</title>
		<link>http://www.robsearles.com/2013/05/bank-holiday-reading-6-may-2013/</link>
		<comments>http://www.robsearles.com/2013/05/bank-holiday-reading-6-may-2013/#comments</comments>
		<pubDate>Mon, 06 May 2013 06:20:17 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[links]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=826</guid>
		<description><![CDATA[To start of with, we have a great story about a game developer releasing a game about game development and what happens when the pirates were pirated. As I find myself tumbling down the rabbit hole of Lisp and it&#8217;s derivatives, this article was very timely for me. An interesting read and makes me want...  <a class="excerpt-read-more" href="http://www.robsearles.com/2013/05/bank-holiday-reading-6-may-2013/" title="ReadBank Holiday Reading 6 May 2013">Read more &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>To start of with, we have a <a href="http://www.greenheartgames.com/2013/04/29/what-happens-when-pirates-play-a-game-development-simulator-and-then-go-bankrupt-because-of-piracy" target="_blank">great story</a> about a game developer releasing a game about game development and what happens when the pirates were pirated.</p>
<p>As I find myself tumbling down the rabbit hole of Lisp and it&#8217;s derivatives, <a href="http://blog.ppenev.com/2013/04/24/learning-lisp-the-bump-free-way.html" target="_blank">this article</a> was very timely for me. An interesting read and makes me want to roll my sleeves up yet further.</p>
<p>To continue the Lispish theme, <a href="http://spin.atomicobject.com/2013/05/02/chicken-scheme-part-1/" target="_blank">this post about Chicken Scheme</a> did not disappoint. A really great read for the weekend. Looks like a very interesting <a href="http://spin.atomicobject.com/" target="_blank">blog</a> to follow.</p>
<p>Just to reinforce the point that the <a href="http://spin.atomicobject.com/" target="_blank">Atomic Object&#8217;s Blog</a> was worth following, here is <a href="http://spin.atomicobject.com/2012/12/23/code-reading/" target="_blank">another good post</a>. This time it is about reading code.</p>
<p>Back to the lisp theme. I have had SICP on my bookshelf for a number of years, but never really tackled it. <a href="http://www.cs.berkeley.edu/~bh/sicp.html" target="_blank">This article by Brian Harvey</a> reminds me that I probably should give it another go.</p>
<p>Finally, if you have had enough reading for the week, watch this <a href="http://www.youtube.com/watch?v=7XTHdcmjenI" target="_blank">15 minute TedX talk</a> about what we can learn from Linus Torvalds.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/05/bank-holiday-reading-6-may-2013/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backbone Reactive &#8211; Fallback Version</title>
		<link>http://www.robsearles.com/2013/05/backbone-reactive-fallback-version/</link>
		<comments>http://www.robsearles.com/2013/05/backbone-reactive-fallback-version/#comments</comments>
		<pubDate>Thu, 02 May 2013 13:37:00 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Backbone Reactive]]></category>
		<category><![CDATA[backbonejs]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[reactive]]></category>
		<category><![CDATA[requirejs]]></category>
		<category><![CDATA[zepto]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=782</guid>
		<description><![CDATA[Even though we will be building a JavaScript intensive application, it is always best to start off with a simple fallback version for those older devices and browsers.]]></description>
				<content:encoded><![CDATA[<div class="alert-help">This is Part One of the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Backbone Reactive Tutorial</a>. For all sections, please view the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Table of Contents</a>.</div>
<p>The first thing we should always do, when building any application that relies heavily on the frontend Javascript is to produce a fallback version. The reasons are pretty straight forward:</p>
<ol>
<li>If a user visits your application and for whatever reason they don&#8217;t happen to Javascript enabled, then they will still be able to get some value out of your site. This will obviously be heavily reduced value, but it is better than nothing, of even worse, a message saying &#8220;You Need Javascript&#8221;</li>
<li>It is always handy for SEO purposes to have at least some content on the page, not just a link to a Javascript file which will generate the content.</li>
</ol>
<p><span id="more-782"></span></p>
<p>So given that we need some fallback content, when should this be produced? Personally, I prefer creating a static site right at the beginning of the process, and then layering the Javascript functionality over the top. For me it makes sense as the likely hood that I&#8217;ll retroactively fit the fallback content underneath the Javascript is pretty slim.</p>
<p>In this initial commit, all I have done is to create the static html fallback site. It includes two main pages &#8211; the homepage and a message list page, as well as 5 messages. In a real application these would most likely not be static pages, but instead generated via PHP, Ruby, NodeJS etc. However, for the purposes of this project, I am not at all interested in the backend.</p>
<p>Included in this initial commit is a very simple static web server (powered by <a href="https://nodejs.org" target="_blank">NodeJS</a>) to serve the files. This has been included so you don&#8217;t have to worry about setting up Apache or similar. Instructions for use are in the _server.js file itself.</p>
<div class="alert-info">You can find the source code of this initial commit in the <a href="https://github.com/robsearles/backbone-reactive/commit/5bd639797c9df0967280fdff5fa5b9b7c047bcb8" target="_blank">Git Repo</a> and under the tag <a href="https://github.com/robsearles/backbone-reactive/tree/ch1-fallback">ch1-fallback</a>.</div>
<p>Next time we&#8217;ll look at building a simple Backbone.js application over the top of our fallback setup.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/05/backbone-reactive-fallback-version/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Backbone Reactive &#8211; Introduction</title>
		<link>http://www.robsearles.com/2013/04/backbone-reactive-introduction/</link>
		<comments>http://www.robsearles.com/2013/04/backbone-reactive-introduction/#comments</comments>
		<pubDate>Tue, 30 Apr 2013 13:28:57 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Backbone Reactive]]></category>
		<category><![CDATA[backbonejs]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[reactive]]></category>
		<category><![CDATA[requirejs]]></category>
		<category><![CDATA[zepto]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=779</guid>
		<description><![CDATA[This series of posts is the culmination of my experimentation, problems faced, decisions made and lessons learned when playing around with BackboneJS to build reactive sites.]]></description>
				<content:encoded><![CDATA[<div class="alert-help">This is Part Zero of the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Backbone Reactive Tutorial</a>. For all sections, please view the <a href="http://www.robsearles.com/category/tutorials/backbone-reactive/">Table of Contents</a>.</div>
<p>At the end of 2012 I began playing around with <a href="http://backbonejs.org/" target="_blank">Backbone.js</a>. One of the things I was very keen on was producing a site that used a single codebase but could handle desktop, tablet and mobile version with minimal effort &#8211; so called &#8220;reactive design&#8221;.</p>
<p>This series of posts is the culmination of my experimentation, problems faced, decisions made and lessons learned.</p>
<p>There will be roughly 15 posts in this series (not sure an exact number as they are still being written, tweaked, combined and expanded. I aim to publish one per week, but if I have the time I&#8217;ll try I&#8217;ll try and up the frequency.</p>
<p>I hope you enjoy.</p>
<div class="alert-info">You can find all the source code for this tutorial in the <a href="https://github.com/robsearles/backbone-reactive/" target="_blank">Git Repo</a>.</div>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/04/backbone-reactive-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Org-Mode, Shared Agenda File List</title>
		<link>http://www.robsearles.com/2013/03/org-mode-shared-agenda-file-list/</link>
		<comments>http://www.robsearles.com/2013/03/org-mode-shared-agenda-file-list/#comments</comments>
		<pubDate>Wed, 27 Mar 2013 08:26:20 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Emacs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=765</guid>
		<description><![CDATA[After watching a chat between Sacha Chau and John Wiegley about Emacs, I&#8217;ve gone on an Emacs rampage. I tweaking my init script to use org-mode (probably more on that another day) and generally getting things ticking along quite nicely. I use three different computers (desktop at office, desktop at home and laptop) and all my...  <a class="excerpt-read-more" href="http://www.robsearles.com/2013/03/org-mode-shared-agenda-file-list/" title="ReadOrg-Mode, Shared Agenda File List">Read more &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>After watching a chat between <a href="http://sachachua.com/blog/2012/06/emacs-chatting-with-john-wiegley-about-the-cool-things-he-does-with-emacs/" target="_blank">Sacha Chau and John Wiegley</a> about Emacs, I&#8217;ve gone on an Emacs rampage. I tweaking my init script to use org-mode (probably more on that another day) and generally getting things ticking along quite nicely.</p>
<p>I use three different computers (desktop at office, desktop at home and laptop) and all my Org files are kept in sync via Dropbox. However, one thing that has irritated me with this setup is the facts that whilst the files are shared between the three machines, which of the Org files are considered <em>agenda </em>files is not. On each machine I have to keep on adding files into the agenda list with <code>C-c [</code>. I have a number of different agenda files (one for each project or sub-project I&#8217;m working on) so files are added to and removed from the list quite frequently.</p>
<p>It would be great if the lost of agenda files could be sync&#8217;d too.</p>
<p>As ever in Emacs, there is a way. In this case it is the variable <code>custom-file</code>. </p>
<p>You can get more information about this variable using the describe-variable command (<code>C-h v</code> then enter <code>custom-file</code>)</p>
<p>So, to change the location of the custom file, all I needed to was to add the below to my init file:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Ensure that any custom variables are synced via Dropbox</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> custom-file <span style="color: #ff0000;">&quot;~/Dropbox/emacs/custom.el&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>load custom-file<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Hurray for Emacs!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/03/org-mode-shared-agenda-file-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Org2Blog</title>
		<link>http://www.robsearles.com/2013/03/testing-org2blog/</link>
		<comments>http://www.robsearles.com/2013/03/testing-org2blog/#comments</comments>
		<pubDate>Tue, 26 Mar 2013 10:14:00 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[org-mode]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=761</guid>
		<description><![CDATA[I have just installed Org2Blog so testing it out. Increasingly my life is being consumed by Emacs and org-mode.]]></description>
				<content:encoded><![CDATA[<p>This is a very quick test of posting to my WordPress blog using <a href="https://github.com/punchagan/org2blog">org2blog</a>. As such, it&#8217;s probably not going to be very long, but hey, it&#8217;s a test. </p>
<div id="outline-container-1" class="outline-3">
<h3 id="sec-1">Heading Level Two</h3>
<div class="outline-text-3" id="text-1">
<p> What does this look like. Clearly this is very exciting! Now, how to publish&hellip; ah, <code>C-c p</code>. </p>
</p></div>
</p></div>
<div id="outline-container-2" class="outline-3">
<h3 id="sec-2">After Publish Edit</h3>
<div class="outline-text-3" id="text-2">
<p> Well, that was surprisingly easy! I could get used to this. </p>
</p></div>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/03/testing-org2blog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Should we use Frameworks?</title>
		<link>http://www.robsearles.com/2013/02/should-we-use-frameworks/</link>
		<comments>http://www.robsearles.com/2013/02/should-we-use-frameworks/#comments</comments>
		<pubDate>Thu, 14 Feb 2013 10:39:52 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=754</guid>
		<description><![CDATA[Over the past month there have been a number of security issues with the Ruby on Rails framework. These issues have been serious. Whilst there is some debate whether this is an isolated Rails issue or if we can expect more frameworks to start seeing these sorts of vulnerabilities discovered this has left me with a niggling...  <a class="excerpt-read-more" href="http://www.robsearles.com/2013/02/should-we-use-frameworks/" title="ReadShould we use Frameworks?">Read more &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>Over the past month there have been a number of security issues with the Ruby on Rails framework. These issues have been serious. Whilst there is <a href="http://news.ycombinator.com/item?id=5145631">some debate</a> whether this is an isolated Rails issue or if we can expect more frameworks to start seeing these sorts of vulnerabilities discovered this has left me with a niggling question at the back of my mind.</p>
<p><b>Should we use frameworks?</b></p>
<p>Rails is a massive monolithic framework, but (security issues aside) do the benefits of using it outweigh the benefits of not using it? This goes for any framework, in any language. Are we ultimately better developers for using frameworks, or for not using frameworks?</p>
<p>To answer these questions, I have drawn up a (completely non extensive and totally subjective) list of pros and cons.</p>
<p>The main benefit of using frameworks is the lack of having to constantly reinvent the wheel. Every time you start a new project, is it easier to use a framework that comes with ready-made modules for accessing the database, session management, caching etc, or is it easier writing all this stuff from scratch?</p>
<p>The answer simply must be the former. The reason for this is two fold:</p>
<p>Firstly, the time you save not having to reinvent the wheel you can put to actually developing your application. In this respect it will greatly speed up development time.</p>
<p>Secondly, as these utility modules (for want of a better term) are within a framework, you can be sure that the code has had more people looking over it than if you wrote it yourself.</p>
<p>This leads on to my next point. As Eric Raymond dubbed &#8220;Linus&#8217;s Law&#8221;: &#8220;<a href="http://en.wikipedia.org/wiki/Linus%27s_Law">given enough eyeballs, all bugs are shallow</a>&#8220;. With a popular framework that has a vibrant and engaged community you should be relatively confident that the code has been reviewed a number of times by a number of different authors. Or at least, you <b>should</b> be relative confident.</p>
<p>But what of the vulnerabilities found in Rails? These issues had been lying there for some time. They had certainly been there long enough for the issues to be reported multiple times, but no action was taken. Again, I&#8217;m not trying to single out Rails here, but it is the highest profile framework at the moment.</p>
<p>However, there is nothing stopping you from auditing the code on your favourite open source framework and contributing back to the community. In my opinion this is another benefit of using a framework &#8211; the community. If you engage with the community, follow the mailing lists, chat in IRC then you can really improve your usage and productivity with the framework, again helping you develop better applications.</p>
<p>But what about the disadvantages of using frameworks?</p>
<p>Firstly it is <i>yet another thing</i> to learn. If you are just starting out with a framework you have to learn it. You must learn it&#8217;s foibles and nuances. This takes time, slowing you down. Granted, once you have learned the framework your development time is quicker than without a framework as discussed, but to start off with, development is slower.</p>
<p>As well as learning a frameworks foibles and nuances you must also learn its limitations. There are just some things that some framework will either not let you do, or are harder to do than if you write the code raw. When developing with a framework this is something that must be taken into account.</p>
<p>Ironically, whilst frameworks contain limitations, a lot of them are also bloated. Zend for PHP springs to mind as in fact does Rails. When using frameworks such as these your application is not as optimal as it can be because it is carrying around the huge and for you possibly mostly unneeded weight of the framework.</p>
<p>However, in my mind the biggest disadvantage is the &#8220;black box&#8221; scenario. People just blindly using the frameworks without delving deep into the code that the framework is made from. There is an element of faith here. Faith that the code is sound and secure. Faith that the code in the framework and the development of that code has been treated seriously, professionally and with respect. This is especially true with Rails, in part due to it&#8217;s &#8220;cool&#8221; tag a lot of newbie programmers are using it. There is nothing wrong with this, Ruby and Rails are great places to start the journey of coding. But if you are just starting out you cannot possibly know of all the implications of taking a codebase on faith.</p>
<p>In this case the users of Rails were badly let down.</p>
<p>So what is the conclusion? Should we use frameworks?</p>
<p>Overall, I think yes. A framework should be used. But what type of framework? that is the more important question.</p>
<p>The framework you use should be small, small enough so you can truly get to understand it. Small enough to only contain the elements that you really need. However, it should have a vibrant and responsive community. Now I don&#8217;t mind if the framework you use is an existing one or it is a framework you have developed for yourself. But if you have lovingly hand-crafted your own framework you should open source it. It is imperative that the framework you use must have a community. A community to find and fix bugs, to keep the code clean and optimised. To document and battle-harden.</p>
<p>You must either find a framework that is small and has a great community, or build the framework you need with a community to match.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/02/should-we-use-frameworks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sonar GNU/Linux on Indiegogo</title>
		<link>http://www.robsearles.com/2013/01/sonar-gnulinux-on-indiegogo/</link>
		<comments>http://www.robsearles.com/2013/01/sonar-gnulinux-on-indiegogo/#comments</comments>
		<pubDate>Thu, 24 Jan 2013 22:07:06 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=748</guid>
		<description><![CDATA[In my random trawling through the internet I can across a very worthwhile Indiegogo campaign. It is raising money to help develop the Sonar Project, a GNU/Linux system focusing on accessibility. This is the thing I particularly love about Linux, and the free software movement in general: the fact that you can take a product...  <a class="excerpt-read-more" href="http://www.robsearles.com/2013/01/sonar-gnulinux-on-indiegogo/" title="ReadSonar GNU/Linux on Indiegogo">Read more &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>In my random trawling through the internet I can across a <a href="http://www.indiegogo.com/sonar/x/1765104" target="_blank">very worthwhile Indiegogo campaign</a>. It is raising money to help develop the <a href="http://www.sonar-project.org/" target="_blank">Sonar Project</a>, a GNU/Linux system focusing on accessibility.</p>
<p>This is the thing I particularly love about Linux, and the free software movement in general: the fact that you can take a product and improve it in the way that best suits you. This is a commendable effort for bringing Linux and computing to those with disabilities and should be supported.</p>
<p>Go and support Sonar now.</p>
<ul>
<li><a href="http://www.indiegogo.com/sonar/x/1765104" target="_blank">Indiegogo Funding Page</a></li>
<li><a href="http://www.sonar-project.org/" target="_blank">Sonar Project Homepage</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/01/sonar-gnulinux-on-indiegogo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Improve your Programming Skills</title>
		<link>http://www.robsearles.com/2013/01/how-to-improve-your-programming-skills/</link>
		<comments>http://www.robsearles.com/2013/01/how-to-improve-your-programming-skills/#comments</comments>
		<pubDate>Thu, 10 Jan 2013 20:05:06 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[improve-coding]]></category>
		<category><![CDATA[improve-programming]]></category>
		<category><![CDATA[self-improvement]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=740</guid>
		<description><![CDATA[As the subtitle of this blog indicates, I am a self taught programmer. Like most self taught coders, I suffer from the horrible feeling that I am missing something in my programming education. As such I am continually striving for improvement. But how do you achieve improvement? Where do you start? What do you do?...  <a class="excerpt-read-more" href="http://www.robsearles.com/2013/01/how-to-improve-your-programming-skills/" title="ReadHow to Improve your Programming Skills">Read more &#187;</a>]]></description>
				<content:encoded><![CDATA[<p>As the subtitle of this blog indicates, I am a self taught programmer.</p>
<p>Like most self taught coders, I suffer from the horrible feeling that I am missing something in my programming education. As such I am continually striving for improvement. But how do you achieve improvement? Where do you start? What do you do? Is there any set methodology?</p>
<p>Searching the internet looking for direction yields a bunch of different advice, but it is mostly really common sense: write more code; read more code; learn a new language; do <a href="http://www.reddit.com/r/dailyprogrammer" target="_blank">coding exercises</a>; etc.</p>
<p>This is obviously all good advice, but it misses the heart of the problem: focus.</p>
<p>It is all well and good learning a new language, just as it is well and good completing some exercises, but this is ultimately busy work if you don&#8217;t know what particular aspects of your coding you want to improve.</p>
<p>Improving your ability to code complex algorithms is very different to improving your ability to code complex front-end user interfaces.</p>
<p>If you are aiming to improve the former, then no amount of learning the latter will help.</p>
<p>Programming is now a vast field, and it is only once you have decided what aspects of programming it is that you want to improve, where to apply your focus, can you truly start to improve.</p>
<p>So, what areas of your coding skills do you want to improve? Define that, then answering the question of how to improve becomes much clearer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2013/01/how-to-improve-your-programming-skills/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
