{"id":332,"date":"2011-06-07T09:12:52","date_gmt":"2011-06-07T09:12:52","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=332"},"modified":"2011-06-09T06:35:34","modified_gmt":"2011-06-09T06:35:34","slug":"creating-a-sencha-touch-mvc-application-from-scratch-part-1","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-1\/","title":{"rendered":"Creating a Sencha Touch MVC application from scratch, part 1"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Sencha Touch is an OOP JavaScript framework that makes it easy to build mobile web applications that look and feel native on iPhone, Android, and BlackBerry touch devices. To find more you can take a look at <a title=\"Sencha Touch\" href=\"http:\/\/www.sencha.com\/products\/touch\/\" target=\"_blank\">Sencha Touch website<\/a>.<\/p>\n<p>If you haven&#8217;t worked with JavaScript in an object-oriented way, I think you&#8217;ll find the framework a bit strange since it&#8217;s completely based on OOP. If you are not familiar with OOP concepts you could take a look <a title=\"wikipedia OOP\" href=\"http:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\" target=\"_blank\">here<\/a>. If you have some OOP knowledge, but you don&#8217;t know how that works in JavaScript you can read this <a title=\"Object-Oriented JavaScript\" href=\"https:\/\/developer.mozilla.org\/en\/Introduction_to_Object-Oriented_JavaScript\" target=\"_blank\">Object-Oriented JavaScript article<\/a>.<\/p>\n<p>Another concept you should know before we dive into coding is MVC pattern (Model View Controller). If you don&#8217;t know what MVC is you should read <a title=\"Model\u2013view\u2013controller\" href=\"http:\/\/en.wikipedia.org\/wiki\/Model%E2%80%93view%E2%80%93controller\" target=\"_blank\">this article<\/a>.<\/p>\n<h2>Let&#8217;s start<\/h2>\n<p>Now, let&#8217;s start creating our first Sencha Touch MVC web app.<\/p>\n<p>First create the project folder on you hard-drive. I named the project MvcTouch.<\/p>\n<p>In the project folder you should create the following structure:<\/p>\n<p><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-project-structure.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-420\" title=\"MvcTouch project structure\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-project-structure.jpg\" alt=\"MvcTouch project structure\" width=\"206\" height=\"129\" \/><\/a><\/p>\n<p>In the <strong>app <\/strong>folder we will put all the application code. For now we only created <em>app.js<\/em> file in there, but as we move forward will add more folders and files.<\/p>\n<p>The <strong>lib <\/strong>folder will contain the Sencha Touch framework and other JavaScript libraries, if necessary.<\/p>\n<p>All the <strong>images <\/strong>and <strong>CSS <\/strong>files will go in the <strong>res <\/strong>folder.<\/p>\n<p>The file <em>index.html<\/em> is the application main-entry point and the only html file we&#8217;ll have.<\/p>\n<p>Now you should go and <a title=\"download Sencha Touch\" href=\"http:\/\/www.sencha.com\/products\/touch\/download\/\" target=\"_blank\">download<\/a> the Sencha Touch framework if you haven&#8217;t did that already.<\/p>\n<p><strong>Let&#8217;s include the framework in our project:<\/strong><\/p>\n<p>Create a folder named<strong> sencha-touch-1.1.0<\/strong> in the <strong>lib <\/strong>folder.<\/p>\n<p>Now copy in <strong><em>lib\/sencha-touch-1.1.0\/<\/em><\/strong> the <strong>sencha-touch.js<\/strong> file from the downloaded package (<strong>sencha-touch-1.1.0\/sencha-touch.js<\/strong>).<\/p>\n<p>We also need the Sencha Touch CSS file so let&#8217;s include that also in<strong> lib\/sencha-touch-1.1.0\/sencha-touch.css<\/strong> (the path to this file in the downloaded package is: <strong>sencha-touch-1.1.0\/resources\/css\/sencha-touch.css<\/strong>).<\/p>\n<p>This two files are all we need to add in our project.<\/p>\n<h2><strong>Now the fun part, the coding<\/strong><\/h2>\n<p>First, we need to add some content to our <em>index.html<\/em><strong>:<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;!DOCTYPE html&gt;\r\n &lt;html&gt;\r\n &lt;head&gt;\r\n &lt;title&gt;MvcTouch&lt;\/title&gt;\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\r\n &lt;\/head&gt;\r\n &lt;body&gt;&lt;\/body&gt;\r\n &lt;\/html&gt;\r\n\r\n<\/pre>\n<p>Nothing complicated here, we just added the Sencha Touch required files (the js and css file) and we&#8217;ve also included our app.js file.<\/p>\n<p><strong>Let&#8217;s write some JavaScript code now.<\/strong><\/p>\n<p>Open <strong>app.js<\/strong> and add the following in it:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\nExt.regApplication({\r\n    name: 'App',\r\n    launch: function()\r\n    {\r\n        alert(&quot;Yay, it's working!&quot;);\r\n    }\r\n});\r\n\r\n<\/pre>\n<p><strong>Great, but what this code does?!<\/strong> Well, it creates our application.<\/p>\n<p>We call <strong>Ext.regApplication<\/strong> function (Ext is the namespace Sencha Touch uses) by passing a configuration object with the name of our application and a function to execute when everything has loaded and the application can do &#8220;our stuff&#8221;.\u00a0 Here, we&#8217;ve just added an alert box just for testing to see if everything it&#8217;s working fine.<\/p>\n<p>Please note that the application name is very important because it will be our namespace in which we&#8217;ll add our custom views, controllers etc. A little fragment from Sencha Touch documentation:<\/p>\n<blockquote><p>&#8220;<em>Instantiating a new application automatically creates a global  variable using the configured\u00a0name property and sets up namespaces for  views, stores, models and controllers within the app<\/em>.&#8221;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n\/\/this code is run internally automatically when creating the app\r\nExt.ns('MyApp', 'MyApp.views', 'MyApp.stores', 'MyApp.models', 'MyApp.controllers');\r\n\r\n<\/pre>\n<\/blockquote>\n<p>So, for example, we will have something like this later in our code: <em>App.views.HomeMainView<\/em>.<\/p>\n<p><strong>Ok, but why <em>App <\/em>and not <em>MvcTouch <\/em>as the name of our project?<\/strong><\/p>\n<p><strong>App <\/strong>is shorter and make more sense because is more consistent. If we named it like the project name, MvcTouch, we will than have <em>MvcTouch.views.HomeMainView etc. <\/em>Later when you&#8217;ll write another app if you will name the application instance again like the project name you&#8217;ll have: <em>AnotherApp.views.HomeView<\/em>. However, if we always name the application instance as App in all of our projects, we will find the &#8220;things&#8221; more quicker: the views will be always in <em>App.views.*<\/em>, the controllers in <em>App.controllers.* etc.<br \/>\n<\/em><\/p>\n<p>Now, open <em>index.html <\/em>in Safari or Google Chrome and see if the alert is showing.<\/p>\n<p><strong>Why Safari or Google Chrome? Why not Firefox also?<\/strong><\/p>\n<p>Sencha Touch role is to build web apps that run on mobile devices, not on desktop browsers. The reason why Safari and Google Chrome can handle a Sencha Touch app is because they are build on top of the webkit browser engine, like a lot of browsers from mobile devices.<strong> <\/strong><\/p>\n<p><strong>That&#8217;s it for part 1!<\/strong><\/p>\n<p><strong><\/strong>You can download the current phase of the project from <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/06\/MvcTouch-part-1.zip\">here<\/a>.<strong><\/strong><\/p>\n<p>If you have questions, suggestions or improvements don&#8217;t 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 1\" \/>  \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-1\/\" \/>  \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>Introduction Sencha Touch is an OOP JavaScript framework that makes it easy to build mobile web applications that look and feel native on iPhone, Android, and BlackBerry touch devices. To find more you can take a look at Sencha Touch website. If you haven&#8217;t worked with JavaScript in an object-oriented way, I think you&#8217;ll find &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-1\/\">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-1\/\"\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":[90,91,89],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/332"}],"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=332"}],"version-history":[{"count":68,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/332\/revisions"}],"predecessor-version":[{"id":353,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/332\/revisions\/353"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}