<?xml version="1.0" encoding="UTF-8"?>
<wiki>
  <body>&lt;p&gt;Now we want to show the tasks in your data.  To do this, you are going to need a controller.  Controllers are where you put the spaghetti code of your application.  They help to shuffle data between your models object and your views, and help to manage committing these changes back to the server when necessary.&lt;/p&gt;
&lt;h1&gt;Creating a Controller&lt;/h1&gt;
&lt;p&gt;In our case, we are going to need one controller for now called the tasksController.  This will be a ArrayController since it helps to display a collection of records held in an array.  The SproutCore generator will help you create one of these.  On the command line, type the following:&lt;/p&gt;
&lt;pre class=&quot;console&quot;&gt;sc-gen controller Todos.tasksController SC.ArrayController&lt;/pre&gt;
&lt;p&gt;This will create a file in apps/todos/controllers/tasks.js.  Open the file and take a look at it.  It should now read:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Todos.tasksController = SC.ArrayController.create(
/** @scope Todos.tasksController.prototype */ {

  // TODO: Add your own code here.

}) ;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An SC.ArrayController behaves just like the array you set as its content, giving you the ability to bind it directly to your ListView which in turn means it will show any array you set as &amp;#8220;content&amp;#8221; property to the ArrayController, without the need of any additional work.&lt;/p&gt;
&lt;h1&gt;Hooking Up Your View&lt;/h1&gt;
&lt;p&gt;Next, we need to hook this controller up to your view.  Back in your main_page, you created an SC.ListView.  We want this list view to display the contents of the collection controller we just created.  We do this using a binding.  Bindings are like wires that connect parts of your application.  They automatically relay changes from one part of your app to the other.&lt;/p&gt;
&lt;p&gt;An ArrayController in particular has two properties that we need to bind to the list view:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;The &lt;strong&gt;arrangedObjects&lt;/strong&gt; property contains an array of Tasks for the current collection.&lt;/li&gt;
	&lt;li&gt;The &lt;strong&gt;selection&lt;/strong&gt; property contains an array with the currently selected Tasks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To bind your ListView to these new properties, you simply need to add the bindings to your design.  Open up your main page design again (in apps/todos/english.lproj/main_page.js) and edit the SC.ListView design like so:&lt;/p&gt;
&lt;p&gt;&lt;i&gt;in &lt;strong&gt;apps/todos/english.lproj/main_page.js&lt;/strong&gt;:&lt;/i&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;contentView: SC.ListView.design({
  contentBinding: 'Todos.tasksController.arrangedObjects',
  selectionBinding: 'Todos.tasksController.selection'
})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Go ahead and reload your page now.  You will notice that you task list is still empty.  Your list view is now getting some content, but it is an empty array!  The controller still doesn&amp;#8217;t have any content.  Let&amp;#8217;s fix that.&lt;/p&gt;
&lt;h2&gt;Creating an array of tasks.&lt;/h2&gt;
&lt;p&gt;When you load your SproutCore application in your web browser, SproutCore loads your views and all of your classes and then finally calls your main() function.  This is defined in apps/todos/main.js.  The last thing you will do in this function is get the initial set of data you want to display and plug it into a controller.  Let&amp;#8217;s do that now.  Open your main.js file and add the following to the bottom of the Todos.main() function in main.js:&lt;/p&gt;
&lt;p&gt;&lt;i&gt;in &lt;strong&gt;apps/todos/main.js&lt;/strong&gt;:&lt;/i&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var tasks = Todos.store.findAll(Todos.Task);
Todos.tasksController.set('content', tasks);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In your fully developed application, this code would reach out to your server and return an array of tasks from the server.  Since we are still using fixtures, this method returns an SC.RecordArray with the task objects found in your fixtures.  It then sets that record array as the content of your tasksController.&lt;/p&gt;
&lt;p&gt;Remember that a controller will &amp;#8220;project&amp;#8221; its content property to any view that is bound to it.  Go ahead and refresh now to see what happens.&lt;/p&gt;
&lt;p style=&quot;text-align:center;&quot;&gt;&lt;img src=&quot;http://img.skitch.com/20090406-crkkrgau94ap9et5th1esj6k66.png&quot; alt=&quot;Todos&quot; style=&quot;border: 1px black solid;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Eureka!  We have data!  All of your tasks are listed there.  You can even click on them to select.&lt;/p&gt;
&lt;h1&gt;Configuring Your List Items&lt;/h1&gt;
&lt;p&gt;So far so good, but the tasks do look pretty ugly.  That&amp;#8217;s because we haven&amp;#8217;t told the list view how it should interpret the task object to display it yet.  Since we haven&amp;#8217;t given it any clues, it just converts the task to a string and you get what you see here.&lt;/p&gt;
&lt;p&gt;As you might guess, you can configure how the ListView should display this by adding options in your main page again.  Edit your SC.ListView in the main page like so:&lt;/p&gt;
&lt;p&gt;&lt;i&gt;in &lt;strong&gt;apps/todos/english.lproj/main_page.js&lt;/strong&gt;:&lt;/i&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  contentView: SC.ListView.design({
    contentBinding: 'Todos.tasksController.arrangedObjects',
    selectionBinding: 'Todos.tasksController.selection',
    contentValueKey: &quot;description&quot;,
    contentCheckboxKey: &quot;isDone&quot;
  })
})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And refresh.  Now we&amp;#8217;re cooking!    You can select your items, and check the boxes.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;NOTE&lt;/span&gt;:&lt;/strong&gt; As of April 5, 2009 with the SproutCore 1.0 alpha the checkboxes are not showing their checked state.  Checking the box will change the isDone value but it will not display on screen.  This is a bug.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p style=&quot;text-align:center;&quot;&gt;&lt;img src=&quot;http://img.skitch.com/20090406-enqbmjrt18j5w3i8u2e146wbqf.png&quot; alt=&quot;Todos&quot; style=&quot;border: 1px black solid;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The contentFOOKey syntax here basically tells the ListView which properties on your content object map to the properties needed by its list items.  In this case, the &amp;#8220;value&amp;#8221; property on a list item is set to show the description and the &amp;#8220;checkbox&amp;#8221; property is set to show the isDone state.&lt;/p&gt;
&lt;p&gt;Now that you have your basic data setup, next we&amp;#8217;ll work on wiring up the remaining bits of the UI.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Continue to next step: [[Step 5: Finishing the UI]] &#187;&lt;/strong&gt;&lt;/p&gt;
&lt;h1&gt;Related Links&lt;/h1&gt;
&lt;h1&gt;Comments&lt;/h1&gt;</body>
  <created-at type="datetime">2008-07-27T17:09:10-07:00</created-at>
  <id type="integer">40069</id>
  <permalink>step-4-hook-up-your-data</permalink>
  <repository-id type="integer">37005</repository-id>
  <title>Step 4: Hook Up Your Data</title>
  <updated-at type="datetime">2009-05-12T18:01:35-07:00</updated-at>
  <user-id type="integer">157</user-id>
</wiki>
