Skip to content

Instantly share code, notes, and snippets.

@ionas
Created December 26, 2010 01:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionas/755139 to your computer and use it in GitHub Desktop.
Save ionas/755139 to your computer and use it in GitHub Desktop.
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
// Hide the form button
$('#products_search input[type="submit"]').hide();
// Push searches onto the location hash
var inputTimeoutId = -1;
$('#products_search input').keyup(function() {
window.clearTimeout(inputTimeoutId); // Reset the input timer.
// Push the state into bbq - will be caught by hashchange event which triggers ajax
inputTimeoutId = window.setTimeout(function() {
$.bbq.pushState($('#products_search').serialize() + '&page=1'); // New search = start on page 1
}, 250);
});
// On triggering submit manually it should do the search instantly :)
$('form#products_search').submit(function(e) {
e.preventDefault(); // Disable manual form submission
window.clearTimeout(inputTimeoutId); // Reset the input timer.
$.bbq.pushState($('#products_search').serialize() + '&page=1'); // New search = start on page 1
});
// Push search result navigation requests onto the location hash
$('#products th a, #products .pagination a').live('click', function(e) {
// Instead of return false; // http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/
e.preventDefault();
// Push the state into bbq - will be caught by hashchange event which triggers ajax
$.bbq.pushState($.param.querystring(this.href));
});
// Bind hash state change event
$(window).bind('hashchange', function() {
// Ajax Request on hashchange
$.getScript($.param.querystring(document.location.href, $.param.fragment()));
});
// Catch any hash state change
$(window).trigger('hashchange');
});
// TODO: changing the hash by hand does not work (jumps to last history)
// TODO: using a bookmark does not fill in the search value into the search box
@ionas
Copy link
Author

ionas commented Dec 27, 2010

  • Fixed BUG: page:1 default working. Now when changing your search it jumps back to page 1
  • Moved the timeout/delay from the hashchange event to the form search input keyup event, as its only required there.
  • Re-enabled manual search submit (it pushes to the hashchange event now)
  • Removed some old outcommented code
  • Moved the TODO list to the bottom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment