{"id":488,"date":"2011-06-20T10:36:50","date_gmt":"2011-06-20T10:36:50","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=488"},"modified":"2011-06-20T10:36:50","modified_gmt":"2011-06-20T10:36:50","slug":"creating-a-sencha-touch-mvc-application-from-scratch-part-2","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-2\/","title":{"rendered":"Creating a Sencha Touch MVC application from scratch, part 2"},"content":{"rendered":"<p>As the title says we will continue to build our <strong>Sencha Touch MVC app<\/strong> we&#8217;ve started  in <a title=\"Creating a Sencha Touch MVC application from scratch, part 1\" href=\"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-1\/\">part 1<\/a>. In this part we will create a <strong>controller<\/strong> and two <strong>views<\/strong> and learn how to use them.<\/p>\n<p>Let&#8217;s modify our current project structure by adding some folders:<\/p>\n<p><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-project-structure21.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-541\" title=\"MvcTouch project structure 2\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-project-structure21.jpg\" alt=\"MvcTouch project structure 2\" width=\"210\" height=\"204\" \/><\/a><\/p>\n<h2>Creating a controller.<\/h2>\n<p>In <strong>app\/controllers<\/strong> create a file named <strong>HomeController.js<\/strong> and add the following content to it:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nExt.regController('Home', {\r\n\r\n\t\/\/ index action\r\n\tindex: function()\r\n\t{\r\n\t\tExt.Msg.alert('Test', &quot;Home's index action was called!&quot;);\r\n\t},\r\n});\r\n<\/pre>\n<p>We create a <a title=\"Sencha Touch Controller\" href=\"http:\/\/dev.sencha.com\/deploy\/touch\/docs\/?class=Ext.Controller\">controller<\/a> by registering within our application with the function <strong>Ext.regController <\/strong>with two parameters: the <strong>name <\/strong>of the controller and a configuration object. In the configuration object we will add our custom properties and functions\/<strong>actions<\/strong>.<\/p>\n<p>A controller action is a function that is called automatically based on a <strong>route<\/strong>. A route is an identifier for a <strong>controller\/action<\/strong> pair and looks like an url: <strong>home\/index<\/strong>, <strong>home\/contact<\/strong> etc.<\/p>\n<p><strong>Note: <\/strong>when you add a custom property or a function to the controller you should always add a comma at the end of it to avoid any errors when you later add other properties or functions.<\/p>\n<p><strong>Another note: <\/strong>you should always have the console of the browser opened to watch for errors.<\/p>\n<h2>The default route (controller\/action pair)<\/h2>\n<p>Every Sencha Touch MVC application will have a default route loaded on startup. Before we start using routes in our application we need to define their structure.<\/p>\n<p>In <strong>app<\/strong> folder create a file named <strong>routes.js<\/strong> and add the following content:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nExt.Router.draw(function(map) {\r\n\tmap.connect(':controller\/:action');\r\n});\r\n<\/pre>\n<p>With the code above a route will be constructed by two parts: the controller and the action, separated by a forward slash. For more info about a route you could check the <a title=\"Sencha Touch Ext.Router\" href=\"http:\/\/dev.sencha.com\/deploy\/touch\/docs\/?class=Ext.Router\">documentation<\/a>.<\/p>\n<p><strong>Let&#8217;s set our default route in our application<\/strong>.<\/p>\n<p>Open <strong>app.js<\/strong> and set the <strong>defaultUrl<\/strong> property to point to <strong>Home\/index<\/strong> route like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nExt.regApplication({\r\n\tname: 'App',\r\n\tdefaultUrl: 'Home\/index',\r\n\tlaunch: function()\r\n\t{\r\n\t},\r\n});\r\n<\/pre>\n<p>Before testing our application we need to include the created files in the <strong>index.html<\/strong> file:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;!-- Sencha Touch framework --&gt;\r\n&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;lib\/sencha-touch-1.1.0\/sencha-touch.css&quot; \/&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;lib\/sencha-touch-1.1.0\/sencha-touch.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;app\/app.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;app\/routes.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;!-- CONTROLLERS --&gt;\r\n&lt;script src=&quot;app\/controllers\/HomeController.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;!-- VIEWS --&gt;\r\n\r\n<\/pre>\n<p><strong>Note: <\/strong>When you&#8217;ll start creating more controllers, views etc. there is a possibility that you&#8217;ll forget this step and wonder why things are not working as expected. So, <strong>don&#8217;t forget to include your files in index.html<\/strong>.<\/p>\n<p><strong>Ready for testing! <\/strong><\/p>\n<p>Open index.html in a webkit based browser and you should see a message box.<a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-alert.jpg\"><\/a><\/p>\n<h2>Creating views<\/h2>\n<p>Our views will be created by extending the Sencha Touch components and they will be responsible with the rendering of our data. With them we make our app look cool.<\/p>\n<p><strong>Creating the main view: the Viewport<\/strong><\/p>\n<p>The first view we need to create when we start a new app will be the <strong>viewport<\/strong>. This is our main view in which we&#8217;ll render the other ones. You can think of it like a canvas.<strong> <\/strong><\/p>\n<p>in our <strong>app\/views<\/strong> folder create a file named <strong>Viewport.js <\/strong>and add the following content in it:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nApp.views.Viewport = Ext.extend(Ext.Panel, {\r\n\tfullscreen: true,\r\n\tlayout: 'card',\r\n\tcardSwitchAnimation: 'slide',\r\n\tdockedItems: [\r\n\t\t{\r\n\t\t\txtype: 'toolbar',\r\n\t\t\ttitle: 'MvcTouch',\r\n\t\t},\r\n\t],\r\n});\r\n<\/pre>\n<p><strong>What&#8217;s in a view?<\/strong><\/p>\n<p>We create our view in <strong>App.views<\/strong> namespace created for us automatically by the application registration.<\/p>\n<p>Our views will be subclasses of an existing component\/class. We create them using the function <strong>Ext.extend<\/strong> by passing two parameters to it: the superclass (the component we want to extend) and an object in which we&#8217;ll add properties and functions.<\/p>\n<p>For our viewport we&#8217;ve choosed\u00a0 to extend the <a title=\"Sencha Touch Panel\" href=\"http:\/\/dev.sencha.com\/deploy\/touch\/docs\/?class=Ext.Panel\">Panel<\/a> class and setting some properties: <strong>the fullscreen<\/strong> property\u00a0to <strong>true<\/strong> in order to take all the available space;\u00a0the <strong>layout<\/strong> <strong> <\/strong>property to <strong>&#8216;card&#8217;<\/strong> so that only one child component will be visible at a time<strong>; <\/strong>the <strong>cardSwitchAnimation<\/strong> to <strong>&#8216;slide&#8217;<\/strong> will handle how the child components will be showed on screen.<\/p>\n<p>About the final property, <strong>dockedItems<\/strong>, Sencha Touch says: &#8220;<em>A component or series of components to be added as docked items to this panel. The docked items can be docked to either the top, right, left or bottom of a panel. This is typically used for things like toolbars or tab bars<\/em>&#8220;.<\/p>\n<p>If you don&#8217;t know what is an\u00a0<strong>xtype<\/strong> you should read <a title=\"xtype defined\" href=\"http:\/\/www.sencha.com\/learn\/Tutorials:xtype_defined\">this<\/a>.<\/p>\n<p>Now we need to instantiate the viewport in our launch function of our app:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlaunch: function()\r\n{\r\n\tthis.viewport = new App.views.Viewport();\r\n},\r\n<\/pre>\n<p>We assign our viewport instance to a property in our app so that we can access it later in our controller this way: <strong>this.application.viewport<\/strong>.<\/p>\n<p>Before testing what we have so far, we need to include the viewport file in our index.html, under the views comment:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- VIEWS --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;app\/views\/Viewport.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p><strong>Now you can test!<\/strong> You should have something that looks like this:<\/p>\n<p><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-viewport-toolbar.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-548\" title=\"MvcTouch viewport toolbar\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-viewport-toolbar.jpg\" alt=\"MvcTouch viewport toolbar\" width=\"297\" height=\"437\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-viewport-toolbar.jpg 297w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-viewport-toolbar-203x300.jpg 203w\" sizes=\"(max-width: 297px) 100vw, 297px\" \/><\/a><\/p>\n<p><strong>Creating a controller specific view.<br \/>\n<\/strong><\/p>\n<p>For the views used by a specific controller we will create a new folder, named as the controller (lowercase), in <strong>app\/views<\/strong> to add them. So, create a folder named <strong>home <\/strong>in app\/views and in that create a file named <strong>HomeIndexView.js<\/strong>. Include this file in index.html<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- VIEWS --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;app\/views\/Viewport.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;app\/views\/home\/HomeIndexView.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>and add the following content in it:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nApp.views.HomeIndex = Ext.extend(Ext.Panel, {\r\n\thtml: &quot;&lt;h3&gt;A Lover's Complaint&lt;\/h3&gt;&quot; +\r\n\t\t&quot;&lt;h4&gt;a poem by&lt;\/h4&gt;&quot; +\r\n\t\t&quot;&lt;h3&gt;William Shakespeare&lt;\/h3&gt;&quot; +\r\n\t\t&quot;&lt;p&gt;From off a hill whose concave womb reworded&lt;\/br&gt;&quot; +\r\n\t\t&quot;A plaintful story from a sist'ring vale,&lt;\/br&gt;&quot; +\r\n\t\t&quot;My spirits t'attend this double voice accorded,&lt;\/br&gt;&quot; +\r\n\t\t&quot;And down I laid to list the sad-tuned tale,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Ere long espied a fickle maid full pale,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Tearing of papers, breaking rings atwain,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Storming her world with sorrow's wind and rain.&lt;\/br&gt;&quot; +\r\n\t\t&quot;Upon her head a platted hive of straw,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Which fortified her visage from the sun,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Whereon the thought might think sometime it saw&lt;\/br&gt;&quot; +\r\n\t\t&quot;The carcase of a beauty spent and done.&lt;\/br&gt;&quot; +\r\n\t\t&quot;Time had not scythed all that youth begun,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Nor youth all quit, but spite of heaven's fell rage&lt;\/br&gt;&quot; +\r\n\t\t&quot;Some beauty peeped through lattice of seared age.&lt;\/p&gt;&quot; +\r\n\r\n\t\t&quot;&lt;p&gt;Oft did she heave her napkin to her eyne,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Which on it had conceited characters,&lt;\/br&gt;&quot; +\r\n\t\t&quot;Laund'ring the silken figures in the brine&lt;\/br&gt;&quot; +\r\n\t\t&quot;That seasoned woe had pelleted in tears,&lt;\/br&gt;&quot; +\r\n\t\t&quot;And often reading what contents it bears;&lt;\/br&gt;&quot; +\r\n\t\t&quot;As often shrieking undistinguished woe&lt;\/br&gt;&quot; +\r\n\t\t&quot;In clamours of all size, both high and low.&lt;\/p&gt;&quot;,\r\n});\r\nExt.reg('HomeIndex', App.views.HomeIndex);\r\n<\/pre>\n<p>Our <strong>HomeIndexView<\/strong> makes use of the <strong>html property<\/strong>, where we&#8217;ve added some content to our view, and <strong>Ext.reg<\/strong> function to register an xtype for our class.<\/p>\n<h2>Rendering a view from a controller<\/h2>\n<p>Open <strong>HomeController.js<\/strong> and alter the index action (function) like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ index action\r\nindex: function()\r\n{\r\n\tif ( ! this.indexView)\r\n\t{\r\n\t\tthis.indexView = this.render({\r\n\t\t\txtype: 'HomeIndex',\r\n\t\t});\r\n\t}\r\n\r\n\tthis.application.viewport.setActiveItem(this.indexView);\r\n},\r\n<\/pre>\n<p>First we test if the view class wasn&#8217;t already created. If it wasn&#8217;t created we create an instance of it with the controller&#8217;s <strong>render <\/strong>function and assign it to a variable. We pass to the render function an object with our registered xtype as a property.<\/p>\n<p>After we have a view instance, we pass it as a parameter to the <strong>setActiveItem<\/strong> function of our viewport. This function will add the view into the viewport as a child.<\/p>\n<p>Refresh your browser and you&#8217;ll see how the index view shows up on the screen with a nice slide animation from the right to the left (remember the <strong>cardSwitchAnimation<\/strong> property we&#8217;ve set in our viewport).<\/p>\n<p><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-551\" title=\"MvcTouch controller view\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view.jpg\" alt=\"MvcTouch controller view\" width=\"297\" height=\"437\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view.jpg 297w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view-203x300.jpg 203w\" sizes=\"(max-width: 297px) 100vw, 297px\" \/><\/a><\/p>\n<p>Your app should look like the image above. We&#8217;ve added some html tags to our content but it&#8217;s still looking like plain text. To fix this we need to set the <strong>styleHtmlContent<\/strong> property of the view to true. Also, as you can see, our content is cut off and we have no option to scroll down. In order to enable scrolling we need to set the <strong>scroll <\/strong>property to <strong>vertical <\/strong>for that. Let&#8217;s do that.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nscroll: 'vertical',\r\nstyleHtmlContent: true,\r\nstyle: 'background: #d8e2ef',\r\n<\/pre>\n<p><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view2.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-552\" title=\"MvcTouch controller view 2\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view2.jpg\" alt=\"MvcTouch controller view 2\" width=\"297\" height=\"437\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view2.jpg 297w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-controller-view2-203x300.jpg 203w\" sizes=\"(max-width: 297px) 100vw, 297px\" \/><\/a><\/p>\n<p>We also add a style property to add some CSS rules to our view to observe the slide animation better. Now the text should look nicer on a blue background and when you will press and hold the mouse in the view and move up and down you&#8217;ll be able to scroll.<\/p>\n<p><strong>That&#8217;s it for part 2!<\/strong><\/p>\n<p>In part 3 we will see how to add more views and how to navigate through them. We&#8217;ll also add some buttons to our toolbar.<\/p>\n<p>You can download the current phase of the project from <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-part2.zip\">here<\/a>.<\/p>\n<p>If you have questions, suggestions or improvements don\u2019t hesitate to use the form below.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Creating a Sencha Touch MVC application from scratch, part 2\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-2\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>As the title says we will continue to build our Sencha Touch MVC app we&#8217;ve started in part 1. In this part we will create a controller and two views and learn how to use them. Let&#8217;s modify our current project structure by adding some folders: Creating a controller. In app\/controllers create a file named &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-2\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-2\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[88],"tags":[92,93,90,89,94],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/488"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=488"}],"version-history":[{"count":55,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/488\/revisions"}],"predecessor-version":[{"id":559,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/488\/revisions\/559"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}