{"id":615,"date":"2011-10-07T09:13:58","date_gmt":"2011-10-07T09:13:58","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=615"},"modified":"2011-10-11T07:36:58","modified_gmt":"2011-10-11T07:36:58","slug":"loading-sencha-touch-controllers-and-view-files-dynamically","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/javascript\/loading-sencha-touch-controllers-and-view-files-dynamically\/","title":{"rendered":"Loading Sencha Touch controllers and views dynamically"},"content":{"rendered":"<p>Usually, when creating a Sencha Touch MVC application all of its controllers and views are included manually in <em>index.html<\/em> and loaded when the app starts. But, when there are a lot of controllers and views to load, the time it takes the application to launch will be longer.<\/p>\n<p>A solution for this could be to load the controllers and views dynamically when there is a need for them. Since Sencha Touch can&#8217;t do this out of the box and doing it yourself can be tricky, I&#8217;ve created a script that will do it for you named <strong>DynamicMvc<\/strong>.<\/p>\n<p>Download <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/10\/DynamicMvc.js?update=1\">DynamicMvc.js<\/a> and include it in <em>index.html<\/em> file and you&#8217;re almost done. The script follows the conventions described below.<\/p>\n<h2>Here&#8217;s how it works.<\/h2>\n<p>Before a controller action is called, the script will check first if a view is registered based on the action name. So the convention is that <em>every action must have a view<\/em>. If not registered (not loaded) the view will be added. After that the controller will be loaded (if it&#8217;s not already).<\/p>\n<p>The registered name of the view must be constructed from the controller and its action name, like this:<\/p>\n<p>if <strong>Home<\/strong> is the <em>controller<\/em> and we have an <em>action<\/em> named <strong>contact<\/strong>, than we should have a folder under <strong>app\/views<\/strong> named after the controller in lower case and in it there must be a view file named <em>app\/views\/home\/<\/em><strong>HomeContactView.js<\/strong>; the registered name of the view must be <strong>HomeContact<\/strong>.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<p><em>controller name<\/em>: <strong>Search<\/strong><br \/>\n<em>controller file<\/em>: <strong>app\/controllers\/SearchController.js<\/strong><br \/>\n<em>action name<\/em>: <strong>index<\/strong><br \/>\n<em>action&#8217;s view name<\/em>: <strong>SearchIndex<\/strong><br \/>\n<em>view file<\/em>: <strong>app\/views\/search\/SearchIndexView.js<\/strong><\/p>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li>If you are using Ext.dispatch you must specify the historyUrl property; without it DynamicMvc can&#8217;t find the right controller to load.<\/li>\n<li>Dynamic loading the controllers and views will halt the application while the files are loaded.<\/li>\n<\/ul>\n<p>That&#8217;s all about my solution for loading controllers and views dynamically.<\/p>\n<p>You can download an example project from <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/10\/DynamicMVC.zip\">here<\/a>.<\/p>\n<p>If you have questions, suggestions or improvements don\u2019t hesitate to add a comment and let me know about them.<\/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 Loading Sencha Touch controllers and views dynamically\" \/>  \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\/loading-sencha-touch-controllers-and-view-files-dynamically\/\" \/>  \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>Usually, when creating a Sencha Touch MVC application all of its controllers and views are included manually in index.html and loaded when the app starts. But, when there are a lot of controllers and views to load, the time it takes the application to launch will be longer. A solution for this could be to &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/loading-sencha-touch-controllers-and-view-files-dynamically\/\">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\/loading-sencha-touch-controllers-and-view-files-dynamically\/\"\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":[98,93,99,90,89],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/615"}],"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=615"}],"version-history":[{"count":18,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/615\/revisions\/630"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}