{"id":109,"date":"2012-08-26T08:40:42","date_gmt":"2012-08-25T21:40:42","guid":{"rendered":"https:\/\/mindbleach.com\/words\/?p=109"},"modified":"2012-10-25T10:27:05","modified_gmt":"2012-10-24T23:27:05","slug":"multiple-pages-with-backbone-js-and-bootstrap","status":"publish","type":"post","link":"https:\/\/mindbleach.com\/words\/2012\/08\/26\/multiple-pages-with-backbone-js-and-bootstrap\/","title":{"rendered":"Multiple Pages with Backbone.js and Bootstrap"},"content":{"rendered":"<p>Backbone.js has routes. Hooray, now &#8216;#here&#8217; can go here, &#8216;#there&#8217; can go there, and so on! That&#8217;s the logic side sorted out.<br \/>\nWhat about presentation?<\/p>\n<p>In many cases, 1 route &lt;=&gt; 1 &#8216;page&#8217; of content.<\/p>\n<p>How do we do that?<\/p>\n<p>The <a href=\"http:\/\/twitter.github.com\/bootstrap\/\" target=\"_blank\">Bootstrap<\/a> library has a tabs component that implements a couple of very useful things:<\/p>\n<ul>\n<li>Provides a link between a menu and a set of &#8216;pages&#8217;<\/li>\n<li>Easy to trigger page changes<\/li>\n<\/ul>\n<p>We still need to add two small things to connect <strong>any<\/strong> router to a set of pages:<\/p>\n<ul>\n<li>When a route is triggered, activate a matching tab.<\/li>\n<li>When a tab is activated, trigger any matching route.<\/li>\n<\/ul>\n<p>All it needs is the following code:<\/p>\n<pre class=\"brush: javascript; gutter: true\">\/\/On click, show the page, allow to route.\r\n$(&#039;#tab_demo_menu a&#039;).on(&#039;click&#039;, function (e) { \r\n\t$(this).tab(&#039;show&#039;); \r\n}\r\n\/\/On route, trigger the &#039;click&#039;\r\nBackbone.history.on(&#039;route&#039;, function(router, event) { \r\n\t$(&#039;#tab_demo_menu a[href=&quot;#&#039;+this.fragment+&#039;&quot;]&#039;).click();\r\n});<\/pre>\n<p>So what does this mean?<\/p>\n<ul>\n<li>No modification needs to be made to the router.<\/li>\n<li>As long as a tab with that route name exists, it will automatically be shown when the route is invoked.<\/li>\n<li>A route with no corresponding tab can be invoked with no ill effects; maybe you want to show a dialog above whatever page is active?<\/li>\n<li>You can link to a particular tab with the #fragment; something that Bootstrap tabs don&#8217;t support by default.<\/li>\n<\/ul>\n<p>Look! A finely crafted demo: <a href=\"https:\/\/mindbleach.com\/demos\/backbone_bootstrap_pages\/\" target=\"_blank\">https:\/\/mindbleach.com\/demos\/backbone_bootstrap_pages\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Backbone.js has routes. Hooray, now &#8216;#here&#8217; can go here, &#8216;#there&#8217; can go there, and so on! That&#8217;s the logic side sorted out. What about presentation? In many cases, 1 route &lt;=&gt; 1 &#8216;page&#8217; of content. How do we do that? &hellip; <a href=\"https:\/\/mindbleach.com\/words\/2012\/08\/26\/multiple-pages-with-backbone-js-and-bootstrap\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":9,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts\/109\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}