{"id":590,"date":"2011-09-21T12:37:10","date_gmt":"2011-09-21T12:37:10","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=590"},"modified":"2011-10-08T10:24:29","modified_gmt":"2011-10-08T10:24:29","slug":"sencha-touch-tabbar-in-a-mvc-structured-application","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/javascript\/sencha-touch-tabbar-in-a-mvc-structured-application\/","title":{"rendered":"Sencha Touch TabBar in a MVC structured application"},"content":{"rendered":"<p>While I&#8217;m working on the 4th part of the <a title=\"Creating a Sencha Touch MVC application from scratch\" href=\"https:\/\/www.osd.net\/blog\/mobile-development\/creating-a-sencha-touch-mvc-application-from-scratch-part-1\/\"><strong>Creating a Sencha Touch MVC application from scratch<\/strong><\/a>\u00a0 series, I&#8217;ve decided to write this little article because the feedback received indicated that this would be useful for some of you.<\/p>\n<p>Many <strong>Sencha Touch<\/strong> developers tried or want to use the <a title=\"Sencha Touch TabPanel\" href=\"http:\/\/dev.sencha.com\/deploy\/touch\/docs\/?class=Ext.TabPanel\">TabPanel<\/a> component in a <strong>MVC structured application<\/strong> \u00a0to navigate between different routes (controller\/action pair), but this component is not intended for this purpose.<\/p>\n<p>The way to do it is to dock a <a title=\"Sencha Touch TabBar\" href=\"http:\/\/dev.sencha.com\/deploy\/touch\/docs\/?class=Ext.TabPanel?class=Ext.TabBar\">TabBar<\/a> to the bottom or to the top of the application&#8217;s viewport and handle all interaction with the tab bar buttons ourselves.<\/p>\n<p>To make it easier and to be reusable in other projects, I&#8217;ve extended the TabBar component into a class that handles all the &#8220;MVC stuff&#8221; automatically, like redirection to controller actions and setting the active tab bar button based on the current application route.<\/p>\n<p>Here&#8217;s how to use it:<\/p>\n<p><strong>1. Download <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/10\/TabBarMvc.js?update=4\">TabBarMvc.js<\/a> and add it to you project;<\/strong> a good place for it is in the <strong>app\/views<\/strong> folder. Don&#8217;t forget to include it in you index.html file:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n...\r\n&lt;!-- VIEWS --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;app\/views\/TabBarMvc.js&quot;&gt;&lt;\/script&gt;&lt;script type=&quot;text\/javascript&quot; src=&quot;app\/views\/Viewport.js&quot;&gt;&lt;\/script&gt;\r\n...\r\n<\/pre>\n<p><strong>2. Add the component as a docked item to the viewport view<\/strong> like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndockedItems: [\r\n    {\r\n    \txtype: 'TabBarMvc',\r\n    \titems: [\r\n\t        {\r\n\t        \ttext: 'Home',\r\n\t        \ticonCls: 'home',\r\n\t        \troute: 'Home\/index', \/\/ custom property of the TabBarMvc component\r\n\t        },\r\n\t        {\r\n\t        \ttext: 'About',\r\n\t        \ticonCls: 'info',\r\n\t        \troute: 'Home\/about', \/\/ custom property of the TabBarMvc component\r\n\t        },\r\n        ],\r\n    },\r\n],\r\n<\/pre>\n<p>As you can see the extended TabBar component has a new property named <strong>route<\/strong>. Based on this property the TabBarMvc will redirect to the right controller action.<\/p>\n<p>Here&#8217;s a screenshot:<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-596\" title=\"TabBarMvc-example\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/09\/TabBarMvc-example.jpg\" alt=\"\" width=\"559\" height=\"376\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/09\/TabBarMvc-example.jpg 559w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/09\/TabBarMvc-example-300x201.jpg 300w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/p>\n<h2>Update:<\/h2>\n<p>TabBarMvc will automatically send a reverse animation to the controller action when you click\/tap a button that&#8217;s to the left of the currently activated one.<br \/>\nTo make use of this feature you should do something like this in the controller action:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nindex: function(options)\r\n{\r\n    ...\r\n\r\n    this.application.viewport.setActiveItem(this.indexView, options.animation);\r\n},\r\n<\/pre>\n<p>The default animation is a slide. To change this, you can set the <strong>switchAnimation<\/strong> property of TabBarMvc to a different effect like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n\txtype: 'TabBarMvc',\r\n\tswitchAnimation: 'slide',\r\n\t...\r\n}\r\n<\/pre>\n<p>or like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n\txtype: 'TabBarMvc',\r\n\tswitchAnimation: {type: 'slide'},\r\n\t...\r\n}\r\n<\/pre>\n<p>Hope you&#8217;ll find the\u00a0<strong>TabBarMvc<\/strong> component useful.<\/p>\n<p>You can download an example project from <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/10\/TabBarMvc.zip\">here<\/a>.<\/p>\n<p>This is the first article that was written as a result of the feedback received so if you have anything to say don&#8217;t hesitate to add your contribution.<\/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 Sencha Touch TabBar in a MVC structured application\" \/>  \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\/web-development\/javascript\/sencha-touch-tabbar-in-a-mvc-structured-application\/\" \/>  \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>While I&#8217;m working on the 4th part of the Creating a Sencha Touch MVC application from scratch\u00a0 series, I&#8217;ve decided to write this little article because the feedback received indicated that this would be useful for some of you. Many Sencha Touch developers tried or want to use the TabPanel component in a MVC structured &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/sencha-touch-tabbar-in-a-mvc-structured-application\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/sencha-touch-tabbar-in-a-mvc-structured-application\/\"\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":[18,88],"tags":[90,89,97,96],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/590"}],"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=590"}],"version-history":[{"count":21,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":639,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/590\/revisions\/639"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}