<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>app</title>
  <id>http://127.0.0.1</id>
  <updated>2011-08-22</updated>
  <author>
    <name/>
  </author>
  <entry>
    <title>Understanding String.match</title>
    <link rel="alternate" href="http://127.0.0.1/2012/10/28/understanding-stringmatch/"/>
    <id>http://127.0.0.1/2012/10/28/understanding-stringmatch/</id>
    <published>2012-10-28</published>
    <updated>2012-10-28</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;p&gt;&lt;code&gt;"string".match()&lt;/code&gt; is often a misunderstood or neglected part of JavaScript. I want to bring some focus on this neat tool and talk about some of its usage. The most common use of this method is to check if a string follows a particular pattern, or includes some bit of text:&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;&lt;code&gt;"string".match()&lt;/code&gt; is often a misunderstood or neglected part of JavaScript. I want to bring some focus on this neat tool and talk about some of its usage. The most common use of this method is to check if a string follows a particular pattern, or includes some bit of text:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var myDomain = document.domain; // (on this site, this is www.natehunzaker.com)

if ( myDomain.match("natehunzaker.com") ) {
    console.log("We must be at the correct domain");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Dead simple, but this doesn&amp;rsquo;t do this method justice. In addition string comparision, &lt;code&gt;.match()&lt;/code&gt; also can utilize regular expressions. When used without the global option (&lt;code&gt;/reg/g&lt;/code&gt;) &lt;code&gt;.match()&lt;/code&gt; can extract additional meaning out of a statement simply by wrapping a rule within parenthesis. These are known as &lt;a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions#special-capturing-parentheses"&gt;capturing parenthesis&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var myString = "Hello, world! My name is Nate Hunzaker!";
var name = myString.match(/My name is (.*) (.*)!/);

console.log( name[1] ) //=&amp;gt; Nate
console.log( name[2] ) //=&amp;gt; Hunzaker
console.log( name )    //=&amp;gt; ['My Name is Nate Hunzaker!', 'Nate', 'Hunzaker']
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Pretty neat. This is particularly useful for extracting patterns of data out of bodies of text. For instance, a while back I needed to map some attributes an object based upon a key/value system within a large body of text:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/*
Well assume the following content has been saved to the "story" variable:

TITLE: The War of the Worlds
AUTHOR: H.G. Wells
BODY: No one would have believed in the last years of the nineteenth century that this world was being watched keenly and closely by intelligences...

*/

var pattern = "(.*)\\:(.*)\n";
var lines = story.match( new RegExp(pattern, "g") );
var data = {};

lines.forEach(function(line) {
    var match = line.match( new RegExp(pattern) );

    if (match) {
        var key = match[1].toLowerCase();
        var val = match[2].trim();
        data[key] = val;
    }
});

console.log(data.title);  //=&amp;gt; "The War of the Worlds"
console.log(data.author); //=&amp;gt; "H.G. Wells
console.log(data.body);   //=&amp;gt; "No one would have believed..."
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately JavaScript lacks Ruby&amp;rsquo;s awesome &lt;a href="http://www.ruby-doc.org/core-1.9.3/String.html#method-i-scan"&gt;&lt;code&gt;String.scan()&lt;/code&gt; method&lt;/a&gt;, but we can make due by first finding all of the matches globally and then parsing each line individually. Generally speaking I have found this to read better and write much cleaner than cruder methods such as &lt;code&gt;.split()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/3969319"&gt;View the gist here&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>A Deeper Understanding of JavaScript Statements</title>
    <link rel="alternate" href="http://127.0.0.1/2012/09/25/a-deeper-understanding-of-javascript-statements/"/>
    <id>http://127.0.0.1/2012/09/25/a-deeper-understanding-of-javascript-statements/</id>
    <published>2012-09-25</published>
    <updated>2012-09-25</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;p&gt;One of the unfortunate observations I have made about the JavaScript community is a very poor understanding of fundamentals of the language. I am no exception to this rule, and as a part of my quest to develop mastery of the language I plan to document my explorations in a number of short articles&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;One of the unfortunate observations I have made about the JavaScript community is a very poor understanding of fundamentals of the language. I am no exception to this rule, and as a part of my quest to develop mastery of the language I plan to document my explorations in a number of short articles.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s take a look at statements, one of the essential building blocks of JavaScript. Statements are executable commands that ultimately define what our program does.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// A declaration statement
var myStatement = "Wow, that was easy";
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Nothing scary here. However you may not be familiar with the raw syntax for a multi-line statement:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Yes, this is executable code, give it a spin
{
    var anotherStatement = "Wait.. what's going on here?";
    console.log("This looks like an object declaration!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you may have gathered, we aren&amp;rsquo;t declaring an object. Rather, we are using the syntax for a &lt;em&gt;&lt;em&gt;compound statement&lt;/em&gt;&lt;/em&gt; &amp;mdash; which is strikingly similar. This pattern is core to JavaScript, and we see it in just about every other aspect of the language. One such example is the &lt;code&gt;if&lt;/code&gt; statement:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if (condition) {
    console.log("We did something");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The code above reads &amp;ldquo;If condition is true, then run the contents of this statement&amp;rdquo;. Remember that the code we placed inside the curly braces acts the exact same way as the single declaration of &lt;code&gt;myStatement&lt;/code&gt; earlier. For this same, reason, we can simplify this code (although some would argue this is bad form):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if (condition) console.log("We did something");
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Same rules apply: if the condition is true, run this statement. This is nothing profound, however it provides an explanation for why curly braces are not required for if statements, loops, and other flow control. Effectively, &lt;em&gt;they perform the same role&lt;/em&gt;. For this same reason, we can have some more fun:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Remember, while (condition is true) [statement] ...
var a = 0;
var keepGoing = true;

while (keepGoing) if (a &amp;lt; 10) a++; else {
    console.log(a);
    keepGoing = false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Obviously, this is &lt;em&gt;&lt;strong&gt;insane&lt;/strong&gt;&lt;/em&gt;, and it should never be used for anything other than experimental purposes. Having said that, I would encourage you to play around with examples such as this as they are excellent for solidifying these concepts.&lt;/p&gt;

&lt;p&gt;By learning more about the specifics of statements and how they are executed writing flexible code becomes much easier. This is a major step towards separating oneself from dependency on libraries such as jQuery and it helps to build a greater appreciation for what these tools do. It exposes the expressiveness of JavaScript and turns it into an exceptionally powerful language.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Taking Advantage of JavaScript's Flexible Ternary Operator</title>
    <link rel="alternate" href="http://127.0.0.1/2012/02/27/taking-advantage-of-javascripts-flexible-ternary-operator/"/>
    <id>http://127.0.0.1/2012/02/27/taking-advantage-of-javascripts-flexible-ternary-operator/</id>
    <published>2012-02-27</published>
    <updated>2012-02-27</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;p&gt;Let&amp;rsquo;s face it, there are some parts of Javascript that simply aren&amp;rsquo;t pretty
to look at. At times I look at what my colleagues are doing in&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Let&amp;rsquo;s face it, there are some parts of Javascript that simply aren&amp;rsquo;t pretty
to look at. At times I look at what my colleagues are doing in
Ruby and it leaves me in complete envy. Yet there exists the potential
to write beautiful Javascript if given proper attention.&lt;/p&gt;

&lt;p&gt;One of the things I love in Ruby are the &lt;code&gt;*.truthy?&lt;/code&gt; methods such as
&lt;code&gt;*.empty?&lt;/code&gt;. Let&amp;rsquo;s try to create something similar in Javascript. First
let&amp;rsquo;s start off with a commonly implemented Javascript ternary:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var criminal = (suspects.length &amp;gt; 0) ? suspects[0] : "noone"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Nothing new, we&amp;rsquo;ve all seen this. But this is a little too heavy for
my taste. What if we dropped the parentheses?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var criminal = suspects.length &amp;gt; 0 ? suspects[0] : "noone"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We&amp;rsquo;re getting there. Now let&amp;rsquo;s make things a little more concise&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var criminal = suspects.length ? suspects[0] : "noone"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Remember that if &lt;code&gt;suspects&lt;/code&gt; is empty, &lt;code&gt;suspects.length&lt;/code&gt; will return 0.
Further, since we have not specified &lt;code&gt;===&lt;/code&gt;, the ternary will use type
coercion in the conditional. Naturally, &lt;code&gt;suspects.length&lt;/code&gt; registers as falsy because 0 gets coerced.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s take this a small step further by removing the whitespace after &lt;code&gt;.length&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var criminal = suspects.length? suspects[0] : "noone";
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Cool, I think the final example is significantly more straightforward
and definitely less syntax heavy than where we started. It&amp;rsquo;s actually is pretty similar Ruby:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Ruby
criminal = suspects.length ? suspects[0] : "noone"

// Javascript
var criminal = suspects.length? suspects[0] : "noone";
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the space after .length in the first example is required
because &lt;code&gt;?&lt;/code&gt; is a valid symbol in Ruby. I also find this to be
particularly useful when creating classy functions where plain english
style logic can be a nice addition:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function Cat(claws) {
    this.claws = claws;
}

var kitty = new cat();

console.log( kitty.claws? "Ouch!" : "A-OK" );
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;No doubt, there are a handful of ways to write this. However I think
the form deserves some more recognition. Further, understanding type
coercion is important step in learning Javascript and this
offers a great example of taking advantage of it to improve code readibility.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Fun with FIGlet</title>
    <link rel="alternate" href="http://127.0.0.1/2012/02/19/fun-with-figlet/"/>
    <id>http://127.0.0.1/2012/02/19/fun-with-figlet/</id>
    <published>2012-02-19</published>
    <updated>2012-02-19</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;p&gt;&lt;a href="http://www.figlet.org/"&gt;FIGlet&lt;/a&gt; is an awesome tool for generating
text-based ascii art. I discovered it a few months go when&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;&lt;a href="http://www.figlet.org/"&gt;FIGlet&lt;/a&gt; is an awesome tool for generating
text-based ascii art. I discovered it a few months go when
&lt;a href="http://scottgonzalez.com/"&gt;Scott Gonzalez&lt;/a&gt; showed me his wonderful
&lt;a href="https://github.com/scottgonzalez/figlet-js"&gt;FIGlet parser written in Javascript&lt;/a&gt;.
It&amp;rsquo;s a perfect example of code which is usable both in the browser and server.&lt;/p&gt;

&lt;p&gt;I love the results, but I will say that finding the right font can
take time. Some text simply doesn&amp;rsquo;t render well with particular fonts,
and constantly flipping back and forth between Emacs and the terminal
(even the built in terminal) is a total killjoy.&lt;/p&gt;

&lt;h3&gt;Did I mention trial and error sucks?&lt;/h3&gt;

&lt;p&gt;Well I&amp;rsquo;m not one for one for blind repetition, so I&amp;rsquo;ve created a &lt;a href="http://figlet.nodejitsu.com"&gt;simple web service&lt;/a&gt;
to both convert text into FIGlet art and browse all the available
options. All one has to do is specify the text and font they&amp;rsquo;d like to
use (&lt;code&gt;?text=Foobar&amp;amp;font=standard&lt;/code&gt;), or &lt;a href="http:figlet.nodejitsu.com/browse"&gt;browse a gallery of all fonts&lt;/a&gt;, which also takes similar query parameters.&lt;/p&gt;

&lt;p&gt;&lt;figure class="center"&gt;
&lt;a href="http://figlet.nodejitsu.com"&gt;&lt;img src="/images/articles/figlet.jpg" alt="figlet.nodejitsu.com" /&gt;&lt;/a&gt;
&lt;/figure&gt;&lt;/p&gt;

&lt;h3&gt;More than just ascii art&lt;/h3&gt;

&lt;p&gt;Perhaps just as superficial, but FIGlet can be used to do some other
cool stuff. What if you wanted to convert text into binary data, or
morse code?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;01100101 01110110 01100101 01101110  01100010 01101001 01101110 01100001 01110010 01111001  01100100 01100001 01110100 01100001&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;. ...- . -.    -... .. -. .- .-. -.--    -.. .- - .-&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
                                __           __                   __            _    
                               /\ \__       /\ \                 /\ \         /'_`\  
      ___       __      __     \ \ ,_\      \ \ \___     __  __  \ \ \___    /\_\/\`\
    /' _ `\   /'__`\  /'__`\    \ \ \/       \ \  _ `\  /\ \/\ \  \ \  _ `\  \/_//'/'
    /\ \/\ \ /\  __/ /\ \L\.\_   \ \ \_  __   \ \ \ \ \ \ \ \_\ \  \ \ \ \ \    /\_\ 
    \ \_\ \_\\ \____\\ \__/.\_\   \ \__\/\ \   \ \_\ \_\ \ \____/   \ \_\ \_\   \/\_\
     \/_/\/_/ \/____/ \/__/\/_/    \/__/\ \/    \/_/\/_/  \/___/     \/_/\/_/    \/_/
                                         \/                                          
&lt;/code&gt;&lt;/pre&gt;

</content>
  </entry>
  <entry>
    <title>Socket.io with Flatiron</title>
    <link rel="alternate" href="http://127.0.0.1/2012/02/11/socketio-with-flatiron/"/>
    <id>http://127.0.0.1/2012/02/11/socketio-with-flatiron/</id>
    <published>2012-02-11</published>
    <updated>2012-02-11</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;p&gt;&lt;a href="http://flatironjs.org/"&gt;Flatiron&lt;/a&gt; is a framework maintained by the guys over at
&lt;a href="http://nodejitsu.com/"&gt;nodejitsu&lt;/a&gt;. It&amp;rsquo;s great, I&amp;rsquo;ve moved most of my pet node&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;&lt;a href="http://flatironjs.org/"&gt;Flatiron&lt;/a&gt; is a framework maintained by the guys over at
&lt;a href="http://nodejitsu.com/"&gt;nodejitsu&lt;/a&gt;. It&amp;rsquo;s great, I&amp;rsquo;ve moved most of my pet node
projects over to it. This has been mostly painless, however I will say
that hit some discomfort with configuring socket.io.&lt;/p&gt;

&lt;p&gt;In retrospect, it&amp;rsquo;s actually rather simple process. Let&amp;rsquo;s look at some code.&lt;/p&gt;

&lt;script src="https://gist.github.com/1805617.js?file=gistfile1.js"&gt;&lt;/script&gt;


&lt;p&gt;The confusion (in my case, anyway) occurs because the &lt;code&gt;server&lt;/code&gt; property
of &lt;code&gt;flatiron.app&lt;/code&gt; is only available after the &lt;code&gt;start&lt;/code&gt; function has
been explicitly called. This means that socket.io can only listen to
the flatiron server after you&amp;rsquo;ve told it to start (when the server
property is actually created).&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s okay if that sounds confusing, it tripped me up too. The solution
is rather simple: set up socket.io after the server has started, like
so:&lt;/p&gt;

&lt;script src="https://gist.github.com/1805766.js"&gt;&lt;/script&gt;


&lt;p&gt;I&amp;rsquo;ve submitted a related
&lt;a href="https://github.com/flatiron/flatiron/pull/22"&gt;pull request&lt;/a&gt; with a
good example of what the whole process looks like. Hopefully this will help eliminate further confusion.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Speakeasy, a natural language processor for node</title>
    <link rel="alternate" href="http://127.0.0.1/2012/02/04/speakeasy-a-natural-language-processor-for-node/"/>
    <id>http://127.0.0.1/2012/02/04/speakeasy-a-natural-language-processor-for-node/</id>
    <published>2012-02-04</published>
    <updated>2012-02-04</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;p&gt;I&amp;rsquo;ve been working lately on
&lt;a href="http://github.com/nhunzaker/nodebot"&gt;nodebot&lt;/a&gt;,
a helper robot written for node.js. It&amp;rsquo;s forced me to learn quite&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;I&amp;rsquo;ve been working lately on
&lt;a href="http://github.com/nhunzaker/nodebot"&gt;nodebot&lt;/a&gt;,
a helper robot written for node.js. It&amp;rsquo;s forced me to learn quite
a few new skills, one of which being natural language processing.&lt;/p&gt;

&lt;p&gt;This project has been in the works for quite a while now and it
suffers from a bit of bloat. To battle this I have begun to pull
elements of it into seperate modules. The first of which is
&lt;a href="http://github.com/nhunzaker/speakeasy"&gt;SpeakEasy&lt;/a&gt;, a natural language
processor.&lt;/p&gt;

&lt;p&gt;The aim of the project isn&amp;rsquo;t to be
&lt;a href="http://nltk.org"&gt;the Natural Language Toolkit&lt;/a&gt;, there is a
&lt;a href="https://github.com/NaturalNode/natural"&gt;wonderful project&lt;/a&gt; with this
goal.
However it does perform some rather useful task such as calculate sentiment and draw basic
meaning out of statements. Check out some sample code:&lt;/p&gt;

&lt;script src="https://gist.github.com/1740327.js?file=speakeasy_sample.js"&gt;&lt;/script&gt;


&lt;p&gt;Pretty neat eh? I have to admit it&amp;rsquo;s a ton of fun to play with. Take a look at &lt;a href="http://github.com/nhunzaker/nodebot"&gt;nodebot&lt;/a&gt;
or the new &lt;a href="http://twittermap.nodejitsu.com"&gt;twittermap&lt;/a&gt; for some nice examples of what&amp;rsquo;s posssible.&lt;/p&gt;

&lt;p&gt;Also checkout the &lt;a href="http://github.com/nhunzaker/speakeasy"&gt;project on Github&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Learning Backbone.js Chapter 1: A Quick Overview</title>
    <link rel="alternate" href="http://127.0.0.1/2011/11/02/learning-backbonejs-chapter-1-a-quick-overview/"/>
    <id>http://127.0.0.1/2011/11/02/learning-backbonejs-chapter-1-a-quick-overview/</id>
    <published>2011-11-02</published>
    <updated>2011-11-02</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;blockquote&gt;&lt;p&gt;I&amp;rsquo;ve found it difficult to find a guide for learning Backbone.js. They&amp;rsquo;re probably out there &amp;mdash; but it isn&amp;rsquo;t easy. &lt;strong&gt;This is the first in a series of articles dedicated to giving a complete overview of Backbone.js through simple examples and concise code.&lt;/strong&gt;&lt;/p&gt;&lt;/blockquote&gt;
</summary>
    <content type="html">&lt;blockquote&gt;&lt;p&gt;I&amp;rsquo;ve found it difficult to find a guide for learning Backbone.js. They&amp;rsquo;re probably out there &amp;mdash; but it isn&amp;rsquo;t easy. &lt;strong&gt;This is the first in a series of articles dedicated to giving a complete overview of Backbone.js through simple examples and concise code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Something of a &lt;strong&gt;cookbook&lt;/strong&gt;, you might say.&lt;/p&gt;&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3&gt;What you&amp;rsquo;ll need:&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="http://documentcloud.github.com/underscore"&gt;Underscore.js&lt;/a&gt;&lt;/strong&gt;: A required library for Backbone, it also adds over 60 functions for making Javascript development easier.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="http://documentcloud.github.com/backbone/" title=""&gt;Backbone.js&lt;/a&gt;&lt;/strong&gt;: The meat of it. This is what we will use for all the magic.&lt;/li&gt;
&lt;/ol&gt;


&lt;hr /&gt;

&lt;h3&gt;What we&amp;rsquo;re making&lt;/h3&gt;

&lt;p&gt;Today we&amp;rsquo;ll make a map which displays tweets via the Google Maps API and twitter search results (For the sake of simplicity, we won&amp;rsquo;t go in to the Twitter API yet). We&amp;rsquo;ll focus on fundamental construction using the &lt;strong&gt;Model&lt;/strong&gt;, &lt;strong&gt;Collection &lt;/strong&gt; and &lt;strong&gt;View&lt;/strong&gt; components of Backbone.&lt;/p&gt;

&lt;p&gt;&lt;figure class="align-center"&gt;
&lt;a href="/images/article_1/twittermap.png" class="lightbox"&gt;&lt;img src="/images/article_1/twittermap.png" title="The Architecture of Backbone" width="300" /&gt;&lt;/a&gt;
&lt;small&gt;The twitter map shows tweets around the world.&lt;/small&gt;
&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;Hopefully, by the end of this you will have a beautiful night sky lit up by tweets around North America. Let&amp;rsquo;s get started!&lt;/p&gt;

&lt;hr /&gt;

&lt;h3&gt;A brief introduction:&lt;/h3&gt;

&lt;p&gt;Backbone.js is used to &amp;ldquo;supply structure to Javascript-heavy applications&amp;rdquo;&lt;sub&gt;&lt;a href="#one"&gt;1&lt;/a&gt;&lt;/sub&gt;. It separates logic into &lt;strong&gt;Models&lt;/strong&gt;, &lt;strong&gt;Views&lt;/strong&gt;, &lt;strong&gt;Collections&lt;/strong&gt;, and &lt;strong&gt;Routers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;figure class="align-right"&gt;
&lt;img src="/images/article_1/Backbone_Arch.png" title="The Architecture of Backbone" /&gt;&lt;/a&gt;
&lt;small&gt;The architecture of Backbone.js&lt;/small&gt;
&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;A typical Backbone application will use &lt;strong&gt;models&lt;/strong&gt; to keep track of information, assisted by &lt;strong&gt;collections&lt;/strong&gt; (groups of data). &lt;strong&gt;Views&lt;/strong&gt; help display this information, by binding themselves to changes in data. &lt;strong&gt;Routers&lt;/strong&gt; help orchestrate all of this by providing the ability to navigate to various parts of an application seamlessly.&lt;/p&gt;

&lt;h3&gt;Backbone is Model Driven&lt;/h3&gt;

&lt;p&gt;At the core of all Backbone applications are models. Models, as you may have guessed, act as the base level of information. Since the purpose of our application is to display tweets, let&amp;rsquo;s create a model to define them here:&lt;/p&gt;

&lt;script src="https://gist.github.com/1326678.js"&gt; &lt;/script&gt;


&lt;p&gt;What just happened? First we created duplicate of the Backbone Model object. Next we instantiated it as a new Tweet within the &lt;strong&gt;myTweet&lt;/strong&gt; variable.&lt;/p&gt;

&lt;p&gt;Pretty simple. But let&amp;rsquo;s say we don&amp;rsquo;t want a carbon copy of the Backbone model? Perhaps we need to add custom methods to our model, or we want to define the same defaults for every instance of the Tweet model. Let&amp;rsquo;s create a new model which &lt;strong&gt;extends&lt;/strong&gt; the base Backbone.Model object:&lt;/p&gt;

&lt;script src="https://gist.github.com/1326714.js"&gt; &lt;/script&gt;


&lt;p&gt;I threw in another function here, &lt;strong&gt;.toJSON()&lt;/strong&gt;. For models, this will return a JSON representation of the model&amp;rsquo;s attributes. When used on collections, it will return an Array of JSON objects for each model it contains.&lt;/p&gt;

&lt;h3&gt;Working with Collections.&lt;/h3&gt;

&lt;p&gt;The Tweet model will work well for this application, however we&amp;rsquo;ll need to keep track of more than one of them. This is where &lt;strong&gt;collections&lt;/strong&gt; come in to play. Collections are designed to create, delete, and manage groups of models. Let&amp;rsquo;s create one here:&lt;/p&gt;

&lt;script src="https://gist.github.com/1327396.js"&gt; &lt;/script&gt;


&lt;p&gt;Noticing a trend? Here we first create a Tweet model. Then we create the TweetsCollection collection which references this model. Finally, we use the &lt;strong&gt;add&lt;/strong&gt; method of collections to create a new Tweet within with the collection.&lt;/p&gt;

&lt;h3&gt;Backbone objects have events&lt;/h3&gt;

&lt;p&gt;In addition to this, all backbone objects can respond to events. Say, for example, you wish to perform an action every time a new record is added to a collection. Consider the following:&lt;/p&gt;

&lt;script src="https://gist.github.com/1332280.js"&gt;&lt;/script&gt;


&lt;p&gt;Pretty simple huh? Backbone events are powerful, I would recommend doing some &lt;a href="http://documentcloud.github.com/backbone/#FAQ-events"&gt;reading&lt;/a&gt; on what events are available to models and collections. There are some really neat things you can do with them.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3&gt;Pulling it all Together&lt;/h3&gt;

&lt;p&gt;Now that we&amp;rsquo;ve established some form of a basic understanding, let&amp;rsquo;s get in to the application! First we need some initial construction:&lt;/p&gt;

&lt;script src="https://gist.github.com/1327424.js"&gt; &lt;/script&gt;


&lt;p&gt;Cool. Now in your &lt;strong&gt;js&lt;/strong&gt; folder, add a file called &amp;ldquo;map.js&amp;rdquo;. We&amp;rsquo;ll add all of the backbone code into here. From this point on we&amp;rsquo;ll pick up quite a bit of speed, but I&amp;rsquo;ll be sure to comment on everything new along the way.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s recycle some of the code made earlier, with some additions:&lt;/p&gt;

&lt;script src="https://gist.github.com/1327437.js"&gt;&lt;/script&gt;


&lt;p&gt;Overall not too bad, but there are some new elements here. &lt;strong&gt;url&lt;/strong&gt;, &lt;strong&gt;fetch&lt;/strong&gt;, &lt;strong&gt;parse&lt;/strong&gt;, and &lt;strong&gt;initialize&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;url&lt;/strong&gt;: Backbone is designed enable easy access to information on a server. When we set the &lt;code&gt;url&lt;/code&gt; property, we tell the collection where it can find information.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;fetch&lt;/strong&gt;: Once a url has been established, &lt;code&gt;fetch&lt;/code&gt; can be used to retrieve new information. In this case, we will request a json object from the Twitter search service.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;parse&lt;/strong&gt;: A handy function. Whenever the &lt;code&gt;fetch&lt;/code&gt; event is fired, parse enables you to preprocess data before it is stored in the collection. In example above, we use the underscore method &lt;code&gt;_.filter()&lt;/code&gt; &lt;sub&gt;&lt;a href="#two"&gt;2&lt;/a&gt;&lt;/sub&gt; to prevent any tweets that do not have a specific geolocation from being stored.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;initialize&lt;/strong&gt;: A staple of Backbone. Initialize is a method of each backbone object which will always run upon instantiation (in this case, the collection). Here we tell it to fetch new information from the server at an interval of 2 seconds.&lt;/li&gt;
&lt;/ul&gt;


&lt;hr /&gt;

&lt;h3&gt;Using Views&lt;/h3&gt;

&lt;p&gt;In order to display these tweets, we&amp;rsquo;ll need to create a view for the map. As said earlier, views are the presentational element associated with Backbone; we&amp;rsquo;ll use them to display content from our collection. I think views are best taught through example, so let&amp;rsquo;s jump right in to it. Take a close look at the preceding code and add it right after your model and collection code&lt;/p&gt;

&lt;script src="https://gist.github.com/1332241.js"&gt; &lt;/script&gt;


&lt;p&gt;A lot going on there, right? Let&amp;rsquo;s take a look at everything going on.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 10:&lt;/strong&gt; The &lt;code&gt;el&lt;/code&gt; property is used to describe the html element associated with a view. In this case, we select the #map_canvas div tag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 14-31:&lt;/strong&gt; Standard Google Maps API stuff. This is where the map gets generated. Note that we set the height of the map_canvas to the height of the window to make sure the map is the correct size.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 36-51:&lt;/strong&gt; Here we add an event to add a tweet to the map every time the collection associated with this view adds a new record.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;strong&gt; UPDATE :&lt;/strong&gt; Newer versions of Backbone will actively make a distinction
between &lt;code&gt;el&lt;/code&gt; and &lt;code&gt;$el&lt;/code&gt;. Use &lt;code&gt;el&lt;/code&gt; to access the raw HTML DOM object.
Use &lt;code&gt;$el&lt;/code&gt; to access the jQuery version, for example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Without jQuery:
this.el.text = "Hello world";

// Use jQuery:
this.$el.text("I'm using jQuery!");
&lt;/code&gt;&lt;/pre&gt;

&lt;hr /&gt;

&lt;h3&gt;Initialize!&lt;/h3&gt;

&lt;p&gt;Finally, we instantiate the tweets collection and map view, setting everything in motion:&lt;/p&gt;

&lt;script src="https://gist.github.com/1338419.js"&gt; &lt;/script&gt;


&lt;hr /&gt;

&lt;h3&gt;Cool, a TwitterMap.&lt;/h3&gt;

&lt;p&gt;Pop open the browser and take a look at your freshly created TwitterMap! We &lt;em&gt;could&lt;/em&gt; use the Twitter API to make the tweets appear at an exceptionally faster rate. However you really can&amp;rsquo;t beat the simplicity of the basic search.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;I&amp;rsquo;m not the first person to write a tutorial on backbone to say &lt;em&gt;&amp;ldquo;But we&amp;rsquo;ve only scratched the surface&amp;rdquo;&lt;/em&gt;, and I certainly won&amp;rsquo;t be the last. Future installments will delve into a more detailed overview of how models and collections work. We&amp;rsquo;ll go through events, syncing information, and other important concepts. I look forward to showing you what can be done within Backbone!&lt;/p&gt;

&lt;p&gt;&lt;footer class="footnotes"&gt;
  &lt;p id="one"&gt;1 Taken from the documentation. It can be found here: &lt;a href="http://documentcloud.github.com/backbone/"&gt;http://documentcloud.github.com/backbone/&lt;/a&gt;.&lt;/p&gt;
  &lt;p id="two"&gt;2 &lt;a href="http://documentcloud.github.com/underscore/#filter"&gt;_.filter()&lt;/a&gt; is one of many exceptional functions provided by &lt;a href="http://documentcloud.github.com/underscore/"&gt;Underscore&lt;/a&gt; for sifting through data.&lt;/p&gt;
&lt;/footer&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Initial Commit</title>
    <link rel="alternate" href="http://127.0.0.1/2011/08/22/initial-commit/"/>
    <id>http://127.0.0.1/2011/08/22/initial-commit/</id>
    <published>2011-08-22</published>
    <updated>2011-08-22</updated>
    <author>
      <name/>
    </author>
    <summary type="html">&lt;p&gt;Just started my Toto blog. Wordpress is amazing, don&amp;rsquo;t get me wrong, but having the simplicity is super nice.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Just started my Toto blog. Wordpress is amazing, don&amp;rsquo;t get me wrong, but having the simplicity is super nice.&lt;/p&gt;
</content>
  </entry>
</feed>
