<?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>Nick Zalabak - techwhizbang &#187; Software</title>
	<atom:link href="http://techwhizbang.com/category/software/feed/" rel="self" type="application/rss+xml" />
	<link>http://techwhizbang.com</link>
	<description>my work, life, and ideas</description>
	<lastBuildDate>Wed, 25 Jan 2012 05:47:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Short and Sweet Disclojure</title>
		<link>http://techwhizbang.com/2012/01/short-and-sweet-disclojure/</link>
		<comments>http://techwhizbang.com/2012/01/short-and-sweet-disclojure/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 06:32:53 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=516</guid>
		<description><![CDATA[After experimenting and bouncing back and forth on several relatively new languages, I was undecided on what new language I wanted to commit myself to. It wasn&#8217;t until I began reading Clojure in Action that I really started to grok and see through its otherwise intimidating syntax. Admittedly, Clojure just looked and sounded too exotic [...]]]></description>
			<content:encoded><![CDATA[<p>After experimenting and bouncing back and forth on several relatively new languages, I was undecided on what new language I wanted to commit myself to. It wasn&#8217;t until I began reading <em><a href="http://amzn.com/1935182595" title="Clojure in Action" target="_blank">Clojure in Action</a></em> that I really started to grok and see through its otherwise intimidating syntax. Admittedly, Clojure just looked and sounded too exotic at first glance. Do I really want to learn Lisp? Aren&#8217;t those parentheses crazy? Shouldn&#8217;t I be programming the next million dollar idea for the iPhone? </p>
<p>It turns out that the answers to those questions are: Yes. No. What money? </p>
<p>I wanted a challenge. I wanted something that would transcend the way I&#8217;ve been thinking about programming. I wanted something that I knew would make me a better software engineer. I wanted something more expressive and powerful. </p>
<p>Luckily, the past two weeks I&#8217;ve had an excellent opportunity at work to apply and push my learnings of Clojure further. I thought it would be an opportune time to share the good, the bad, and whatever else in between. A reflective and honest &#8220;disclojure&#8221;. </p>
<p>It seems that the best way to learn a new language is finding well documented source code and blogs from likeminded people. Clojure is a &#8220;newer&#8221; burgeoning language so it goes without saying that there are far fewer comprehensive online resources to tap into. The blogosphere is actually a bit sparse on Clojure. I&#8217;d strongly recommend having a couple Clojure e-books at your fingertips for reference. Speaking of books, don&#8217;t expect to learn Clojure by reading a stack of books for 5 months and think that it will magically flow from your digits. It&#8217;ll never ever happen. The best recipe for me has been a 1:1 ratio of time spent reading and coding. If you&#8217;re in pursuit of books that might help you on your quest to learning Clojure, remember Clojure is a Lisp. <em><a href="http://amzn.com/0262560992" title="The Little Schemer" target="_blank">The Little Schemer</a></em> and <em><a href="http://amzn.com/026256100X" title="The Seasoned Schemer" target="_blank">The Seasoned Schemer</a></em> have been priming Lisp programmers for decades. If anything reading these two books will make you hungrier for more, wink wink nudge nudge.</p>
<p>Github has been an invaluable asset when looking for good source code examples. One of the questions I continue to ask myself while coding, &#8220;Is the code I&#8217;m writing idiomatic?&#8221; That is important to me because one of my main objectives is to transcend my thinking and programming style. So before you add that dependency to your <a href="https://github.com/technomancy/leiningen" title="Leiningen" target="_blank">Leiningen</a> project.clj have a peek at the author&#8217;s source. In reality, you might often find yourself sifting through source because of the lack of documentation or examples. Actually I read other developers source code daily for this very reason. So let me re-phrase that, while you&#8217;re trying to figure out how the heck some library or &#8220;clojar&#8221; works, be mindful of the coding style. Undoubtedly you will pick up something useful.</p>
<p>I had a perplexing problem the other day and for the life of me I couldn&#8217;t figure it out. I simply lacked the context necessary that other more advanced Clojure developers have. I found my answer after posting my question on the <a href="http://groups.google.com/group/clojure" title="Clojure Google Groups" target="_blank">Clojure forum</a> hosted on Google Groups. More specifically, I was using lein run to execute a simple database preparation task, but the process was hanging on well after it completed. It wasn&#8217;t until someone informed me that I should use (shutdown-agents) after the task I was running finished because of the way the agent thread pool delays shut down. See my gist <a href="https://gist.github.com/1635837">here</a>. The turn around time on the Clojure forum varies. I think it took 2 or 3 days before my post was moderated and then received a response. Most if not all of the Clojure core developers answer questions in this forum so the responses are of high quality and are super helpful.</p>
<p>Sometimes you&#8217;re totally stuck. Even after using your e-book as a reference and reading the source code it just doesn&#8217;t make sense. Enter <a href="irc://irc.freenode.net/#clojure" title="Clojure IRC" target="_blank">Clojure IRC</a>! During the daytime it is bustling with chatter. Lots of the folks who answer questions on the Clojure forum hang out here too. In my experiences, the community that gathers here is friendly and willing to help. </p>
<p>Clojure is as expressive, powerful, and succinct as advertised. The Java interop with Clojure is about the simplest and cleanest I&#8217;ve seen thus far and that includes JRuby and Scala. I&#8217;m impressed with the clean HTTP API Ring has to offer and using the Clojure JDBC API is really nice. If only JDBC were that easy in Java there would be no need for heavyweight clunky ORMs. In terms of testing, speclj is a pretty awesome testing framework that mirrors Ruby&#8217;s RSpec API. I also have enjoyed rolling my own solutions for things that aren&#8217;t available yet. And one of the most important parts&#8230;it is fast and performs very well under load. I don&#8217;t make these claims falsely either, we ran a load test today at work and we were absolutely thrilled with the response times and throughput. I look forward to posting more about Clojure in the coming weeks. Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2012/01/short-and-sweet-disclojure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monitoring a remote JVM process</title>
		<link>http://techwhizbang.com/2011/12/monitoring-a-remote-jvm-process/</link>
		<comments>http://techwhizbang.com/2011/12/monitoring-a-remote-jvm-process/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 22:52:49 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=503</guid>
		<description><![CDATA[Monitoring a remote JVM process has never been easier. As part of the default JDK installation you have a utility called jconsole at your fingertips. jconsole provides a simple way to attach to a running JVM process either locally or remotely. There are basically two ways to attach to the process, either choosing to attach [...]]]></description>
			<content:encoded><![CDATA[<p>Monitoring a remote JVM process has never been easier. As part of the default JDK installation you have a utility called jconsole at your fingertips. jconsole provides a simple way to attach to a running JVM process either locally or remotely. There are basically two ways to attach to the process, either choosing to attach to the PID or through JMX. Personally, I find that attaching to a PID is the easiest either remotely or locally.</p>
<p>Attaching to a process remotely might seem difficult or impossible, but if you are able to use X11 window forwarding through SSH it is a nice option. Check your /etc/ssh/ssh_config and make sure that you see ForwardX11 is set to &#8220;yes&#8221;, sometimes it is X11Forwarding, check your Linux distribution to be sure.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Host <span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #666666; font-style: italic;">#   ForwardAgent no</span>
   ForwardX11 <span style="color: #c20cb9; font-weight: bold;">yes</span></pre></div></div>

<p>Once you have made this change, restart your ssh service /etc/init.d/ssh restart or service ssh restart. Voila. Now ssh onto your remote server with the X option</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-X</span> user<span style="color: #000000; font-weight: bold;">@</span>remote.server.com</pre></div></div>

<p>So once you&#8217;ve SSH&#8217;d into your remote server startup jconsole, assuming it is in your PATH you should be able to just type it straight away. You should then immediately see that jconsole has started up through X11 as if it were running on your local machine. Just select the PID and you will be able to start monitoring memory, threads, garbage collection, etc.</p>
<div id="attachment_507" class="wp-caption aligncenter" style="width: 545px"><img class="size-full wp-image-507 " title="jconsole " src="http://techwhizbang.com/wp-content/uploads/2011/12/Screen-Shot-2011-12-07-at-2.50.28-PM.png" alt="jconsole" width="535" height="448" /><p class="wp-caption-text">jconsole</p></div>
<p>Generally when I am load testing an application (I use JMeter) on the JVM I will crack open jconsole to see if there are any blaring memory leaks or memory consumption problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/12/monitoring-a-remote-jvm-process/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fat chunky ugly bloated &#8220;models&#8221;</title>
		<link>http://techwhizbang.com/2011/08/fat-chunky-ugly-bloated-models/</link>
		<comments>http://techwhizbang.com/2011/08/fat-chunky-ugly-bloated-models/#comments</comments>
		<pubDate>Sat, 27 Aug 2011 08:29:14 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=461</guid>
		<description><![CDATA[I&#8217;m not referring to the runway model variety, I&#8217;m talking about code. Observation I&#8217;ve had the opportunity to work with many Ruby code bases the past few years. One reoccurring theme I keep seeing is bloated models. Fat chunky ugly bloated models. Literally thousands of lines, mixing in of modules, and extensions galore. All of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not referring to the runway model variety, I&#8217;m talking about code.</p>
<h3>Observation</h3>
<p>I&#8217;ve had the opportunity to work with many Ruby code bases the past few years. One reoccurring theme I keep seeing is bloated models. Fat chunky ugly bloated models. Literally thousands of lines, mixing in of modules, and extensions galore. All of which are convoluting the notion of two completely separate things, data access and business logic. The phrase &#8220;fat models&#8221; has been taken too literally. I have one question. Why?</p>
<p>I am officially declaring it an anti-pattern for the Ruby community. A &#8220;model&#8221; is supposed to be an object representation of the underlying data your application needs. So when working with a _data_ model, you ought to stay laser focused on the DML behavior. Get your data and get out. Move on to the next set of objects that are responsible for determining, strategizing, composing, decorating, and displaying (these action verbs should sound familiar) the object representations of your data.</p>
<p>Remarkably most Java projects I&#8217;ve seen get this one right. Typically there is DAO, there is an entity, and beyond those two objects that are focused on DML, there are other objects that responsible for doing &#8220;things&#8221; with that data. I say &#8220;things&#8221;, but I am referring to whatever your business or domain does with the data beyond creating, reading, updating, or deleting it.</p>
<h3>Real World Example</h3>
<p>Here&#8217;s a solid example for the sake of discussion. Every e-commerce site has some notion of a product or service. Suppose you are modeling a &#8220;Product&#8221;. Your product has some attributes regarding description, size, weight, color, etc. You get the gist. Let&#8217;s not forget pricing, every product has some type of pricing. I see pricing go sideways almost every time. Pricing always starts out simple. You might have a &#8220;base&#8221; price and &#8220;selling&#8221; price. The &#8220;base&#8221; price is your company&#8217;s cost, and the &#8220;selling&#8221; price is your customers cost. Shortly after you deliver that functionality your product development team will undoubtedly come up with all kinds of pricing <strong><em>strategies</em></strong>.</p>
<p>These should all sound familiar:</p>
<ul>
<li>site wide sales</li>
<li>single use coupons</li>
<li>volume discounts</li>
<li>bundling discounts</li>
<li>affiliate pricing tiers</li>
<li>employee discounts</li>
</ul>
<h3>Wrong</h3>
<p>This is where it goes sideways. It&#8217;s so easy, right? Just fetch the &#8220;selling&#8221; price within an instance of the &#8220;Product&#8221; model and being applying the business rules inside your model.</p>
<h3>Right</h3>
<p>I hinted a couple times in that this is a perfect application for the strategy pattern. <a title="Strategy Pattern" href="http://en.wikipedia.org/wiki/Strategy_pattern" target="_blank">Not familiar?</a> Each individual pricing type should have it&#8217;s own strategy. It is safe to let a &#8220;Price&#8221; object flow though the strategies. The pricing strategies classes can then apply the discounts and the appropriate pricing separately. One of the golden rules is that composition is almost always better than inheritance and that is the construct the strategy pattern embraces.</p>
<p>That should look and feel better on many different levels. You should observe the freedom to add or remove pricing features without the tight coupling to any particular model. In the case that your business evolves, and it inevitable does, your pricing strategies should be flexible enough to handle any sort of pricing irrespective of whether it has anything to do with a &#8220;Product&#8221; or not. It should also be easier to test. There are no worries about the creation of database objects or fixtures, simply assert the strategies of pricing with a &#8220;Pricing&#8221; object. Also as the pricing strategies grow in size and complexity it will certainly be easier to abstract away into a separate library.</p>
<h3>Facetious Wonderment</h3>
<p>Here is where my &#8220;wonderment&#8221; comes into action. How is it possible that so many &#8220;Rubyists&#8221; are ignoring traditional OO programming techniques and patterns when coding at the data access layer? Is it possible that the suggestive nature of the most popular and opinionated Ruby framework has created some psychological voodoo on developers making them believe that classes only come in 4 flavors (models, views, controllers, and helpers)? Is it really that scary to add new directories or categorize your classes differently? Perhaps it is the dynamic nature of the language? Can it be the power and &#8220;expressiveness&#8221; of the language? A language wherein the lack of true interfaces, strict type checking, and the magical injection of behaviors and methods has lead its developers down a path of wicked and corrupt habits. Gosh&#8230;why should I create a new class when I can just inject a few unrelated wacky ass methods to the String class? Is it laziness? Could it be that Ruby has given way and lowered the barrier to entry for people who shouldn&#8217;t be programming at all? Or perhaps it is a lack of classic OO programming training?</p>
<h3>Conclusion</h3>
<p>Draw your own conclusions. The major takeaway for me is that I&#8217;m sick of it; or rather I&#8217;m sickened by it. Unwavering I will remain steadfast on an ever-quest to right the wrong where ever my fingertips land regarding this matter. You should too. There are some great code katas that focus on this very problem set. Give the <a title="Gilded Rose" href="https://github.com/professor/GildedRose" target="_blank">Gilded Rose kata</a> a whirl. You might be surprised by the harshness of my befuddlement and/or the generalizations I&#8217;ve made about the Ruby community. Don&#8217;t get me wrong. There are many super intelligent people out there that swear by Ruby, who are doing the right thing, and you know who you are. I am just saying based on my observations and experiences there are many more people doing the wrong thing, specifically in the data access realm, that outweigh the right-doers. But hey that&#8217;s the case with most things in life, isn&#8217;t it?</p>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/08/fat-chunky-ugly-bloated-models/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Implementing Ruby&#8217;s inject method</title>
		<link>http://techwhizbang.com/2011/07/implementing-rubys-inject-method/</link>
		<comments>http://techwhizbang.com/2011/07/implementing-rubys-inject-method/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 06:37:34 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=443</guid>
		<description><![CDATA[I spent a little time tinkering with some of the &#8220;newer&#8221; RSpec 2 features while re-implementing the inject method that belongs to the Enumerable module. It is a fun and simple exercise because it reinforces the understanding of blocks and the sometimes confusing nature of the inject method. Here it is. &#160; require 'rubygems' require [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a little time tinkering with some of the &#8220;newer&#8221; RSpec 2 features while re-implementing the inject method that belongs to the Enumerable module. It is a fun and simple exercise because it reinforces the understanding of blocks and the sometimes confusing nature of the inject method. Here it is.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rspec'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#CC00FF; font-weight:bold;">Enumerable</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> inject<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>args.<span style="color:#9900CC;">size</span> == <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      inject_binary_operation<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span><span style="color:#006600; font-weight:bold;">&#40;</span>args.<span style="color:#9900CC;">size</span> == <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> !block_given?<span style="color:#006600; font-weight:bold;">&#41;</span>
      inject_binary_operation<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">first</span>, args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">drop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      the_memo = args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">first</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> the_memo = <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>the_memo, item<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given? <span style="color:#006600; font-weight:bold;">&#125;</span>
      the_memo
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#ff3333; font-weight:bold;">:reduce</span> <span style="color:#ff3333; font-weight:bold;">:inject</span>
&nbsp;
  private
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> inject_binary_operation<span style="color:#006600; font-weight:bold;">&#40;</span>the_memo, operator, enum<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">TypeError</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{operator} is not a symbol&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> operator.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Symbol</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    enum.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> the_memo = the_memo.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>operator, item<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    the_memo
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
shared_examples_for <span style="color:#996600;">&quot;an Enumerable object&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  describe <span style="color:#996600;">&quot;inject&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
    describe <span style="color:#996600;">&quot;given there is a specified memo value&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
      it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should return the newly injected &quot;memo&quot; value if using a block'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>memo, enum_item<span style="color:#006600; font-weight:bold;">|</span> memo.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>enum_item<span style="color:#006600; font-weight:bold;">&#41;</span> ? memo : memo <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> enum_item <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">1</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      describe <span style="color:#996600;">&quot;when it is a binary operation&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
        it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should return a TypeError because the single argument is not a Symbol'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">should</span> raise_error<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">TypeError</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should add the values'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span>, :<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">8</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should multiply the values'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span>, :<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">12</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should subtract the values'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span>, :<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">4</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    describe <span style="color:#996600;">&quot;given there isn't a specified memo value&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
      it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should return the newly injected &quot;memo&quot; value if using a block'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        enumerable.<span style="color:#9900CC;">inject</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>memo, enum_item<span style="color:#006600; font-weight:bold;">|</span> memo <span style="color:#006600; font-weight:bold;">&gt;</span> enum_item ? memo : enum_item <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">3</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      describe <span style="color:#996600;">&quot;when it is a binary operation&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
        it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should add the values'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">6</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should multiply the values'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">6</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        it<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'should subtract the values'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          enumerable.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">4</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>  
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
describe <span style="color:#CC00FF; font-weight:bold;">Range</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  it_should_behave_like <span style="color:#996600;">&quot;an Enumerable object&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    let<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:enumerable</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
describe <span style="color:#CC0066; font-weight:bold;">Array</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  it_should_behave_like <span style="color:#996600;">&quot;an Enumerable object&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    let<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:enumerable</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I especially like the RSpec documentation format because it provides such great readability. When appropriate the &#8220;shared_examples_for&#8221; is also nice to keep your test suite DRY.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rspec injectable.<span style="color:#9900CC;">rb</span> <span style="color:#006600; font-weight:bold;">--</span><span style="color:#CC0066; font-weight:bold;">format</span> documentation
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Range</span>
  it should behave like an <span style="color:#CC00FF; font-weight:bold;">Enumerable</span> object
    inject
      given there is a specified memo value
        should <span style="color:#0000FF; font-weight:bold;">return</span> the newly injected <span style="color:#996600;">&quot;memo&quot;</span> value <span style="color:#9966CC; font-weight:bold;">if</span> using a block
        <span style="color:#9966CC; font-weight:bold;">when</span> it is a binary operation
          should <span style="color:#0000FF; font-weight:bold;">return</span> a <span style="color:#CC00FF; font-weight:bold;">TypeError</span> because the single argument is <span style="color:#9966CC; font-weight:bold;">not</span> a <span style="color:#CC00FF; font-weight:bold;">Symbol</span>
          should add the values
          should multiply the values
          should subtract the values
      given there isn<span style="color:#996600;">'t a specified memo value
        should return the newly injected &quot;memo&quot; value if using a block
        when it is a binary operation
          should add the values
          should multiply the values
          should subtract the values
&nbsp;
Array
  it should behave like an Enumerable object
    inject
      given there is a specified memo value
        should return the newly injected &quot;memo&quot; value if using a block
        when it is a binary operation
          should return a TypeError because the single argument is not a Symbol
          should add the values
          should multiply the values
          should subtract the values
      given there isn'</span>t a specified memo value
        should <span style="color:#0000FF; font-weight:bold;">return</span> the newly injected <span style="color:#996600;">&quot;memo&quot;</span> value <span style="color:#9966CC; font-weight:bold;">if</span> using a block
        <span style="color:#9966CC; font-weight:bold;">when</span> it is a binary operation
          should add the values
          should multiply the values
          should subtract the values
&nbsp;
Finished <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006666;">0.007</span> seconds
<span style="color:#006666;">18</span> examples, <span style="color:#006666;">0</span> failures</pre></div></div>

<p>And of course the gist&#8230;<br />
<a href="https://gist.github.com/1096650" target="_blank">https://gist.github.com/1096650</a></p>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/07/implementing-rubys-inject-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript</title>
		<link>http://techwhizbang.com/2011/05/object-oriented-javascript/</link>
		<comments>http://techwhizbang.com/2011/05/object-oriented-javascript/#comments</comments>
		<pubDate>Wed, 11 May 2011 07:10:39 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[object oriented]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=428</guid>
		<description><![CDATA[This is a presentation I gave recently on Object Oriented JavaScript. There is also source code that goes along with it that can be found here: https://github.com/techwhizbang/js_techtalk Object Oriented JavaScript View more presentations from techwhizbang]]></description>
			<content:encoded><![CDATA[<p>This is a presentation I gave recently on Object Oriented JavaScript. There is also source code that goes along with it that can be found here: <a href="https://github.com/techwhizbang/js_techtalk">https://github.com/techwhizbang/js_techtalk</a></p>
<div style="width:425px" id="__ss_7919789"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/techwhizbang/object-oriented-javascript-7919789" title="Object Oriented JavaScript">Object Oriented JavaScript</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/7919789" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/techwhizbang">techwhizbang</a> </div>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/05/object-oriented-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript MVC Pattern</title>
		<link>http://techwhizbang.com/2011/04/javascript-mvc-pattern/</link>
		<comments>http://techwhizbang.com/2011/04/javascript-mvc-pattern/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 21:46:06 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=395</guid>
		<description><![CDATA[There are a lot of excellent JavaScript frameworks to choose from that provide some sort of MVC pattern. Of the most popular frameworks under this broad umbrella include Backbone.js, Sencha ExtJS, and SproutCore. All of these frameworks have their strengths, but I really crave simplicity. I really don&#8217;t want to have to read about a [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of excellent JavaScript frameworks to choose from that provide some sort of MVC pattern. Of the most popular frameworks under this broad umbrella include Backbone.js, Sencha ExtJS, and SproutCore. All of these frameworks have their strengths, but I really crave simplicity. I really don&#8217;t want to have to read about a framework for a couple hours and fiddle with it before I become productive. I really don&#8217;t want to scrap all of the &#8220;legacy&#8221; JavaScript written and attempt to shoehorn the framework into my project. I really don&#8217;t need all those super fancy UI widgets. You might be thinking the same thing, but you recognize that the way you&#8217;ve been organizing and/or writing JavaScript is flawed. The biggest mistake I see again and again is that the patterns we adhere to when writing in our favorite server side language are thrown to the wind when using JavaScript. Why? Why? Why? I could speculate and throw my theories out there, but none of that really matters. The thing that matters most is that you have recognized that something is wrong and you&#8217;d like to correct it.</p>
<p>I&#8217;d like to offer up a pattern I follow when writing JavaScript.<br />
Let&#8217;s take a look at the JavaScript MVC pattern I follow.</p>
<p>First things first. In your JavaScript directory, create the following folders.</p>
<ul>
<li>controllers</li>
<li>models</li>
<li>views</li>
<li>handlers</li>
<li>pages</li>
</ul>
<p>Handlers? Pages? What? No worries. I will discuss each of these and of course you can fit and mold this pattern to your needs. My examples assume you&#8217;re using <a href="http://jquery.com/" target="_blank">jQuery</a>, but you could use <a href="http://mootools.net/" target="_blank">MooTools</a>, <a href="http://www.prototypejs.org/" target="_blank">prototype</a>, or none of them.</p>
<h3>Models:</h3>
<p>As an example lets say you have some data that represents <em>projects</em>. Create a file under your models directory called <em>project.js</em>. I like to do &#8220;Object-Oriented&#8221; JavaScript so maybe your project.js would look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Project <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>projectId<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;">projectId</span> <span style="color: #339933;">=</span> projectId<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Project.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">find</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>projectId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'projects/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">projectId</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'.json'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//do whatever with your data</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;d also like to point out that I often like to write the JSON or XML I need (when I can) to the page so that I don&#8217;t make superfluous network calls. In this example I created a function called <em>find</em> that makes an Ajax call, but it could be just as simple to read JSON from a hidden element with a custom attribute called &#8220;data&#8221; on it like so.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
* Reads JSON from a hidden element on the page with an attribute called 'data'
* Example: &lt;input type=&quot;hidden&quot; /&gt;
*/</span>
&nbsp;
Project.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">all</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dataSelector<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">parseJSON</span><span style="color: #009900;">&#40;</span>dataSelector.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>My philosophy on JavaScript models is to keep them centered on the retrieval of the data they represent. Nothing more, nothing less.</p>
<h3>Views:</h3>
<p>This is the area where I feel most people go wrong and is generally my biggest gripe. How many times have you cracked open a .js file and found N-instances of the selector flying all over the code. Let me clarify. For example, you have a project carousel widget on your site and the selector has an id, namely <em>projects</em>. So everywhere, you look</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#projects'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">something</span></pre></div></div>

<p>is recklessly abundant all throughout the code. Good luck re-factoring or repurposing that, even _with_ tests, especially after a few months.</p>
<p>Now, had you captured all the selectors for your <em>projects</em> view in a file managed under the views directory and properly added a layer of encapsulation. It would have taken you 2 seconds to swap whatever existing selectors there were with a new one or perhaps extended your projects view. Sometimes I like to use a view factory to encapsulate my views. Again, you don&#8217;t have to use the factory pattern you could just run the prototype pattern directly. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ProjectViewFactory <span style="color: #339933;">=</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>
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> BaseProjectView<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: #000066;">name</span> <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;baseProjectView&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  BaseProjectView.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getProjectDataSelector</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> <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#projects'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> CarouselProjectView<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: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;carouselProjectView&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #006600; font-style: italic;">//I hand rolled a simple inheritance mechanism, you can find it under my Github account as inheritance.js</span>
  CarouselProjectView.<span style="color: #660066;">inheritsFrom</span><span style="color: #009900;">&#40;</span>BaseProjectView<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  CarouselProjectView.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getProjectDataSelector</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> <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#carouselProjects'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  CarouselProjectView.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getNextSelector</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> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getProjectDataSelector</span>.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.next'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  CarouselProjectView.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getPreviousSelector</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> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getProjectDataSelector</span>.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.previous'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
    getCarouselProjectView<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> <span style="color: #003366; font-weight: bold;">new</span> CarouselProjectView<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;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The fundamental concept is to align your view widgets on a page to a file managed under the views directory. Anytime, you use a selector it better be in a file like this. Never leak a selector outside the views directory. It is a very simple and straightforward rule to remember.</p>
<h3>Controllers:</h3>
<p>I like to keep my controllers slim and trim akin to the principle used very often in the Rails community.<br />
Your controllers should be passed the appropriate views required to activate and listen for the appropriate binding events (ie click, mouseover, etc). The controller should only bind the events and then hand the rest of the actual duties off to what I call a <i>handler</i> (we&#8217;ll discuss handlers next). Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ProjectsController <span style="color: #339933;">=</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>
&nbsp;
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   bindEvents <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>projectsView<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> projectModel <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Project<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> projectHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ProjectHandler<span style="color: #009900;">&#40;</span>projectsView<span style="color: #339933;">,</span> projectModel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     projectsView.<span style="color: #660066;">getNextSelector</span>.<span style="color: #660066;">click</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>
       projectHandler.<span style="color: #660066;">updateToNext</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;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     projects.<span style="color: #660066;">getPreviousSelector</span>.<span style="color: #660066;">click</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>
       projectHandler.<span style="color: #660066;">updateToPrevious</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;">&#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: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Handlers:</h3>
<p>I&#8217;d like to introduce one more letter in the MVC acronym, &#8220;H&#8221;. A handler is in charge of the dirty work bound to the event triggered. So you can imagine that with a project carousel with a next and previous button that several different things might happen like an image transition, description, numbering, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ProjectHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>projectView<span style="color: #339933;">,</span> projectModel<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;">projectView</span> <span style="color: #339933;">=</span> projectView<span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">projectModel</span> <span style="color: #339933;">=</span> projectModel<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
ProjectHandler.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">updateToPrevious</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>
  <span style="color: #006600; font-style: italic;">// fill in the details here</span>
<span style="color: #009900;">&#125;</span>
ProjectHandler.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">updateToNext</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>
  <span style="color: #006600; font-style: italic;">// fill in the details here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Pages:</h3>
<p>The last and final piece of the puzzle. So a page may consist of many different things including a project carousel, recommendations, comments, and whatever else you can dream up. Assuming you followed the rules given above that means you&#8217;ve developed a projects, recommendations, and comments controller along with their corresponding views, models, and handlers.<br />
So a <i>page</i> might look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">SomeAwesomePage <span style="color: #339933;">=</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>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
    bindEvents<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: #006600; font-style: italic;">//initialize the views needed here</span>
      ProjectsController.<span style="color: #660066;">bindEvents</span><span style="color: #009900;">&#40;</span>projectsView<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      RecommendationsController.<span style="color: #660066;">bindEvents</span><span style="color: #009900;">&#40;</span>recommendationsView<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      CommentsController.<span style="color: #660066;">bindEvents</span><span style="color: #009900;">&#40;</span>commentsView<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: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Following this pattern lends itself to a very flexible and modular design. It is totally agnostic to other frameworks that you might already be using. The beauty part of this approach is that you can take a pure JavaScript approach without the bulkiness or dependency on yet another pre-defined framework and spin it to the needs of your project. I always encourage testing JavaScript. Recently I&#8217;ve been pairing this pattern with Jasmine.</p>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/04/javascript-mvc-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript Testing with Jasmine</title>
		<link>http://techwhizbang.com/2011/02/javascript-testing-with-jasmine/</link>
		<comments>http://techwhizbang.com/2011/02/javascript-testing-with-jasmine/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 01:35:51 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[jasmine]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=385</guid>
		<description><![CDATA[This is a presentation I gave on the use of Jasmine, a BDD testing framework for JavaScript. Examples of Jasmine specs can be found here: https://github.com/techwhizbang/js_techtalk. Have a look at my presentation on Object Oriented JavaScript also. Testing JavaScript with Jasmine View more presentations from techwhizbang.]]></description>
			<content:encoded><![CDATA[<p>This is a presentation I gave on the use of Jasmine, a BDD testing framework for JavaScript. Examples of Jasmine specs can be found here: <a href="https://github.com/techwhizbang/js_techtalk">https://github.com/techwhizbang/js_techtalk</a>.<br />
Have a look at my presentation on <a href="http://techwhizbang.com/2011/05/object-oriented-javascript/">Object Oriented JavaScript</a> also.</p>
<div style="width:425px" id="__ss_7037839"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/techwhizbang/testing-javascript-with-jasmine" title="Testing JavaScript with Jasmine">Testing JavaScript with Jasmine</a></strong><object id="__sse7037839" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=unittestingjavascript-110223193423-phpapp02&#038;stripped_title=testing-javascript-with-jasmine&#038;userName=techwhizbang" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse7037839" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=unittestingjavascript-110223193423-phpapp02&#038;stripped_title=testing-javascript-with-jasmine&#038;userName=techwhizbang" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/techwhizbang">techwhizbang</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/02/javascript-testing-with-jasmine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB Presentation Part 2</title>
		<link>http://techwhizbang.com/2011/01/mongodb-part-2/</link>
		<comments>http://techwhizbang.com/2011/01/mongodb-part-2/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 21:31:18 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[nosql]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=373</guid>
		<description><![CDATA[MongoDB Part 2 View more presentations from techwhizbang.]]></description>
			<content:encoded><![CDATA[<div style="width:425px" id="__ss_6631050"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/techwhizbang/mongodb-part-2" title="MongoDB Part 2">MongoDB Part 2</a></strong><object id="__sse6631050" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mongodbpartii-110119152914-phpapp01&#038;stripped_title=mongodb-part-2&#038;userName=techwhizbang" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse6631050" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mongodbpartii-110119152914-phpapp01&#038;stripped_title=mongodb-part-2&#038;userName=techwhizbang" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/techwhizbang">techwhizbang</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/01/mongodb-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intro to MongoDB Presentation</title>
		<link>http://techwhizbang.com/2011/01/intro-to-mongodb-presentation/</link>
		<comments>http://techwhizbang.com/2011/01/intro-to-mongodb-presentation/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 05:38:59 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[nosql]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=366</guid>
		<description><![CDATA[MongoDB View more presentations from techwhizbang. Here is the second part of the presentation I gave: http://techwhizbang.com/2011/01/mongodb-part-2/]]></description>
			<content:encoded><![CDATA[<div style="width:425px" id="__ss_6463802"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/techwhizbang/mongodb-6463802" title="MongoDB">MongoDB</a></strong><object id="__sse6463802" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mongodb-110105233629-phpapp02&#038;stripped_title=mongodb-6463802&#038;userName=techwhizbang" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse6463802" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mongodb-110105233629-phpapp02&#038;stripped_title=mongodb-6463802&#038;userName=techwhizbang" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/techwhizbang">techwhizbang</a>.</div>
</div>
<p>Here is the second part of the presentation I gave:<br />
<a href="http://techwhizbang.com/2011/01/mongodb-part-2/">http://techwhizbang.com/2011/01/mongodb-part-2/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2011/01/intro-to-mongodb-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The JVM is the future</title>
		<link>http://techwhizbang.com/2010/12/the-jvm-is-the-future/</link>
		<comments>http://techwhizbang.com/2010/12/the-jvm-is-the-future/#comments</comments>
		<pubDate>Mon, 27 Dec 2010 22:59:51 +0000</pubDate>
		<dc:creator>techwhizbang</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://techwhizbang.com/?p=358</guid>
		<description><![CDATA[Amongst the new stuff coming with JDK 7, I think one of the most exciting additions to come along is JSR 292. JSR 292 is all about support for dynamic languages. Wait, what? Yes, Java is finally &#8220;yielding&#8221; to other languages (pun intended). The popularity and success of JRuby, Jython, Clojure, and all the other [...]]]></description>
			<content:encoded><![CDATA[<p>Amongst the new stuff coming with JDK 7, I think one of the most  exciting additions to come along is JSR 292. JSR 292 is all about  support for dynamic languages. Wait, what? Yes, Java is finally  &#8220;yielding&#8221; to other languages (pun intended). The popularity and success  of JRuby, Jython, Clojure, and all the other dynamic JVM languages have  become too hard to ignore.</p>
<p>It is sort of crazy to think that the  JVM has been overlooked and misunderstood by so many for so long. Most  developers think the JVM is a platform/OS agnostic place where <strong>Java</strong> byte code gets executed. Partly true. It is platform agnostic. Funny  thing is the JVM doesn&#8217;t know a damn thing about the Java syntax as we  developers know it, all it knows about is the specific JVM byte code  format it knows how to interpret. So this means if you can &#8220;compile&#8221; a  .rb or .clj file and if it outputs the expected JVM class byte code format,  you&#8217;re ready to rock. This ought to dispel some of the perceived magic involved in getting other languages running on the JVM. Just stop  and consider what an amazing forethought that was, or was it?  In any case, brilliant.</p>
<p>You might be wondering why this JSR is even needed if these  dynamic languages are already running on the JVM? The truth is the  implementers of JRuby, Clojure, etc have gone through great lengths and  round-about ways to make them work. Some would even call these solutions  creative and innovative hacks. Personally, I am overjoyed that people  are thinking out of the box and have pushed the JVM to new horizons and  given it new life. Here is interesting article that I found that answers  these types of questions.</p>
<p><a href="http://java.sun.com/developer/technicalArticles/DynTypeLang/index.html" target="_blank">http://java.sun.com/developer/technicalArticles/DynTypeLang/index.html</a></p>
<p>This JSR is part of a bigger and broader vision of the JVM known as the &#8220;Da Vinci Machine Project&#8221;.</p>
<p><a href="http://openjdk.java.net/projects/mlvm/" target="_blank">http://openjdk.java.net/projects/mlvm/</a></p>
<p>Most  developers know that the Java language is dated, it&#8217;s old news, but it  seems many have a hard time coming to grips with it. The JVM is the future, not the Java language. But wait, what  about the argument of Java&#8217;s excellent performance, what about all the great  libraries and tools that have been built over the years? We need to embrace the good things, but move on to greener and more productive pastures. The beauty part of the new JVM languages is that you can intermix and mingle Java libraries with your choice of language. Check the facts, some of the &#8220;newer&#8221; languages to run on the JVM are on par with Java or close to it.</p>
<p>Evolve and rise above so as not to become stoic boilerplate funk. The JVM is the future where all languages may frolic about.</p>
]]></content:encoded>
			<wfw:commentRss>http://techwhizbang.com/2010/12/the-jvm-is-the-future/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.766 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-06 15:01:02 -->

