<?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>It&#039;s Got What Plants Crave</title>
	<atom:link href="http://www.itsgotwhatplantscrave.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.itsgotwhatplantscrave.com</link>
	<description>Sproutcore and other technologies with electrolytes</description>
	<lastBuildDate>Tue, 16 Feb 2010 21:19:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Nested Records in Sproutcore</title>
		<link>http://www.itsgotwhatplantscrave.com/2010/01/24/nested-records-in-sproutcore/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2010/01/24/nested-records-in-sproutcore/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 01:08:44 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Coding Tutorials]]></category>
		<category><![CDATA[Sproutcore]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=212</guid>
		<description><![CDATA[On the project that I am working on, we created this construct called Nested Records. I have just ported it over to the Sproutcore framework. It is extremely useful for data models that use a nested structure. These include most document based database systems like CouchDB and Persevere.  These databases and subsequent APIs sometimes [...]]]></description>
			<content:encoded><![CDATA[<p>On the project that I am working on, we created this construct called <b>Nested Records</b>. I have just ported it over to the Sproutcore framework. It is extremely useful for data models that use a nested structure. These include most document based database systems like <a href='http://couchdb.apache.org/'>CouchDB</a> and <a href='http://www.persvr.org/'>Persevere</a>.  These databases and subsequent APIs sometimes need to be configured to make them more Sproutcore-friendly and, now with the new <b>Nested Records</b>, it should be much more easy to create APIs and data structures that make more sense. These nested records can be <b>n-levels</b> and polymophic so that it is a highly flexible framework.</p>
<p><span id="more-212"></span></p>
<h2>What is a Nested Record?</h2>
<p>First, let&#8217;s define what a nested record looks like.  This is the JSON that I will use for the rest of the example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #009900;">&#123;</span>
    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Person'</span><span style="color: #339933;">,</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Albert Einstein'</span><span style="color: #339933;">,</span>
    address<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Address'</span><span style="color: #339933;">,</span>
      street<span style="color: #339933;">:</span> <span style="color: #3366CC;">'123 Genius Lane'</span><span style="color: #339933;">,</span>
      city<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Princeton'</span><span style="color: #339933;">,</span>
      state<span style="color: #339933;">:</span> <span style="color: #3366CC;">'NJ'</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and a list of nested records would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #009900;">&#123;</span>
    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Group'</span><span style="color: #339933;">,</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Smart People'</span><span style="color: #339933;">,</span>
    people<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
      <span style="color: #009900;">&#123;</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Person'</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Albert Einstien'</span><span style="color: #339933;">,</span>
        addresses<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
          <span style="color: #009900;">&#123;</span>
            type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Address'</span><span style="color: #339933;">,</span>
            street<span style="color: #339933;">:</span> <span style="color: #3366CC;">'123 Genius Lane'</span><span style="color: #339933;">,</span>
            city<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Princeton'</span><span style="color: #339933;">,</span>
            state<span style="color: #339933;">:</span> <span style="color: #3366CC;">'NJ'</span>
          <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          <span style="color: #009900;">&#123;</span>
            type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Address'</span><span style="color: #339933;">,</span>
            street<span style="color: #339933;">:</span> <span style="color: #3366CC;">'77 Gedanken Way'</span><span style="color: #339933;">,</span>
            city<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Ether'</span><span style="color: #339933;">,</span>
            state<span style="color: #339933;">:</span> <span style="color: #3366CC;">'MT'</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Person'</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Richard Feynman'</span><span style="color: #339933;">,</span>
        addresses<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
          <span style="color: #009900;">&#123;</span>
            type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Address'</span><span style="color: #339933;">,</span>
            street<span style="color: #339933;">:</span> <span style="color: #3366CC;">'6626 Planck Pl'</span><span style="color: #339933;">,</span>
            city<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Altadena'</span><span style="color: #339933;">,</span>
            state<span style="color: #339933;">:</span> <span style="color: #3366CC;">'CA'</span>
          <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          <span style="color: #009900;">&#123;</span>
            type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Address'</span><span style="color: #339933;">,</span>
            street<span style="color: #339933;">:</span> <span style="color: #3366CC;">'11 Schrödinger Ct'</span><span style="color: #339933;">,</span>
            city<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Baja'</span><span style="color: #339933;">,</span>
            state<span style="color: #339933;">:</span> <span style="color: #3366CC;">'CA'</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Nested Record Structure in Sproutcore</h2>
<p>Now, that we know what the nested records are in JSON. What is the structure in Sproutcore?  Well, it really boils down to two things: <b>Setting up your Record to be a parent</b> and <b>Child Records</b>.  Obviously, parent records can have one or more child records and this is where the power comes into the equation.  The parent record are set up with the use of the <b>toOne()</b> and <b>toMany()</b> attributes on the model. This will tell the specific RecordAttributes wether to handle the relationship as a guid or as the specific hashes.  You can also do this on SC.ChildRecords as well to create heavily nested structures. So what would this look like for our first example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  NestedRecord.<span style="color: #660066;">Address</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ChildRecord</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    street<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    city<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    state<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>defaultValue<span style="color: #339933;">:</span> <span style="color: #3366CC;">'VA'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  NestedRecord.<span style="color: #660066;">Person</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/** Child Record Namespace */</span>
    childRecordNamespace<span style="color: #339933;">:</span> NestedRecord<span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    address<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">toOne</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'NestedRecord.Address'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> nested<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and for the second example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  NestedRecord.<span style="color: #660066;">Group</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/** Child Record Namespace */</span>
    childRecordNamespace<span style="color: #339933;">:</span> NestedRecord<span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    people<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">toMany</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'NestedRecord.Person'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> nested<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  NestedRecord.<span style="color: #660066;">Person</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ChildRecord</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/** Child Record Namespace */</span>
    childRecordNamespace<span style="color: #339933;">:</span> NestedRecord<span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    addresses<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">toMany</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'NestedRecord.Address'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> nested<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  NestedRecord.<span style="color: #660066;">Address</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ChildRecord</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    street<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    city<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    state<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Record</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span>String<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>defaultValue<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;VA&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Single Child Version</h2>
<p>I will take the first example: A Person with an Address attached to them.  First, I should talk a little about the theory and approach that I have taken.  The idea is that these are javascript objects on the primary object, but they can be accessed as SC.Record objects in the store.  This is an important distinction when it comes to setting the values at the different levels of the nested record stack. For example, if you were to change the variables on the address of Albert Einstein you would do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> albert <span style="color: #339933;">=</span> NestedRecord.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'store'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span>NestedRecord.<span style="color: #660066;">Person</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// assuming that it is the first record here...</span>
  <span style="color: #003366; font-weight: bold;">var</span> al_address <span style="color: #339933;">=</span> albert.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'address'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  al_address.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'street'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'2233 General Relativity Way'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>but if you wanted to change the entire address you would need to do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> albert <span style="color: #339933;">=</span> NestedRecord.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'store'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span>NestedRecord.<span style="color: #660066;">Person</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// assuming that it is the first record here...</span>
  albert.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'address'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Address'</span><span style="color: #339933;">,</span>
    street<span style="color: #339933;">:</span> <span style="color: #3366CC;">'2233 General Relativity Way'</span><span style="color: #339933;">,</span>
    city<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Princeton'</span><span style="color: #339933;">,</span>
    state<span style="color: #339933;">:</span> <span style="color: #3366CC;">'NJ'</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The reason is having to do with the reference frame.  When you are talking to a parent record, the child records are JS objects and when you are talking to the child record themselves they are SC.Records.</p>
<h2>Polymorphism</h2>
<p>Child Records where built with polymorphism in mind and there are two rules that need to be followed in order for this to work:</p>
<ul>
<li> Parent Records must have a <b>childRecordNamespace</b> property</li>
<li> Child Records must have a <b>type</b> property that relates to the name of the SC.ChildRecord
</ul>
<p>If you don&#8217;t want polymorphism, you can just add the Record name string in the child relation definition in the model as you can see from the above examples.</p>
<h2>Multiple Children&#8230;Duggar-style</h2>
<p>So that last thing I will talk about is the case where you have multiple child records attached to a parent record like the second example.  When you <b>get()</b> one of these params then you get a <b>SC.ChildArray</b> which is exactly like a <b>SC.ManyArray</b> which means that it mixes in <b>SC.Enumerable</b> and <b>SC.Array</b> to give you all the KVO/KVC compliant things, but the important thing to remember is that they must be accessed in the KVO/KVC compliant way if you want to have the bindings and observes fire in your application.  So the standard calls looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> smart_ppl <span style="color: #339933;">=</span> NestedRecord.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'store'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span>NestedRecord.<span style="color: #660066;">Group</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// assuming that it is the first record here..</span>
  <span style="color: #003366; font-weight: bold;">var</span> len <span style="color: #339933;">=</span> smart_ppl.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'length'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns the length of the array</span>
  <span style="color: #003366; font-weight: bold;">var</span> albert <span style="color: #339933;">=</span> smart_ppl.<span style="color: #660066;">objectAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// return the first object in the array</span>
  smart_ppl.<span style="color: #660066;">pushObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Person'</span><span style="color: #339933;">,</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Neumann János Lajos'</span><span style="color: #339933;">,</span>
    addresses<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Pushes a new child record onto the array</span>
  <span style="color: #006600; font-style: italic;">// The same works for: popObject, shiftObject, unshiftObject etc..</span></pre></div></div>

<h2>Conclusion</h2>
<p>So hopefully, this will provide a missing feature to the Sproutcore Datasource framework.  I wrote 35 test with 251 assertions so it works pretty well.  I need to have other people test it in different situations so that I can improve the construct.  There was also a side benefit&#8230;I had to add a new function to the <b>SC.Store</b> to unload records when you don&#8217;t need them and it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  unloadRecord<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>recordType<span style="color: #339933;">,</span> id<span style="color: #339933;">,</span> storeKey<span style="color: #339933;">,</span> newStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// code is here</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The newStatus defaults to <b>SC.Record.EMPTY</b>, but you can set it to anything that you want like <b>SC.Record.DESTROYED_CLEAN</b> for the case where a record was destroyed on your datasource without your GUI&#8217;s knowledge.</p>
<p>So please use this new construct&#8230;it can be found on the main <a href='http://github.com/sproutit/sproutcore'>Sproutcore</a> repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2010/01/24/nested-records-in-sproutcore/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Basic View API (part 2)</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/08/23/basic-view-api-part-2/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/08/23/basic-view-api-part-2/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 10:08:10 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Coding Tutorials]]></category>
		<category><![CDATA[Sproutcore]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=183</guid>
		<description><![CDATA[In part 2, we will discuss some more advanced concepts in the View API like composite views.  We will talk a little bit about the mechanics of how to create childViews as a part of the View API.  Creating child views is a very important aspect of creating Sproutcore applications.  You will [...]]]></description>
			<content:encoded><![CDATA[<p>In part 2, we will discuss some more advanced concepts in the View API like <a href='http://www.itsgotwhatplantscrave.com/2009/07/29/composite-views/'>composite views</a>.  We will talk a little bit about the mechanics of how to create <b>childViews</b> as a part of the View API.  Creating child views is a very important aspect of creating Sproutcore applications.  You will probably do this more often in the code than you will be creating totally custom views.  Also you will use them when you architect your application to create different views of the application.</p>
<p><span id="more-183"></span></p>
<h2>Basics of Childviews</h2>
<p><b>ChildViews</b> can be designed in two main places: In the instance of the view (<b>.lproj</b> directory), and the definition of the custom view (<b>view</b> directory).  Depending on what you want to do, you would define the child views in the right layer.  For one-off view layout constuction, as in building your user-focused application and laying out the views, you will define the child views in the <b>.lproj</b> directories.  If you want to build a composite view that you will be using repeatedly then you would build the definition of the childviews in the <b>view</b> directory.</p>
<h3>Childviews in the Instance Layer</h3>
<p>We have already seen this in our current application in the <b>main_page.js</b> in <b>english.lproj</b>.  We are going to add another child view to show what we can do:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">mainPage</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Page</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// The main pane is made visible on screen as soon as your app is loaded.</span>
    <span style="color: #006600; font-style: italic;">// Add childViews to this pane for views to display immediately on page </span>
    <span style="color: #006600; font-style: italic;">// load.</span>
    mainPane<span style="color: #339933;">:</span> SC.<span style="color: #660066;">MainPane</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      childViews<span style="color: #339933;">:</span> <span style="color: #3366CC;">'titleView blogBodyView'</span>.<span style="color: #660066;">w</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// &lt;== The List of Child View names that you want to render...</span>
&nbsp;
      titleView<span style="color: #339933;">:</span> MyApp.<span style="color: #660066;">TitleView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'another-custom-class'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Basic View API'</span><span style="color: #339933;">,</span>
        icon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'tutorial'</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
      blogBodyView<span style="color: #339933;">:</span> SC.<span style="color: #660066;">LabelView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        escapeHTML<span style="color: #339933;">:</span> NO<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// This means that if there is HTML in the value string do not escape it, render the HTML</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'blog-body'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is the intro to an awesome blog post. &lt;h2&gt;New Header for cool stuff&lt;/h2&gt; Keep posting some cool stuff&quot;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>OK, we have added two childviews to the main page: <b>titleView</b>, which is the custom view that we have built, and <b>blogBodyView</b>, which is using an basic SC.View. The important thing to note is that we create these as named views in the <b>SC.MainPane</b> design definition.  This gives us the ability to reference the child views through named paths:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mainPane.titleView'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the instance of the MyApp.TitleView in the page</span>
  MyApp.<span style="color: #660066;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mainPane.blogBodyView'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the instance of the SC.LabelView in the page</span></pre></div></div>

<p>Then, if you want to create them in the page and make them visible to add them to the <b>childViews</b> parameter.  This is the most common way that you are going to be creating childviews.</p>
<h3>Childviews in the View Definition Layer</h3>
<p>Sometimes you will want to create composite views made up of view that you want to use over an over.  For this example, we will make a rating bar at the bottom of the blog post. So let&#8217;s run:</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">  sc-gen view MyApp.RatingBarView</pre></div></div>

<p>Which will create <b>rating_bar.js</b> in the <b>view</b> directory.  We are going to add a function called <b>createChildViews()</b> to the file and this is where we are going to put three different views in there: An SC.LabelView; a SC.ButtonView for &#8216;Helpful Post&#8217;; and one SC.ButtonView for &#8216;Unhelpful Post&#8217;.  So we will make the file look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">RatingBarView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope MyApp.RatingBarView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    createChildViews<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: #003366; font-weight: bold;">var</span> view<span style="color: #339933;">,</span> childViews <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// The first view.  Using the Class and Full Parameter Method</span>
      view <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">createChildView</span><span style="color: #009900;">&#40;</span> SC.<span style="color: #660066;">LabelView</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>left<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">25</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Was this Post Helpful?&quot;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      childViews.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// The Second View...using the Design Pattern method...only need to fill out the first parameter of the createChildView()</span>
      view <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">createChildView</span><span style="color: #009900;">&#40;</span> 
        SC.<span style="color: #660066;">ButtonView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
          layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>right<span style="color: #339933;">:</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">22</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Yes&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      childViews.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// The Third View...</span>
      view <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">createChildView</span><span style="color: #009900;">&#40;</span> 
        SC.<span style="color: #660066;">ButtonView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
          layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>right<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">22</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      childViews.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'childViews'</span><span style="color: #339933;">,</span> childViews<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And then we make a simple change to the <b>main_page.js</b> in the <b>english.lproj</b> directory:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">mainPage</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Page</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// The main pane is made visible on screen as soon as your app is loaded.</span>
    <span style="color: #006600; font-style: italic;">// Add childViews to this pane for views to display immediately on page </span>
    <span style="color: #006600; font-style: italic;">// load.</span>
    mainPane<span style="color: #339933;">:</span> SC.<span style="color: #660066;">MainPane</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      childViews<span style="color: #339933;">:</span> <span style="color: #3366CC;">'titleView blogBodyView ratingBar'</span>.<span style="color: #660066;">w</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
      titleView<span style="color: #339933;">:</span> MyApp.<span style="color: #660066;">TitleView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'another-custom-class'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Basic View API'</span><span style="color: #339933;">,</span>
        icon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'tutorial'</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
      blogBodyView<span style="color: #339933;">:</span> SC.<span style="color: #660066;">LabelView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        escapeHTML<span style="color: #339933;">:</span> NO<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// This means that if there is HTML in the value string do not escape it, render the HTML</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'blog-body'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is the intro to an awesome blog post. &lt;h2&gt;New Header for cool stuff&lt;/h2&gt; Keep posting some cool stuff&quot;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
      ratingBar<span style="color: #339933;">:</span> MyApp.<span style="color: #660066;">RatingBarView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">580</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">32</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'rating-bar'</span><span style="color: #009900;">&#93;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And now if we add a little more css, we get the app to look like this:</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/08/basic_view_api_part_2_pic_1.jpg" alt="basic view api part 2 pic 1"></p>
<h3>using the createChildView()</h3>
<p>The <b>createChildView()</b> is a very important function to use.  You should only use this function when you are creating child views that will live in your parent view. This function will set up the ownership chain and all the little things that you need the view to do to function correctly in Sproutcore. There are two methods for using the <b>createChildView()</b> that you can choose to do: <b>Class and Full Parameter</b> method and the <b>Design Pattern</b> method.  Let&#8217;s take a look at the first method, this is lifted from the <b>view.js</b> code in Sproutcore:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">/**
    Instantiates a view to be added to the childViews array during view 
    initialization. You generally will not call this method directly unless 
    you are overriding createChildViews(). Note that this method will 
    automatically configure the correct settings on the new view instance to 
    act as a child of the parent.
&nbsp;
    @param {Class} viewClass
    @param {Hash} attrs optional attributes to add
    @returns {SC.View} new instance
    @test in createChildViews
  */</span>
  createChildView<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>view<span style="color: #339933;">,</span> attrs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Code to do the stuff...</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first parameter is the view class and the second parameters is all the attributes that you want to send to the function when it is created.  The second method (<b>Design Pattern</b>) takes advantage of the fact that <b>.design()</b> acts just like a extension of the view class object.  It is really up to you which you prefer to do, they work exactly the same.</p>
<h2>Micellaneous</h2>
<p>There are a couple of functions that are important to note when you are making basic views.  You might not need to do all or any of these but they have some little quirks that need to be known:</p>
<h3>init()</h3>
<p>You might find the need to overwrite the <b>init()</b> function to do some special initialization.  This is common practice but you must follow this pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  init<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>
    sc_super<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// &lt;== YOUR CODE HERE</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The reason for this is that the <b>init()</b> in the view is what sets up the bindings and that should be done first and then your stuff second.</p>
<h3>didCreateLayer()</h3>
<p>This function is called only once at the end of the cycle of creating the view layer.  This is where you would set up some extra bindings or some special event handling that needs all the parts created and instantiated first. You do not need to call sc_super() if you <b>extend()</b> SC.View because there is no implementation of <b>didCreateLayer()</b> in SC.View.  If you extend one of your own classes or one that does implement <b>didCreateLayer()</b> remember to call sc_super().</p>
<h3>willDestroyLayer()</h3>
<p><b>willDestroyLayer()</b> is the paired function to <b>didCreateLayer()</b> and you would need to remove any work that would cause memory leaks that you did in the <b>didCreateLayer())</b>.  The same sc_super() principles apply.</p>
<h2>Conclusions</h2>
<p>This is the basics for building custom views and composite views and 90% of the views that you will create will just need to use what is in this post and <a href='http://www.itsgotwhatplantscrave.com/2009/08/17/basic-view-api-part-1/'>part 1</a>.  In a later post, I want to show you some more advanced stuff that you can do with the child views.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/08/23/basic-view-api-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Basic View API (part 1)</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/08/17/basic-view-api-part-1/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/08/17/basic-view-api-part-1/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 10:22:06 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Coding Tutorials]]></category>
		<category><![CDATA[Sproutcore]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=158</guid>
		<description><![CDATA[We will talk about some of the fundamental elements of the View API and use this knowledge to build an advanced composite view.  The Sproutcore View API in 1.0 is very impressive and has a lot of power behind it, but with all that power it can sometime be very intimidating to understand where [...]]]></description>
			<content:encoded><![CDATA[<p>We will talk about some of the fundamental elements of the View API and use this knowledge to build an advanced composite view.  The Sproutcore View API in 1.0 is very impressive and has a lot of power behind it, but with all that power it can sometime be very intimidating to understand where to start or what it all means when you use properties or functions.  I will try to break down the basic elements of the View API in to the following categories: <b>Invoking Methods</b>, <b>Properties</b>, and <b>Functions</b>.  There will be an example to follow and I am assuming that you have created a project called <b>MyApp</b>.  We will be making a very simple application that will show a blog post with the ability to do some actions on the post like make the post a favorite or the author a favorite&#8230;</p>
<p><span id="more-158"></span></p>
<h2>Invoking Methods</h2>
<p>Invoking Methods are used when you are implementing a new generic view class, or designing a specific instance of a view for later use, or creating an instance of a view. There are three functions that do this:</p>
<ul>
<li><b>extend():</b> implements a generic view class</li>
<li><b>design():</b> designs a specific instance of a view for later use</li>
<li><b>create():</b> creates a specific instance of the view</li>
</ul>
<h3>Using extend()</h3>
<p>For basic use, <b>extend()</b> is the function that you use to create a generic view class from another view class and usually this is the <b>SC.View</b> class.  For this post, we will be building up a basic view and using all the basic methods that you will come across.  So let&#8217;s begin&#8230;</p>
<p>So the first thing we have to do is make a simple view to show the title of the blog post.  We will call this the TitleView. So we will run the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">  sc-gen view MyApp.TitleView</pre></div></div>

<p>This will create the view from our root directory (<b>/apps/my_app/views</b>) and it will create a new file called <b>title.js</b>&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// Project:   MyApp.TitleView</span>
  <span style="color: #006600; font-style: italic;">// Copyright: ©2009 My Company, Inc.</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #009966; font-style: italic;">/*globals MyApp */</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">/** @class
&nbsp;
    This is the Title View that we are going to use all the different
    View API Calls
&nbsp;
    @extends SC.View
  */</span>
  MyApp.<span style="color: #660066;">TitleView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope MyApp.TitleView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// TODO: Add your own code here.</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So this is just the shell of the object that we are going to fill with the stuff that we want use from the View API.  When we use <b>extend()</b> we are extending the base class of <b>SC.View</b> and then we are going to add our own stuff as well as override some specific API elements of <b>SC.View</b> to get what we want.  The use of <b>extend()</b> doesn&#8217;t create an instance of the view and here is the first&#8230;</p>
<div style='border: 1px solid red'>
<b style='color: red;'>BEST PRACTICE TIP:</b> you should only use <b>extend()</b> in the views directory structure.  This is where you create these view classes to be used by your project.  If you are using <b>extend()</b> for a view outside the directory you are probably doing something wrong.
</div>
<p>So this isn&#8217;t very exciting yet, because we can&#8217;t see anything.  So we have to actually create an instance of this <b>TitleView</b> and now we will learn the correct way of&#8230;</p>
<h3>Using design()</h3>
<p>This is a part of the Design View API.  This is a very special API that is used to create instances of a view without actually creating the view yet.  They are sort of stored for later use when the application needs to create them. So how we would use this would be to add the view to the main pane. In the <b>main_page.js</b>, we change it to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// Project:   MyApp - mainPage</span>
  <span style="color: #006600; font-style: italic;">// Copyright: ©2009 My Company, Inc.</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #009966; font-style: italic;">/*globals MyApp */</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'views/title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// This page describes the main user interface for your application.  </span>
  MyApp.<span style="color: #660066;">mainPage</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Page</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// The main pane is made visible on screen as soon as your app is loaded.</span>
    <span style="color: #006600; font-style: italic;">// Add childViews to this pane for views to display immediately on page </span>
    <span style="color: #006600; font-style: italic;">// load.</span>
    mainPane<span style="color: #339933;">:</span> SC.<span style="color: #660066;">MainPane</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      childViews<span style="color: #339933;">:</span> <span style="color: #3366CC;">'titleView'</span>.<span style="color: #660066;">w</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
      titleView<span style="color: #339933;">:</span> MyApp.<span style="color: #660066;">TitleView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The most important thing to see here is that view cannot live outside an <b>SC.Page</b>. <b>SC.Page</b> is a holder for views to access them.  It manages it keeps them accessible when they are hidden from the application and give you a means to get to them from other parts of tha application.</p>
<h3>Using create()</h3>
<p>The final <b>Invoking Method</b> is <b>create()</b>.  This method is actually used the least and only when you need to create a view right at the moment.  This is usually done in another view where you need to keep an internal view around for display purposes.  We will create one later on when we add an action to the title bar to show a popup for the author.</p>
<h2>Properties</h2>
<p>Now, we will tackle the properties that are the most common for you to use when using the Sproutcore 1.0 View API.  The first one that we will discuss is&#8230;</p>
<h3>classNames</h3>
<p>Out of the box, any view that <b>extends()</b> and SC.View has a class named <b>sc-view</b>, but you can add your own classes by adding a property to your view called <b>classNames</b>.  This is an array of strings that will be added to the root element in the DOM object.  So let&#8217;s make the <b>TitleView</b> look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">TitleView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope MyApp.TitleView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'title-view'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'another-custom-class'</span><span style="color: #009900;">&#93;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After we create a file called <b>style.css</b> in the <b>english.lproj</b> and add some simple css, our view should look like this:</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/08/view_api_pic_1.jpg" alt="view-api-pic 1"></p>
<p>and the HTML looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">  &lt;div style=&quot;left: 0px; right: 0px; top: 0px; bottom: 0px;&quot; class=&quot;sc-view sc-pane sc-main&quot; id=&quot;sc255&quot;&gt;
    &lt;div style=&quot;left: 50%; width: 400px; margin-left: -200px; top: 10px; height: 50px;&quot; class=&quot;sc-view title-view another-custom-class&quot; id=&quot;sc256&quot;/&gt;
  &lt;/div&gt;</pre></div></div>

<p>You can see that we have the class names that were add to the view.  Now, you can add these class names in the <b>MyApp.TitleView</b> file or you can add the same property to the instance of the view when you use <b>design()</b> the only difference is that if you do it in the <b>MyApp.TitleView</b> it will be added to every instance of the <b>MyApp.TitleView</b> and if you use it in the <b>design()</b> the class will only exist for that instance. So the next thing that we want to do is change the tag of our view from a <b>div</b> to a <b>H1</b> tag, we accomplish this by using the <b>tagName</b> and <b>layout</b> properties&#8230;</p>
<h3>tagName and layout</h3>
<p>We will make a simple change to the <b>MyApp.TitleView</b> to make it look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">TitleView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope MyApp.TitleView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    tagName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'h1'</span><span style="color: #339933;">,</span>
    classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'title-view'</span><span style="color: #009900;">&#93;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, if we re-run the application the html will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">  &lt;div style=&quot;left: 0px; right: 0px; top: 0px; bottom: 0px;&quot; class=&quot;sc-view sc-pane sc-main&quot; id=&quot;sc255&quot;&gt;
    &lt;h1 style=&quot;left: 50%; width: 400px; margin-left: -200px; top: 10px; height: 50px;&quot; class=&quot;sc-view title-view another-custom-class&quot; id=&quot;sc256&quot;/&gt;
  &lt;/div&gt;</pre></div></div>

<p>Now, we have full control over what the base tag of our view. By the way, I moved one of the <b>clasNames</b> out to the instance of the view in the main pane to show that properties can live on the <b>extend()</b> or the <b>design()</b></p>
<p>For the <b>layout</b> property, we have already used this in the <b>MainPane</b> and frozencanuck has a pretty good <a href='http://frozencanuck.wordpress.com/2009/08/13/simple-positioning-of-views-in-sproutcore/'>explanation</a> of how to use <b>layout</b> to position objects on the page.  The list of layout parameters that you can use are:</p>
<ul>
<li><b>top:</b> Anchors to the top of the frame that you are in</li>
<li><b>right:</b> Anchors to the right of the frame that you are in</li>
<li><b>bottom:</b> Anchors to the bottom of the frame that you are in</li>
<li><b>left:</b> Anchors to the left of the frame that you are in</li>
<li><b>centerX:</b> Anchors to the middle horizontal position of the frame that you are in</li>
<li><b>centerY:</b> Anchors to the middle vertical postion of the frame that you are in</li>
<li><b>height:</b> The fixed height of the view</li>
<li><b>minHeight:</b> The minimum height of the view</li>
<li><b>maxHeight:</b> The max height of the view</li>
<li><b>width:</b> The fixed width of the view</li>
<li><b>minWidth:</b> The minimum width of the view</li>
<li><b>maxWidth:</b> The max width of the view</li>
</ul>
<p>Layout is usually added to the actual instance of the view in the <b>.lproj</b> layer of the code. So the next two properties that we will talk about is&#8230;</p>
<h3>status properties</h3>
<p>These two properties (<b>isEnabled</b> and <b>isVisible</b>), when used, will add the following classes respectively: <b>disabled</b> and <b>hidden</b>.  These can be very important classes you use with bindings.  So if you had a controller that was going to hide one of the views you would create a binding like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">mainPage</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Page</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// The main pane is made visible on screen as soon as your app is loaded.</span>
    <span style="color: #006600; font-style: italic;">// Add childViews to this pane for views to display immediately on page </span>
    <span style="color: #006600; font-style: italic;">// load.</span>
    mainPane<span style="color: #339933;">:</span> SC.<span style="color: #660066;">MainPane</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      childViews<span style="color: #339933;">:</span> <span style="color: #3366CC;">'titleView'</span>.<span style="color: #660066;">w</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
      titleView<span style="color: #339933;">:</span> MyApp.<span style="color: #660066;">TitleView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'another-custom-class'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        isVisibleBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'MyApp.blogController.showTitle'</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will change that value on the view to make it visible or not depending on the bound value. Now, there is one more property that we need to talk about and that is&#8230;</p>
<h3>displayProperties</h3>
<p>The property <b>displayProperties</b> is a very special property that will trigger the view to re-render itself when new data is set on the view.  So let&#8217;s add a new property that we will use on the view called <b>name</b> and <b>icon</b> that will be used to print out the name of the blog post that this title view is representing and add an icon to the title.  So we will change the <b>MyApp.TitleView</b> to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">TitleView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope MyApp.TitleView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
    @public: Property to change the view too..
    */</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
    icon<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
&nbsp;
    tagName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'h1'</span><span style="color: #339933;">,</span>
    classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'title-view'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    displayProperties<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'icon'</span><span style="color: #009900;">&#93;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, when ever something calls a <b>set()</b> on these any one of these properties, the view will trigger a refresh on the view and re-render the html with the changes to reflect the new values. We have add this it is all well and good but it doesn&#8217;t really help us at all.  We need to add some functions to the view to create our our custom feel to the view&#8230;So we will transition to talking about&#8230;</p>
<h2>Functions</h2>
<p>The very first function that we will talk about is the most important one gives us the ability to create a custom feel to our view and that function is&#8230;</p>
<h3>render()</h3>
<p>The <b>render()</b> function has two parameters: <b>context</b> and <b>firstTime</b>. The first (<b>context</b>) is the SC.RenderContext that is used to build up the HTML to render in the page.  Part of the power of Sproutcore is that it makes use of the <b>innerHTML()</b> speed in the browser to re-render pages very quickly without the need for DOM manipulation.  This is very important for performance on the not so stellar browsers (like IE). The second parameter is <b>firstTime</b> and this indicates when the object is rendered for a second time or more.  This can be triggered by a change in the one of the <b>displayProperties</b>.  So, we are going to add a <b>render()</b> to to the <b>MyApp.TitleView</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">TitleView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope MyApp.TitleView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
    @public: Property to change the view too..
    */</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
    icon<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
&nbsp;
    tagName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'h1'</span><span style="color: #339933;">,</span>
    classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'title-view'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    displayProperties<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'icon'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
    render<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> firstTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> icon <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'icon'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>firstTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        context <span style="color: #339933;">=</span> context.<span style="color: #660066;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image %@-icon'</span>.<span style="color: #660066;">fmt</span><span style="color: #009900;">&#40;</span>icon<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        context <span style="color: #339933;">=</span> context.<span style="color: #660066;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and the <b>MainPane</b> is changed to look like the following&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">mainPage</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Page</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// The main pane is made visible on screen as soon as your app is loaded.</span>
    <span style="color: #006600; font-style: italic;">// Add childViews to this pane for views to display immediately on page </span>
    <span style="color: #006600; font-style: italic;">// load.</span>
    mainPane<span style="color: #339933;">:</span> SC.<span style="color: #660066;">MainPane</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      childViews<span style="color: #339933;">:</span> <span style="color: #3366CC;">'titleView'</span>.<span style="color: #660066;">w</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
      titleView<span style="color: #339933;">:</span> MyApp.<span style="color: #660066;">TitleView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> centerX<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'another-custom-class'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Basic View API'</span><span style="color: #339933;">,</span>
        icon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'tutorial'</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Added some css to make it look different in <b>style.css</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">  h1<span style="color: #6666ff;">.title-view</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  div<span style="color: #6666ff;">.image</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">9px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  span<span style="color: #6666ff;">.name</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">9px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">338px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>and now it looks like this:</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/08/basic_view_api_pic_2.jpg" alt="basic view api - pic 2" height="540" width="818"></p>
<p>with the HTML looking like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">  &lt;h1 style=&quot;left: 50%; width: 400px; margin-left: -200px; top: 10px; height: 50px;&quot; class=&quot;sc-view title-view another-custom-class&quot; id=&quot;sc256&quot;&gt;
    &lt;div class=&quot;image tutorial-icon&quot;/&gt;
    &lt;span class=&quot;name&quot;&gt;Basic View API&lt;/span&gt;
  &lt;/h1&gt;</pre></div></div>

<p>which brings us to another&#8230;</p>
<div style='border: 1px solid red'>
<b style='color: red;'>BEST PRACTICE TIP:</b> Any thing that is built by the <b>SC.RenderContext</b> in a View can be css styled with anything.  The absolute positioning is done only on the base view object and the reason for this is that Sproutcore implements <b>Event Delegation</b> and it builds a view tree from the absolute position that the <b>SC.RootResponder</b> walks to delegate the events to the right view.
</div>
<p>We also touched on some of the functions in the <b>SC.RenderContext</b>:</p>
<ul>
<li><b>begin():</b> This begins a tag and it takes one parameter of tagname</li>
<li><b>addClass():</b> This adds a class to the current context tag</li>
<li><b>text():</b> This adds the inner text of the current context tag</li>
<li><b>end():</b> This ends the current context tag</li>
</ul>
<p>I will do a later post on the <b>SC.RenderContext</b> API.  So now lets do something a little tricky, we are going to modify the view at runtime and have the render function called again to make the view change.  So we change the <b>TitleView</b> again:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MyApp.<span style="color: #660066;">TitleView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope MyApp.TitleView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
    @public: Property to change the view too..
    */</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
    icon<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
&nbsp;
    tagName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'h1'</span><span style="color: #339933;">,</span>
    classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'title-view'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    displayProperties<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'icon'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
    render<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> firstTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Rendering TitleView'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> icon <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'icon'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> isFav <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'isFavorite'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>firstTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        context <span style="color: #339933;">=</span> context.<span style="color: #660066;">setClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'favorite'</span><span style="color: #339933;">,</span> isFav<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        context <span style="color: #339933;">=</span> context.<span style="color: #660066;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image %@-icon'</span>.<span style="color: #660066;">fmt</span><span style="color: #009900;">&#40;</span>icon<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        context <span style="color: #339933;">=</span> context.<span style="color: #660066;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.image'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replaceWith</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;image %@-icon&quot; /&gt;'</span>.<span style="color: #660066;">fmt</span><span style="color: #009900;">&#40;</span>icon<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.name'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We are actually doing DOM manipulation with the subsequent calls and we are touching on the first part of the <b>SC.CoreQuery</b> which is a subset of the JQuery DOM manipulation.  You can add JQuery to your project and it will override the <b>SC.CoreQuery</b>, but there are some really helpful function that are very similar to the <b>SC.RenderContext</b> API.  The way that you access the elements is very JQuery-selectoresque with the <b>$()</b> notation. If you want to access the base object of the view, you can use: <b>this.$()</b> which you can add class or change the css.  And this brings up another:</p>
<div style='border: 1px solid red'>
<b style='color: red;'>BEST PRACTICE TIP:</b> DOM Manipulation is expensive. So it is best to only do up to two DOM manipulation elements before it get too expensive and you should just redraw the layer using the <b>SC.RenderContext</b> again.
</div>
<p>To see some changes, you can run the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cmd" style="font-family:monospace;">  var titleView = MyApp.mainPage.getPath('mainPane.titleView')
  SC.run(function() { titleView.set('name', 'RenderContext API'); titleView.set('icon', 'commentary'); })</pre></div></div>

<h2>Conclusions</h2>
<p>For part 1, we have done all that we need to do create custom views and manipulate them with changes to the view when properties get updated.  In part 2, we will add some more functionality like add <b>childViews</b> and pair them with the current <b>SC.RenderContext</b> to make composite views with custom HTML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/08/17/basic-view-api-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sproutcore Blogsphere</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/08/16/sproutcore-blogsphere/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/08/16/sproutcore-blogsphere/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 11:27:28 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/2009/08/16/sproutcore-blogsphere/</guid>
		<description><![CDATA[I just want to give a shout out to some guys who are on my team who are blogging some cool stuff.
Mike Cohen (frozencanuck)
Mike just joined our team a just few weeks ago. He is learning Sproutcore from scratch and doing a great job.  He is posting some beginner tutorials about what he is [...]]]></description>
			<content:encoded><![CDATA[<p>I just want to give a shout out to some guys who are on my team who are blogging some cool stuff.</p>
<h3>Mike Cohen (frozencanuck)</h3>
<p>Mike just joined our team a just few weeks ago. He is learning Sproutcore from scratch and doing a great job.  He is posting some beginner tutorials about what he is learning from the team.  It is great to see through his eyes the stuff that can be confusing and he is writing some posts on the following:</p>
<ul>
<li><a href='http://frozencanuck.wordpress.com/2009/08/12/connecting-views-to-model-object/'>Connecting Views to a Model</a></li>
<li><a href='http://frozencanuck.wordpress.com/2009/08/13/simple-positioning-of-views-in-sproutcore/'>Absolute Positioning</a></li>
<li><a href='http://frozencanuck.wordpress.com/2009/08/14/creating-a-simple-custom-view-in-sproutcore-part1/'>Simple Custom View Construction</a> plus an <a href='http://frozencanuck.wordpress.com/2009/08/14/updates-to-part-1-of-creating-a-custom-view-in-sproutcore/'> update</a></li>
</ul>
<h3>Josh Holt</h3>
<p>Josh is another member of our team that is doing a little more advanced stuff.  He is taking the <a href='http://www.itsgotwhatplantscrave.com/2009/07/30/root-controller-paradigm/'>Root Controller Paradigm</a> and expanding on it to do some dynamic calculations.  It is a good read on the relationships of properties in objects and between objects when trying to get the data that you need for your views.  You can find his post <a href='http://blog.thesempiternalholts.com/post/161917555'>here</a></p>
<p>Great Work Guys!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/08/16/sproutcore-blogsphere/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Root Controller Paradigm</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/07/30/root-controller-paradigm/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/07/30/root-controller-paradigm/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 16:02:16 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Coding Tutorials]]></category>
		<category><![CDATA[Sproutcore]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=128</guid>
		<description><![CDATA[In this blog post, I will spend some time talking about using the power of bindings in the controller layer with the Root Controller Paradigm.  This is a really powerful paradigm where you do most of your work in one controller and then let the bindings cascade all the data through a controller chain. [...]]]></description>
			<content:encoded><![CDATA[<p>In this blog post, I will spend some time talking about using the power of bindings in the controller layer with the Root Controller Paradigm.  This is a really powerful paradigm where you do most of your work in one controller and then let the bindings cascade all the data through a controller chain.  This provides several points to bind your view layer in order to present the data in whatever way that your want. I will also touch on adding search functionality that you can use.  For the purposes of example, I will be constructing an example controller layer that you would use to build an iTunes-like library application&#8230;</p>
<p><span id="more-128"></span></p>
<h2>The Basics</h2>
<p>In more detail, the Root Controller Paradigm is based off the notion that you need a controller that is the source or root of where all the other controllers get their content.  This makes maintenance much easier because all the work is usually done in one place for the records and the other controllers have maybe 2-3 lines of code to set up the bindings correctly to populate the content. Then, if you need to do some specialized work (like searching) you do it in the appropriate layer.  Your controller chain is segmented in the proper object-oriented manner saving you time in development, documentation, and debugging.</p>
<p>I usually follow these steps to make it easier:</p>
<ol>
<li> Draw a Picture of the Root Controller, Controller Chain and Bindings </li>
<li> Create the Root Controller </li>
<li> Create the Controller Chain with the Bindings</li>
<li> Add any specialty code that is needed in the Controller Chain</li>
</ol>
<h2>Step #1: Draw a Picture</h2>
<p>I find it extremely useful to draw a picture of the Controller Chain and the Bindings.  This is the best way to review your work and make sure you haven&#8217;t forgotten anything.  It is also helpful to diagram out what functionality you need at what layer.  It is also a wonderful documentation tool for future reference.  For this iTunes-like functionality, the Controller Chain looks like the following:</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/08/sprouttunes_controller.jpg" alt="SproutTunes Controller"></p>
<p>You can see that the Root Controller is the <b>sourceCategoriesController</b>.  This is where most of the work will be done and where we will spend most of our time and where most of the complex stuff will be done. Then you can see how the Controller Chain will cascade the data through the structure.  You can see we are going to use an interaction of a TreeController, ArrayControllers, and ObjectControllers to accomplish what we want to do.</p>
<h2>Step #2: Create the Root Controller</h2>
<p>When we develop the <b>sourceCategoriesController</b> (Root Controller), we are going create a TreeController that will cascade the data through the structure. This particular controller will drive the view on the left side of our iTunes-like app. The structure is lifted from iTunes to give a point of reference. Here is a sample of what the code would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// SproutTunes.sourceCategoriesController</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">/** @static
&nbsp;
    This is the Root Controller...
    @extends SC.TreeController
    @author Evin Grano [EG]
  */</span>
  SproutTunes.<span style="color: #660066;">sourceCategoriesController</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">TreeController</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope SproutTunes.sourceCategoriesController.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    treeItemIsGrouped<span style="color: #339933;">:</span> YES<span style="color: #339933;">,</span>
    content<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
      @public function for contructing the source list on the left side
    */</span>
    refreshSources<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>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> librarySources <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Object</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        treeItemIsExpanded<span style="color: #339933;">:</span> YES<span style="color: #339933;">,</span>
        hasContentIcon<span style="color: #339933;">:</span> NO<span style="color: #339933;">,</span>
        displayName<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Libraries'</span><span style="color: #339933;">,</span>
        treeItemChildren<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
          SC.<span style="color: #660066;">Object</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            contentValueKey<span style="color: #339933;">:</span> <span style="color: #3366CC;">'displayName'</span><span style="color: #339933;">,</span>
            displayName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Music&quot;</span>
            items<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: #003366; font-weight: bold;">var</span> collection <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
              <span style="color: #003366; font-weight: bold;">var</span> q <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Query</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>recordType<span style="color: #339933;">:</span> SproutTunes.<span style="color: #660066;">Music</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              collection <span style="color: #339933;">=</span> store.<span style="color: #660066;">findAll</span><span style="color: #009900;">&#40;</span>q<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
              <span style="color: #000066; font-weight: bold;">return</span> collection<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">property</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">cacheable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          SC.<span style="color: #660066;">Object</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            contentValueKey<span style="color: #339933;">:</span> <span style="color: #3366CC;">'displayName'</span><span style="color: #339933;">,</span>
            displayName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Movies&quot;</span><span style="color: #339933;">,</span>
            assets<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: #003366; font-weight: bold;">var</span> collection <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
              <span style="color: #003366; font-weight: bold;">var</span> q <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Query</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>recordType<span style="color: #339933;">:</span> SproutTunes.<span style="color: #660066;">Movie</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              collection <span style="color: #339933;">=</span> store.<span style="color: #660066;">findAll</span><span style="color: #009900;">&#40;</span>q<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
              <span style="color: #000066; font-weight: bold;">return</span> collection<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">property</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">cacheable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
          <span style="color: #006600; font-style: italic;">// ADD MORE LIBRARY SOURCES: TV SHOWS, PODCASTS, ETC...</span>
        <span style="color: #009900;">&#93;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> playlists <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Object</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        treeItemIsExpanded<span style="color: #339933;">:</span> YES<span style="color: #339933;">,</span>
        hasContentIcon<span style="color: #339933;">:</span> NO<span style="color: #339933;">,</span>
        displayName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Playlists&quot;</span><span style="color: #339933;">,</span>
        treeItemChildren<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
          SC.<span style="color: #660066;">Object</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            displayName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;SproutTunes DJ&quot;</span>.<span style="color: #660066;">loc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            items<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: #003366; font-weight: bold;">var</span> collection <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
              <span style="color: #006600; font-style: italic;">// CODE FOR CREATING DJ MUSIC PLAYLIST</span>
              <span style="color: #000066; font-weight: bold;">return</span> collection<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">property</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">cacheable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
          <span style="color: #006600; font-style: italic;">// Enter a List of SC.Objects for Playlists</span>
        <span style="color: #009900;">&#93;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> allSources <span style="color: #339933;">=</span> SC.<span style="color: #660066;">Object</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        treeItemIsExpanded<span style="color: #339933;">:</span> YES<span style="color: #339933;">,</span>
        treeItemChildren<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>librarySources<span style="color: #339933;">,</span> playlists<span style="color: #009900;">&#93;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #339933;">,</span> allSources<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selection'</span><span style="color: #339933;">,</span> SC.<span style="color: #660066;">SelectionSet</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So you can see that SC.Object that is selected will drive the content of the <b>sourceController</b>.  </p>
<h2>Step #3: Create the Controller Chain with the Bindings</h2>
<p>This is the first controller that gets the benefits of the Root Controller Paradigm. The code would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// SproutTunes.sourceController</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">/** @static
&nbsp;
    @extends SC.ObjectController
    @author Evin Grano
  */</span>
  SproutTunes.<span style="color: #660066;">sourceController</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ObjectController</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope SproutTunes.sourceController.prototype */</span> <span style="color: #009900;">&#123;</span>
    contentBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'SproutTunes.sourceCategoriesController.selection'</span><span style="color: #339933;">,</span>
    contentBindingDefault<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Binding</span>.<span style="color: #660066;">single</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, we start the chain so the next controller in our chain is the <b>itemsController</b>.  This is driven by the &#8216;items&#8217; property that we set up in the Root Controller objects.  The code for this would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// SproutTunes.itemsController</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">/** @static
&nbsp;
    @extends SC.ArrayController
    @author Evin Grano
  */</span>
  SproutTunes.<span style="color: #660066;">itemsController</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ArrayController</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope SproutTunes.itemsController.prototype */</span> <span style="color: #009900;">&#123;</span>
    contentBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'SproutTunes.sourceController.items'</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// search stuff</span>
    search<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    searchContent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
    _searchHasChanged<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: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'searchContent'</span><span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">observes</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'search'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    _contentHasChanged<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: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'searchContent'</span><span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">observes</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[]'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, we have the next part of the chain which is the <b>itemsSearchController</b> that is what the &#8217;song&#8217; list will show.  The code will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// SproutTunes.itemsSearchController</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">/** @static
&nbsp;
    @extends SC.ArrayController
    @author Evin Grano
  */</span>
  SproutTunes.<span style="color: #660066;">itemsSearchController</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ArrayController</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope SproutTunes.itemsSearchController.prototype */</span> <span style="color: #009900;">&#123;</span>
    contentBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'SproutTunes.itemsController.searchContent'</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and now the final controller in the chain, <b>itemController</b> and this is what the code would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// SproutTunes.itemController</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">/** @static
&nbsp;
    @extends SC.ObjectController
    @author Evin Grano
  */</span>
  SproutTunes.<span style="color: #660066;">itemController</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ObjectController</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope SproutTunes.itemController.prototype */</span> <span style="color: #009900;">&#123;</span>
    contentBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'SproutTunes.itemsSearchController.selection'</span><span style="color: #339933;">,</span>
    contentBindingDefault<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Binding</span>.<span style="color: #660066;">single</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Step #4: Add Specialty code</h2>
<p>Now that we have our completed Controller Chain, we could hook up a ListView to the <b>sourceCategoriesController</b> to get a list on the left side and we could hook up another ListView to the <b>itemsSearchController</b> to show the list of items once the source is chosen. But, we want to add searching to the <b>itemsController</b>.  The code would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
  <span style="color: #006600; font-style: italic;">// SproutTunes.itemsController</span>
  <span style="color: #006600; font-style: italic;">// ==========================================================================</span>
&nbsp;
  sc_require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">/** @static
&nbsp;
    @extends SC.ArrayController
    @author Evin Grano
  */</span>
  SproutTunes.<span style="color: #660066;">itemsController</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ArrayController</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009966; font-style: italic;">/** @scope SproutTunes.itemsController.prototype */</span> <span style="color: #009900;">&#123;</span>
    contentBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'SproutTunes.sourceController.items'</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// search stuff</span>
    search<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    searchContent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
    _updateSearchContent<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: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> search <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'search'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>search <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>c<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;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'searchContent'</span><span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        search <span style="color: #339933;">=</span> search.<span style="color: #660066;">toLowerCase</span><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> currItem<span style="color: #339933;">,</span> currSearchFields<span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> searchArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> c.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'length'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          currItem <span style="color: #339933;">=</span> c.<span style="color: #660066;">objectAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          currSearchFields <span style="color: #339933;">=</span> currItem.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'searchFields'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currSearchFields.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>search<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> searchArray.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>currItem<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;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'searchContent'</span><span style="color: #339933;">,</span> searchArray<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: #339933;">,</span>
&nbsp;
    _searchHasChanged<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;">this</span>._updateSearchContent<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: #660066;">observes</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'search'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    _contentHasChanged<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;">this</span>._updateSearchContent<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: #660066;">observes</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[]'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Conclusion</h2>
<p>Now, you have the very clear separation of the all the functionality in controller layer and you can trust that the bindings are going to be set correctly.  So most of your work is done in the Root Controller and then you just add functionality only at the levels that make sense.  This will keeps all the information in the correct layer of the MVC structure as well as increase your ability to maintain the code and you get the benefits of the bindings in the controller layer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/07/30/root-controller-paradigm/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Composite Views</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/07/29/composite-views/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/07/29/composite-views/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 14:52:50 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Coding Tutorials]]></category>
		<category><![CDATA[Sproutcore]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[composite]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=121</guid>
		<description><![CDATA[In my last post on bindings, I talked about Rule #2 (The &#8216;-Path&#8217; Convention) which started a bit of buzz on when and why it is used.  First off, it is a naming convention that my team and I have come up with to deal with passing binding paths around in a composite view. [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post on <a href='http://www.itsgotwhatplantscrave.com/2009/06/20/bindings-unleashed/'>bindings</a>, I talked about Rule #2 (The &#8216;-Path&#8217; Convention) which started a bit of buzz on when and why it is used.  First off, it is a naming convention that my team and I have come up with to deal with passing binding paths around in a composite view. A composite view is just a custom view that is made up of using other views in some special way.  Composite Views are a great organizational construct but they can be easily misused&#8230;</p>
<p><span id="more-121"></span></p>
<h2>What is a Composite View?</h2>
<p>As stated before, a composite view is a custom view that you build that is mainly made up of other views that operates in a special way.  You would often use this when you are developing a structure of views in your &#8216;.lproj&#8217; layer and you find yourself repeating a paradigm or wanting a group of views or view structure to operate in a specific manner.  Examples of these can be a composite view made up of a textfield, labels, buttons that change the look and appearance of view. View that have special mouse functionality like a popup window or similar structure that makes passing some simple information for bindings or content to a internal child view.</p>
<h2>Example</h2>
<p>I will use a very simplistic example of a composite view for a master list view with a search bar.  I call this composite view the MasterView and it will consist of a SC.TextFieldView and a SC.ListView.  You usually don&#8217;t want to do such a simplistic view as this, but it will work for the purposes of illustration. The code might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">myApp.<span style="color: #660066;">MasterView</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>
<span style="color: #009966; font-style: italic;">/** @scope myApp.MasterView.prototype */</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'master-view'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">/**
    Necessary config elements to set up binding in the composite view
  */</span>
  contentPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>      <span style="color: #006600; font-style: italic;">// Binding Path for the content of the ListView</span>
  selectionPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>    <span style="color: #006600; font-style: italic;">// Binding Path for the selection on the ListView</span>
  searchPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>       <span style="color: #006600; font-style: italic;">// Binding Path for the text searching</span>
  contentValueKey<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>  <span style="color: #006600; font-style: italic;">// ContentValueKey to be passed to the ListView</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">/**
    Overwritten createChildView where you set up all 
    the internal child view and where we are
    going to use the Binding Paths
  */</span>
  createChildViews<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: #003366; font-weight: bold;">var</span> childViews <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> view<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//Add the search text field</span>
    view <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">createChildView</span><span style="color: #009900;">&#40;</span>
      SC.<span style="color: #660066;">TextFieldView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #006600; font-style: italic;">/* layout here */</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        valueBinding<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'searchPath'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#123;</span> rootElementPath<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    childViews.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    view <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">createChildView</span><span style="color: #009900;">&#40;</span>
      SC.<span style="color: #660066;">ListView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* layout here */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
        contentValueKey<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contentValueKey'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        contentBinding<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contentPath'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        selectionBinding<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selectionPath'</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;">&#123;</span> rootElementPath<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    childViews.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'childViews'</span><span style="color: #339933;">,</span> childViews<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #009966; font-style: italic;">/* Do Special Stuff as Needed */</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then, as you need to create instances of your composite view in the &#8216;.lproj&#8217; layer it would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  aRealMasterView<span style="color: #339933;">:</span> myApp.<span style="color: #660066;">MasterView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* layout here */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    contentPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.myRecordsController.arrangedObjects'</span><span style="color: #339933;">,</span>
    selectionPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.myRecordsController.selection'</span><span style="color: #339933;">,</span>
    searchPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.myRecordsController.search'</span><span style="color: #339933;">,</span>
    contentValueKey<span style="color: #339933;">:</span> <span style="color: #3366CC;">'name'</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h2>Conclusion</h2>
<p>Its as simple as this.  Now you are able to pass your bindings around without creating superfluous bindings that have to be updated, but have no real using in your composite view layer.  The &#8216;-Binding&#8217; convention is a great convenience, but unless you specifically want to create a binding in that view layer it is easily misuse and can cause performance hits.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/07/29/composite-views/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Bindings Unleashed</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/06/20/bindings-unleashed/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/06/20/bindings-unleashed/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 12:45:30 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Coding Tutorials]]></category>
		<category><![CDATA[Sproutcore]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=108</guid>
		<description><![CDATA[Bindings are one of the most important aspects of Sproutcore. Bindings give us the ability to do complex UI interactions with very little code.  They have turned tedious and error-prone code to update the interface a thing of the past.  It now becomes a simple setup and configuration task to wire up things [...]]]></description>
			<content:encoded><![CDATA[<p>Bindings are one of the most important aspects of Sproutcore. Bindings give us the ability to do complex UI interactions with very little code.  They have turned tedious and error-prone code to update the interface a thing of the past.  It now becomes a simple setup and configuration task to wire up things to be automatically updated. They are incredibly powerful, but can be easily confusing and misused.</p>
<p><span id="more-108"></span></p>
<h2>the basics</h2>
<p>Bindings are just configuration that one sets up so that when a value changes it changes all the elements that are connected to it.  They are found most often in the view layer, but are used heavily in the controller and the model as well.  We will focus on the view layer.  Here is a simple picture of what bindings do for you:<br />
<img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/06/bindings_views.png" alt="Bindings-Views" height="287" width="415"></p>
<p>The general concept is that all the views connect to one value &#8216;name&#8217; in the controller so that if it changes at any time it is reflected automatically in the views.  This is the power of bindings.</p>
<h2>the basic rules for bindings</h2>
<p>There are a few simples rules to follow when using bindings in sproutcore.  If you follow these, you will have a much better understanding of what is occurring and will make code debugging much easier.  Often times if your model and controller layers are set up correctly, most of your bugs will ben in your bindings.</p>
<h3>Rule #1: Use the &#8216;-Binding&#8217; naming convention ONLY for a bindings</h3>
<p>When you set up a binding you uses the following convention:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// DO THIS </span>
  nameBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.modelController.name'</span><span style="color: #339933;">,</span>
  <span style="color: #006600; font-style: italic;">// NOT THESE</span>
  skiBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Rossignol'</span>
  glueBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Very Firm'</span></pre></div></div>

<p>Often you might want to pass bindings through a view to another view.  DO NOT use the &#8216;-Binding&#8217; convention!  Think of anything with the postfix of &#8216;-Binding&#8217; as a reserved word. This will set up a real binding that will update whenever something changes it will update.  This will slow your app down as things are being updated without the need.</p>
<h3>Rule #2: Use the &#8216;-Path&#8217; naming convention to pass a binding to another view</h3>
<p>If you do need to pass a binding to a child view or pass it around for some reason, use the &#8216;-Path&#8217; naming convention:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  namePath<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.modelController.name'</span></pre></div></div>

<p>A further discussion of using this convention can be found in the discussion about <a href='http://www.itsgotwhatplantscrave.com/2009/07/29/composite-views/'>composite views</a>.</p>
<h3>Rule #3: View Bindings must only be set up in the &#8216;.lproj&#8217; layer</h3>
<p>Fight the urge to set up bindings for the view layer anywhere but in the &#8216;.lproj&#8217; layer.  You should never be directly setting bindings in a view class even if you know what something is supposed to be.  This is a maintenance nightmare and will cause lots of problems if you ever have to change something.  It also violates some of the basic OO design structure. If you need a bind set up further down, use the &#8216;-Path&#8217; naming convention.</p>
<h3>Rule #4: Use One Way Bindings whenever possible</h3>
<p>Always use a One Way Binding when you can. This will speedup performance because the SC RunLoop won&#8217;t have to do extra work to update values and observe values on both ends. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  nameBinding<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Binding</span>.<span style="color: #660066;">from</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myApp.modelController.name'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneWay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This is particularly useful when you are creating views that are for display purposes only.  There is no need to have any changes to the view be observed for change.</p>
<h2>Using Bindings in Sproutcore</h2>
<p>When you set up bindings you are actually setting up a binding pattern that is created at run time in the <b>init()</b> method.  This is why it is very important to add the <b>sc_super()</b> to the top of your <b>init()</b> if you ever overwrite it.</p>
<h3>The &#8216;*&#8217; Trek Convention</h3>
<p>Often times you will see a &#8216;*&#8217; being used in some bindings in sproutcore.  This little feature is one of the massive improvements that sproutcore has made to Binding from Cocoa.  The &#8216;*&#8217; creates a chained observation structure that is very powerful and causes less code to have to be created.</p>
<p>The &#8216;*&#8217; creates the observation point in the binding observation chain. Everything to the left of the &#8216;*&#8217; is the object that is to be observed and everything to the right of the &#8216;*&#8217; is the property chain that is being observed for changes. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  nameBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.modelController*otherProperty.name'</span></pre></div></div>

<p>An english reading of this would be: &#8220;Observe &#8216;myApp.modelController&#8217; for changes to the property &#8216;otherProperty.name&#8217;&#8221;.  This means that any change to &#8216;otherProperty&#8217; or &#8216;otherProperty.name&#8217; will trigger a binding update. This is particularly useful when you want to observe an array for changes to the whole thing or to the size of the array.</p>
<p>You never want to add a &#8216;*&#8217; to the last property it is already implied:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  nameBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.modelController.name'</span>
  <span style="color: #006600; font-style: italic;">// Same as...</span>
  nameBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myApp.modelController*name'</span></pre></div></div>

<h3>Absolute and Relative Paths</h3>
<p>Most of the time you will be using an absolute path for setting up your bindings.  This is the most common form that you will use.  Often you might want to make a relative path for a binding.  This will be used when you want to bind a child view to a parent view in a view class.  You can still use the &#8216;*&#8217; naming convention. Here is an example of a relative path:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  nameBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'.parentView.name'</span>
  descriptionBinding<span style="color: #339933;">:</span> <span style="color: #3366CC;">'.parentView*otherObject.name'</span></pre></div></div>

<h3> Setting up bindings implicitly</h3>
<p>There are a few times where you might want to set up binding explicitly.  The best to use the <b>.bind()</b> method on SC.Object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">,</span> SC.<span style="color: #660066;">Binding</span>.<span style="color: #660066;">from</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myApp.modelController.name'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneWay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You will want to use this rarely and only when you want to do some dynamic binding that can change or morph.  For the general uses, you want to avoid using this at all costs, but it is good to know for future references.</p>
<h2>conclusion</h2>
<p>Bindings are incredibly powerful and are the base of what makes Sproutcore so cool.  In a later post we will talk about how to use bindings to your advantage in the controller layer with the Root Controller methodology.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/06/20/bindings-unleashed/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Chromium</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/05/25/chromium/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/05/25/chromium/#comments</comments>
		<pubDate>Mon, 25 May 2009 22:23:05 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=106</guid>
		<description><![CDATA[I guess I&#8217;m a little late to the party but you can get daily builds of Chromium for the mac.  Pretty cool 
]]></description>
			<content:encoded><![CDATA[<p>I guess I&#8217;m a little late to the party but you can get <a href="http://build.chromium.org/buildbot/snapshots/sub-rel-mac/">daily builds</a> of Chromium for the mac.  Pretty cool </p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/05/25/chromium/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Sproutcore Apps with Statecharts (part 2)</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/02/22/building-sproutcore-apps-with-statecharts-part-2/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/02/22/building-sproutcore-apps-with-statecharts-part-2/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 22:36:56 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=86</guid>
		<description><![CDATA[OK, What is it really look like&#8230;
So you might be reading the last post and got excited want to try it in one of your own applications.  Then, the Reality Fairy drops by to seed doubt and you say, &#8216;Great, but what does it look like in real life&#8217;.  So we have created [...]]]></description>
			<content:encoded><![CDATA[<h2>OK, What is it really look like&#8230;</h2>
<p>So you might be reading the last post and got <a href="http://www.youtube.com/watch?v=pFlcqWQVVuU">excited</a> want to try it in one of your own applications.  Then, the Reality Fairy drops by to seed doubt and you say, &#8216;Great, but what does it look like in real life&#8217;.  So we have created an <a href= "http://www.itsgotwhatplantscrave.com/sc-apps/">example application</a> to show how it is done. First, we will start with the statecharts.  This is a very simple application of a contact address book.</p>
<p><span id="more-86"></span></p>
<h2>Using Statecharts for GUI Interactions&#8230;</h2>
<p>The first statechart is of the general application structure and workflow:<br />
<img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/02/contacts-tool.png" alt="Contacts Tool" height="330" width="612"></p>
<p>You can see from the chart that there are two states that have substates.  This one of the powers of statecharts in limiting the complexity of the application and diagram.  The next statechart is for the <strong>File Upload Wizard</strong>:</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/02/wizard.png" alt="Wizard" width="506" height="355" /></p>
<p>Next, we will show the last state chart for the failure state:</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/02/failure.png" alt="Failure" width="710" height="311" /></p>
<h2>Using Statecharts for Server Calls&#8230;</h2>
<p>We created a new collection object that handles the calls to the server.  The reason that this is helpful is that now you can use the objects currently loaded in the collection and then refresh the list in the background seamlessly.  This is the state chart:</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/02/collection-statechart.png" alt="Collection Statechart" width="572" height="327" /></p>
<h2>Example application</h2>
<p>We created a really simple application called <a href="http://www.itsgotwhatplantscrave.com/sc-apps/">Contact Tool.</a>  It&#8217;s a port of the contacts sproutcore sample app to the new bitburger view with states for server calls.  We&#8217;ve left all the state transitions explicit so you can see it go through all the states.  You can also download the source from github <a href="http://github.com/onkis/contacts-tool"> here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/02/22/building-sproutcore-apps-with-statecharts-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building Sproutcore Apps with Statecharts (part 1)</title>
		<link>http://www.itsgotwhatplantscrave.com/2009/02/22/building-sproutcore-apps-with-statecharts-part-1/</link>
		<comments>http://www.itsgotwhatplantscrave.com/2009/02/22/building-sproutcore-apps-with-statecharts-part-1/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 22:18:15 +0000</pubDate>
		<dc:creator>Evin</dc:creator>
				<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://www.itsgotwhatplantscrave.com/?p=63</guid>
		<description><![CDATA[
Introduction
In this post, we will discuss creating a sproutcore application using a modeling method called &#8216;Statecharts&#8217;.  This was invented by a man named David Harel, who invented it in order to have a state diagram that could model things like: superstates; concurrent states; and activities as part of a state.  These are starting [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/02/state-types.png" alt="State Types" height="230" width="358"></p>
<h2>Introduction</h2>
<p>In this post, we will discuss creating a sproutcore application using a modeling method called &#8216;Statecharts&#8217;.  This was invented by a man named <a href='http://en.wikipedia.org/wiki/David_Harel'>David Harel</a>, who invented it in order to have a state diagram that could model things like: superstates; concurrent states; and activities as part of a state.  These are starting to gain more use in the software development world because of the distinct advantages that it provides to developers.  These advantages include, but are not limited to:</p>
<p><span id="more-63"></span></p>
<ul>
<li>Usability Testing</li>
<li>Interaction Testing</li>
<li>Scope Definition</li>
<li>Bug Discovery (they&#8217;re in the statechart)</li>
<li>Real Bugs are easier to Find/Fix</li>
</ul>
<p>Statecharts also provide the ability to model cross-state events and interactions on multiple levels.  This reduces the complexity of the diagrams and pre-segments the application into functional areas for development.  You often find that once you get an &#8216;area&#8217; correct is just stays &#8216;correct&#8217; and make modification to the code a lot easier.  There is another great paper on use of statecharts <a href='http://www.wisdom.weizmann.ac.il/~dharel/SCANNED.PAPERS/Statecharts.pdf'>here</a>.</p>
<h2>Using Statecharts in Sproutcore</h2>
<p>One of the distinct advantages of Statecharts is that you can actually use them in your code.  That way you can match your statecharts directly to the code.  This is accomplished in sproutcore with a <a href='http://github.com/onitunes/sproutcore-statechart/tree/master'>plugin</a> that Erich Ocean wrote.  You can install it with the Sproutcore Build tools:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  sc<span style="color: #339933;">-</span>install onitunes<span style="color: #339933;">-</span>statechart</pre></div></div>

<p>The best structure for using statecharts in Sproutcore is to make two new files: <b>core_states.js</b> and <b>core_actions.js</b>.<br />
<b>core_states.js</b> should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core_actions'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MyApp.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span> <span style="color: #006600; font-style: italic;">/** @scope MyApp */</span><span style="color: #009900;">&#123;</span>
    goStateA1<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;">// TODO: Settings for State A1</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    goStateA2<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;">// TODO: Settings for State A2</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    goStateB1<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;">// TODO: Settings for State B1</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    goStateB2<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;">// TODO: Settings for State B2</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>core_actions.js</b> should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'core'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  MyApp.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span> <span style="color: #006600; font-style: italic;">/** @scope MyApp */</span><span style="color: #009900;">&#123;</span>
    submitLogin<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: #003366; font-weight: bold;">var</span> handled <span style="color: #339933;">=</span> NO<span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">state</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span>
            <span style="color: #006600; font-style: italic;">// TODO: More actions</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">goState</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            handled <span style="color: #339933;">=</span> YES<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>handled<span style="color: #009900;">&#41;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submitLogin Action not handled in state %@[%@]'</span>.<span style="color: #660066;">fmt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">state</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    reauthenticate<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: #003366; font-weight: bold;">var</span> handled <span style="color: #339933;">=</span> NO<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">state</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span>
            <span style="color: #006600; font-style: italic;">// TODO: More Actions</span>
            <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You need to Reauthenticate...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">goState</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            window.<span style="color: #660066;">location</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/login.aspx&quot;</span><span style="color: #339933;">;</span>
            handled <span style="color: #339933;">=</span> YES<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>handled<span style="color: #009900;">&#41;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'reauthorize Action not handled in state %@[%@]'</span>.<span style="color: #660066;">fmt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">state</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>What do statecharts look like?</h2>
<p>Statecharts look a lot like state diagrams and are comprised of boxes and lines with some standard styling to indicate what they are. Major areas of statecharts are divided into two different types: <b>States</b> and <b>Events</b>.</p>
<h3>States</h3>
<p>States are further divided into the following type of states: <b>Start</b>, <b>End</b>, <b>Switch</b>, <b>External</b>, <b>Transient</b>, <b>States</b>, <b>State w/ substates</b>, and <b>History</b></p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/02/state-types.png" alt="State Types" height="230" width="358"></p>
<h3>Rules for States</h3>
<p>There are several rules for the different state types. <b>Start</b>, <b>End</b>, <b>External</b>, and <b>History</b> States are more functional indications than real states.  They tell you how the application is supposed to function and basically modify the states they are linked to. <b>States</b> and <b>State w/ substates</b> are the most common and the most used.  These state are only supposed to be setting values.  There should be no &#8216;if&#8217; or &#8217;switch&#8217; statements in these states.  This is where the power of statecharts is revealed when you are in a state, it looks only one way. The last two states: <b>Switch</b> and <b>Transient</b> are special.  <b>Switch</b> states are allowed to have &#8216;if&#8217; and &#8217;switch&#8217; statements but only those statements. <b>Transient</b> states are &#8216;holding&#8217; states with only <b>Auto</b> exit events.</p>
<p>This is what they look like in the code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  goStateA1<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>
    MyApp.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pageView'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'MyApp.loginPage.mainView'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MyApp.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'globalNavigationMenu'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'MyApp.navigationPage.globalMenuView'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  goStateA2<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>
    MyApp.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pageView'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'MyApp.bodyPage.mainView'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MyApp.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'globalNavigationMenu'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'MyApp.navigationPage.globalMenuView'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<h3>Events (transitions)</h3>
<p>Events are further divided into the following types: <b>Regular</b> and <b>Auto</b>. They can linked to <b>states</b> and <b>sub-states</b>; and they can have <b>parameters</b> and/or <b>conditions</b>. They are represented by arrowed lines.</p>
<p><img src="http://www.itsgotwhatplantscrave.com/wp-content/uploads/2009/02/event-types.png" alt="Event Types" height="133" width="284"></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  submitLogin<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: #003366; font-weight: bold;">var</span> handled <span style="color: #339933;">=</span> NO<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">state</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span>
          <span style="color: #006600; font-style: italic;">// TODO: More actions</span>
          <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">goState</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          handled <span style="color: #339933;">=</span> YES<span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>handled<span style="color: #009900;">&#41;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submitLogin Action not handled in state %@[%@]'</span>.<span style="color: #660066;">fmt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">state</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<h3>Miscellaneous State Objects:</h3>
<p>In addition to the major components of States and Events, there are several other elements that are useful.  They include:</p>
<ul>
<li><b>Grouping</b>: This is where you box a few state that have the same exit\entry events so that you can clean up the diagram
<li><b>Parallel States</b>: These are concurrent states that are mutually exclusive.  This happens rarely.
<li><b>Indexing</b>:  Once you create the state chart you index the states numerically and group them by alphabetical area
</ul>
<h2>Where To Use Statecharts</h2>
<h3>GUI Interactions</h3>
<p>The primary use of statecharts in development is to describe the function of the application. This is a interaction map of the application.</p>
<h3>Object Life Cycle</h3>
<p>For complex models, you can use statecharts to handle the life cycle of the object.  This allows different interactions to occur and can gate the object.</p>
<h3>Server Communications</h3>
<p>Statecharts are perfect for handling complex calls to the server and lazy loading of information.  You can use them for interactive speed gains such as a quick load of object followed by a larger load of objects in the background.</p>
<h2>Conclusion</h2>
<p>Statecharts are a great tool to use in developing interactive applications.  They provide a great way to segment and develop the application with fewer bugs.  Sproutcore comes with great plugin to help you create solid applications.  We will have a follow up post to show you what is really looks like.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.itsgotwhatplantscrave.com/2009/02/22/building-sproutcore-apps-with-statecharts-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.104 seconds -->
