PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/engine/vendors/github/defunkt-jquery-pjax-7d9841e/README.md

https://github.com/wamplo/red
Markdown | 233 lines | 167 code | 66 blank | 0 comment | 0 complexity | da8929becba9443ff83dd3c1bc599ac1 MD5 | raw file
  1. ## pushState + ajax = pjax
  2. .--.
  3. / \
  4. ## a a
  5. ( '._)
  6. |'-- |
  7. _.\___/_ ___pjax___
  8. ."\> \Y/|<'. '._.-'
  9. / \ \_\/ / '-' /
  10. | --'\_/|/ | _/
  11. |___.-' | |`'`
  12. | | |
  13. | / './
  14. /__./` | |
  15. \ | |
  16. \ | |
  17. ; | |
  18. / | |
  19. jgs |___\_.\_
  20. `-"--'---'
  21. ## what is it?
  22. pjax loads HTML from your server into the current page
  23. without a full reload. It's ajax with real permalinks,
  24. page titles, and a working back button that fully degrades.
  25. pjax enhances the browsing experience - nothing more.
  26. You can find a demo on <http://pjax.heroku.com/>
  27. ## three ways to pjax on the client side:
  28. One. Functionally obtrusive, loading the href with ajax into data-pjax:
  29. ```html
  30. <a href='/explore' data-pjax='#main'>Explore</a>
  31. ```
  32. ```js
  33. $('a[data-pjax]').pjax()
  34. ```
  35. Two. Slightly obtrusive, passing a container and jQuery ajax options:
  36. ```html
  37. <a href='/explore' class='js-pjax'>Explore</a>
  38. ```
  39. ```js
  40. $('.js-pjax').pjax('#main', { timeout: null, error: function(xhr, err){
  41. $('.error').text('Something went wrong: ' + err)
  42. }})
  43. ```
  44. Three. Unobtrusive, showing a 'loading' spinner:
  45. ```html
  46. <div id='main'>
  47. <div class='loader' style='display:none'><img src='spin.gif'></div>
  48. <div class='tabs'>
  49. <a href='/explore'>Explore</a>
  50. <a href='/help'>Help</a>
  51. </div>
  52. </div>
  53. ```
  54. ```js
  55. $('a').pjax('#main').live('click', function(){
  56. $(this).showLoader()
  57. })
  58. ```
  59. ## $(link).pjax( container, options )
  60. The `$(link).pjax()` function accepts a container, an options object,
  61. or both. The container MUST be a string selector - this is because we
  62. cannot persist jQuery objects using the History API between page loads.
  63. The options are the same as jQuery's `$.ajax` options with the
  64. following additions:
  65. * `container` - The String selector of the container to load the
  66. reponse body. Must be a String.
  67. * `clickedElement` - The element that was clicked to start the pjax call.
  68. * `push` - Whether to pushState the URL. Default: true (of course)
  69. * `replace` - Whether to replaceState the URL. Default: false
  70. * `error` - By default this callback reloads the target page once
  71. `timeout` ms elapses.
  72. * `timeout` - pjax sets this low, <1s. Set this higher if using a
  73. custom error handler. It's ms, so something like
  74. `timeout: 2000`
  75. * `fragment` - A String selector that specifies a sub-element to
  76. be pulled out of the response HTML and inserted
  77. into the `container`. Useful if the server always returns
  78. full HTML pages.
  79. ## $.pjax( options )
  80. You can also just call `$.pjax` directly. It acts much like `$.ajax`, even
  81. returning the same thing and accepting the same options.
  82. The pjax-specific keys listed in the `$(link).pjax()` section work here
  83. as well.
  84. This pjax call:
  85. ```js
  86. $.pjax({
  87. url: '/authors',
  88. container: '#main'
  89. })
  90. ```
  91. Roughly translates into this ajax call:
  92. ```js
  93. $.ajax({
  94. url: '/authors',
  95. dataType: 'html',
  96. beforeSend: function(xhr){
  97. xhr.setRequestHeader('X-PJAX', 'true')
  98. },
  99. success: function(data){
  100. $('#main').html(data)
  101. history.pushState(null, $(data).filter('title').text(), '/authors')
  102. })
  103. })
  104. ```
  105. ## pjax on the server side
  106. You'll want to give pjax requests a 'chrome-less' version of your page.
  107. That is, the page without any layout.
  108. As you can see in the "ajax call" example above, pjax sets a custom 'X-PJAX'
  109. header to 'true' when it makes an ajax request to make detecting it easy.
  110. In Rails, check for `request.headers['X-PJAX']`:
  111. ```ruby
  112. def my_page
  113. if request.headers['X-PJAX']
  114. render :layout => false
  115. end
  116. end
  117. ```
  118. Django: <https://github.com/jacobian/django-pjax>
  119. Asp.Net MVC3: <http://biasecurities.com/blog/2011/using-pjax-with-asp-net-mvc3/>
  120. ## page titles
  121. Your HTML should also include a `<title>` tag if you want page titles to work.
  122. When using a page fragment, pjax will check the fragment DOM element
  123. for a `title` or `data-title` attribute and use any value it finds.
  124. ## events
  125. pjax will fire two events on the container you've asked it to load your
  126. reponse body into:
  127. * `pjax:start` - Fired when a pjax ajax request begins.
  128. * `pjax:end` - Fired when a pjax ajax request ends.
  129. This allows you to, say, display a loading indicator upon pjaxing:
  130. ```js
  131. $('a.pjax').pjax('#main')
  132. $('#main')
  133. .bind('pjax:start', function() { $('#loading').show() })
  134. .bind('pjax:end', function() { $('#loading').hide() })
  135. ```
  136. Because these events bubble, you can also set them on the body:
  137. ```js
  138. $('a.pjax').pjax()
  139. $('body')
  140. .bind('pjax:start', function() { $('#loading').show() })
  141. .bind('pjax:end', function() { $('#loading').hide() })
  142. ```
  143. ## browser support
  144. pjax only works with browsers that support the history.pushState API.
  145. For a table of supported browsers see: <http://caniuse.com/#search=pushstate>
  146. To check if pjax is supported, use the `$.support.pjax` boolean.
  147. When pjax is not supported, `$('a').pjax()` calls will do nothing (aka links
  148. work normally) and `$.pjax({url:url})` calls will redirect to the given URL.
  149. ## install it
  150. ```
  151. $ cd path/to/js
  152. $ wget https://github.com/defunkt/jquery-pjax/raw/master/jquery.pjax.js
  153. ```
  154. Then, in your HTML:
  155. ```html
  156. <script src="path/to/js/jquery.pjax.js"></script>
  157. ```
  158. Replace `path/to/js` with the path to your JavaScript directory,
  159. e.g. `public/javascripts`.
  160. ## minimize it
  161. ```
  162. curl \
  163. -d output_info=compiled_code \
  164. -d compilation_level=SIMPLE_OPTIMIZATIONS \
  165. -d code_url=https://github.com/defunkt/jquery-pjax/raw/master/jquery.pjax.js \
  166. http://closure-compiler.appspot.com/compile
  167. ```