hash-history-jquery-bbq.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Hash history with jQuery BBQ &middot; Isotope Docs</title>
  6. <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  7. <link rel="stylesheet" href="../css/style.css" />
  8. <!-- scripts at bottom of page -->
  9. </head>
  10. <body class="docs ">
  11. <nav id="site-nav">
  12. <h1><a href="../index.html">Isotope</a></h1>
  13. <h2>Docs</h2>
  14. <ul>
  15. <li><a href="../docs/introduction.html">Introduction</a>
  16. <li><a href="../docs/options.html">Options</a>
  17. <li><a href="../docs/methods.html">Methods</a>
  18. <li><a href="../docs/layout-modes.html">Layout modes</a>
  19. <li><a href="../docs/filtering.html">Filtering</a>
  20. <li><a href="../docs/sorting.html">Sorting</a>
  21. <li><a href="../docs/animating.html">Animating</a>
  22. <li><a href="../docs/adding-items.html">Adding items</a>
  23. <li><a href="../docs/extending-isotope.html">Extending Isotope</a>
  24. <li class="current"><a href="#content">Hash history with jQuery BBQ</a>
  25. <ul class="toc">
  26. <li><a href="#markup">Markup</a></li>
  27. <li><a href="#jquery_script">jQuery script</a></li>
  28. </ul>
  29. </li>
  30. <li><a href="../docs/help.html">Help</a>
  31. <li><a href="../docs/license.html">License</a>
  32. </ul>
  33. <h2>Demos</h2>
  34. <ul>
  35. <li><a href="../demos/basic.html">Basic</a>
  36. <li><a href="../demos/elements-complete.html">Elements Complete</a>
  37. <li><a href="../demos/elements-partial.html">Elements Partial</a>
  38. <li><a href="../demos/layout-modes.html">Layout modes</a>
  39. <li><a href="../demos/filtering.html">Filtering</a>
  40. <li><a href="../demos/sorting.html">Sorting</a>
  41. <li><a href="../demos/relayout.html">reLayout</a>
  42. <li><a href="../demos/adding-items.html">Adding items</a>
  43. <li><a href="../demos/infinite-scroll.html">Infinite Scroll</a>
  44. <li><a href="../demos/images.html">Images</a>
  45. <li><a href="../demos/combination-filters.html">Combination filters</a>
  46. <li><a href="../demos/hash-history.html">Hash history</a>
  47. <li><a href="../demos/fluid-responsive.html">Fluid / responsive</a>
  48. </ul>
  49. <h2>Custom layout modes</h2>
  50. <ul>
  51. <li><a href="../custom-layout-modes/centered-masonry.html">Centered Masonry</a>
  52. <li><a href="../custom-layout-modes/category-rows.html">Category rows</a>
  53. <li><a href="../custom-layout-modes/masonry-corner-stamp.html">Masonry corner stamp</a>
  54. <li><a href="../custom-layout-modes/masonry-gutters.html">Masonry gutters</a>
  55. <li><a href="../custom-layout-modes/spine-align.html">Spine align</a>
  56. </ul>
  57. <h2><a href="../tests/index.html">Tests</a></h2>
  58. </nav> <!-- #site-nav -->
  59. <section id="content">
  60. <h1>Hash history with jQuery BBQ</h1>
  61. <p>As cool as Isotope is, the only thing that could make it even cooler would be adding bookmark-able URL hashes. Ben Alman&#8217;s <a href='http://benalman.com/projects/jquery-bbq-plugin/'>jQuery BBQ</a> allows us to do just that.</p>
  62. <blockquote>
  63. <p>jQuery BBQ leverages the HTML5 hashchange event to allow simple, yet powerful bookmarkable #hash history.</p>
  64. </blockquote>
  65. <p><a href='../demos/hash-history.html'><strong>See Demo: Hash history</strong></a></p>
  66. <p>BBQ is a marvelous plugin that provides for a lot more functionality. The <a href='../demos/hash-history.html'>hash history demo</a> uses multiple options (<code>sortBy</code>, <code>sortAscending</code>, and <code>layoutMode</code> in addition to <code>filter</code>), the ability to use back-button history, and properly highlights selected links.</p>
  67. <p>Given BBQ&#8217;s tremendous capabilities, the code can grow to be a bit complex. Be sure to read through <a href='http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html'>BBQ&#8217;s docs</a> and take look at <a href='http://benalman.com/code/projects/jquery-bbq/examples/'>its examples</a> before you dive in and code up your own solution.</p>
  68. <h2 id='markup'>Markup</h2>
  69. <p>Instead of setting the option values and keys with <code>data</code> attributes, we can add the option in the <code>href</code> for each link.</p>
  70. <div class='highlight'><pre><code class='html'><span class='nt'>&lt;ul</span> <span class='na'>class=</span><span class='s'>&quot;option-set&quot;</span><span class='nt'>&gt;</span>
  71. <span class='nt'>&lt;li&gt;&lt;a</span> <span class='na'>href=</span><span class='s'>&quot;#filter=*&quot;</span> <span class='na'>class=</span><span class='s'>&quot;selected&quot;</span><span class='nt'>&gt;</span>show all<span class='nt'>&lt;/a&gt;&lt;/li&gt;</span>
  72. <span class='nt'>&lt;li&gt;&lt;a</span> <span class='na'>href=</span><span class='s'>&quot;#filter=.metal&quot;</span><span class='nt'>&gt;</span>metal<span class='nt'>&lt;/a&gt;&lt;/li&gt;</span>
  73. <span class='nt'>&lt;li&gt;&lt;a</span> <span class='na'>href=</span><span class='s'>&quot;#filter=.transition&quot;</span><span class='nt'>&gt;</span>transition<span class='nt'>&lt;/a&gt;&lt;/li&gt;</span>
  74. <span class='nt'>&lt;li&gt;&lt;a</span> <span class='na'>href=</span><span class='s'>&quot;#filter=.alkali%2C+.alkaline-earth&quot;</span><span class='nt'>&gt;</span>alkali and alkaline-earth<span class='nt'>&lt;/a&gt;&lt;/li&gt;</span>
  75. <span class='nt'>&lt;li&gt;&lt;a</span> <span class='na'>href=</span><span class='s'>&quot;#filter=%3Anot(.transition)&quot;</span><span class='nt'>&gt;</span>not transition<span class='nt'>&lt;/a&gt;&lt;/li&gt;</span>
  76. <span class='nt'>&lt;li&gt;&lt;a</span> <span class='na'>href=</span><span class='s'>&quot;#filter=.metal%3Anot(.transition)&quot;</span><span class='nt'>&gt;</span>metal but not transition<span class='nt'>&lt;/a&gt;&lt;/li&gt;</span>
  77. <span class='nt'>&lt;/ul&gt;</span>
  78. </code></pre>
  79. </div>
  80. <p>The <code>href</code> value is a serialized string, suitable for a URL. These values can be created with <a href='http://api.jquery.com/jQuery.param/'>jQuery.param()</a>.</p>
  81. <div class='highlight'><pre><code class='javascript'><span class='nx'>$</span><span class='p'>.</span><span class='nx'>param</span><span class='p'>({</span> <span class='nx'>filter</span><span class='o'>:</span> <span class='s1'>&#39;.metal&#39;</span> <span class='p'>})</span>
  82. <span class='c1'>// &gt;&gt; &quot;filter=.metal&quot;</span>
  83. <span class='nx'>$</span><span class='p'>.</span><span class='nx'>param</span><span class='p'>({</span> <span class='nx'>filter</span><span class='o'>:</span> <span class='s1'>&#39;.alkali, alkaline-earth&#39;</span> <span class='p'>})</span>
  84. <span class='c1'>// &gt;&gt; &quot;filter=.alkali%2C+alkaline-earth&quot;</span>
  85. <span class='nx'>$</span><span class='p'>.</span><span class='nx'>param</span><span class='p'>({</span> <span class='nx'>filter</span><span class='o'>:</span> <span class='s1'>&#39;:not(.transition)&#39;</span> <span class='p'>})</span>
  86. <span class='c1'>// &gt;&gt; &quot;#filter=%3Anot(.transition)&quot;</span>
  87. </code></pre>
  88. </div>
  89. <h2 id='jquery_script'>jQuery script</h2>
  90. <p>These serialized <code>href</code> values can be converted into their proper jQuery object form when clicked using <a href='http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html#jQuery.deparam'>jQuery.deparam()</a> from jQuery BBQ.</p>
  91. <div class='highlight'><pre><code class='javascript'><span class='nx'>$</span><span class='p'>(</span><span class='s1'>&#39;.option-set a&#39;</span><span class='p'>).</span><span class='nx'>click</span><span class='p'>(</span><span class='kd'>function</span><span class='p'>(){</span>
  92. <span class='c1'>// get href attr, remove leading #</span>
  93. <span class='kd'>var</span> <span class='nx'>href</span> <span class='o'>=</span> <span class='nx'>$</span><span class='p'>(</span><span class='k'>this</span><span class='p'>).</span><span class='nx'>attr</span><span class='p'>(</span><span class='s1'>&#39;href&#39;</span><span class='p'>).</span><span class='nx'>replace</span><span class='p'>(</span> <span class='sr'>/^#/</span><span class='p'>,</span> <span class='s1'>&#39;&#39;</span> <span class='p'>),</span>
  94. <span class='c1'>// convert href into object</span>
  95. <span class='c1'>// i.e. &#39;filter=.inner-transition&#39; -&gt; { filter: &#39;.inner-transition&#39; }</span>
  96. <span class='nx'>option</span> <span class='o'>=</span> <span class='nx'>$</span><span class='p'>.</span><span class='nx'>deparam</span><span class='p'>(</span> <span class='nx'>href</span><span class='p'>,</span> <span class='kc'>true</span> <span class='p'>);</span>
  97. <span class='c1'>// set hash, triggers hashchange on window</span>
  98. <span class='nx'>$</span><span class='p'>.</span><span class='nx'>bbq</span><span class='p'>.</span><span class='nx'>pushState</span><span class='p'>(</span> <span class='nx'>option</span> <span class='p'>);</span>
  99. <span class='k'>return</span> <span class='kc'>false</span><span class='p'>;</span>
  100. <span class='p'>});</span>
  101. </code></pre>
  102. </div>
  103. <p>Calling <a href='http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html#jQuery.bbq.pushState'>$.bbq.pushState()</a> will trigger the <code>hashchange</code> event. At that point, we can parse the hash from the URL and use it to trigger the proper change in the Isotope instance.</p>
  104. <div class='highlight'><pre><code class='javascript'><span class='nx'>$</span><span class='p'>(</span><span class='nb'>window</span><span class='p'>).</span><span class='nx'>bind</span><span class='p'>(</span> <span class='s1'>&#39;hashchange&#39;</span><span class='p'>,</span> <span class='kd'>function</span><span class='p'>(</span> <span class='nx'>event</span> <span class='p'>){</span>
  105. <span class='c1'>// get options object from hash</span>
  106. <span class='kd'>var</span> <span class='nx'>hashOptions</span> <span class='o'>=</span> <span class='nx'>$</span><span class='p'>.</span><span class='nx'>deparam</span><span class='p'>.</span><span class='nx'>fragment</span><span class='p'>();</span>
  107. <span class='c1'>// apply options from hash</span>
  108. <span class='nx'>$container</span><span class='p'>.</span><span class='nx'>isotope</span><span class='p'>(</span> <span class='nx'>hashOptions</span> <span class='p'>);</span>
  109. <span class='p'>})</span>
  110. <span class='c1'>// trigger hashchange to capture any hash data on init</span>
  111. <span class='p'>.</span><span class='nx'>trigger</span><span class='p'>(</span><span class='s1'>&#39;hashchange&#39;</span><span class='p'>);</span>
  112. </code></pre>
  113. </div>
  114. <p>Now any filter buttons that are clicked will update the URL hash, so these options can be bookmarked.</p>
  115. <footer>
  116. Isotope by <a href="http://desandro.com">David DeSandro</a> / <a href="http://metafizzy.co">Metafizzy</a>
  117. </footer>
  118. </section> <!-- #content -->
  119. </body>
  120. </html>