Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix double-fire PJAX issue. Handle error responses

  • Loading branch information...
commit 46a903c0bff1798d3b20e0ed4e353cdd1023edfe 1 parent 045b01a
@rwdaigle authored
View
48 app/assets/javascripts/search.js
@@ -4,11 +4,6 @@ $(function() {
displayResults();
});
-// Subsequent loads
-$(window).bind('pjax:end', function() {
- displayResults();
-});
-
var displayResults = function() {
// Make sure results html is displayed
@@ -39,28 +34,59 @@ var wireSearch = function() {
});
// Pass form submits through PJAX
- $("#top_search_form").submit(function() {
- submitSearch();
- return false;
+ $("#top_search_form").submit(function(e) {
+ return submitSearch();
});
var submitSearch = function() {
- console.log("Submitting search form");
form = $("#top_search_form");
$.pjax({
url: form.attr('action') + '?' + form.serialize(),
- container: '#results'
+ container: '#results',
+ timeout: 2500
});
+ return false;
};
$(window).bind('pjax:start', function() {
$(".indicator").css("visibility", "visible");
});
- $(window).bind('pjax:end', function() {
+ $(window).bind('pjax:complete', function() {
$(".indicator").css("visibility", "hidden");
});
+ $(window).bind('pjax:success', function() {
+ $("#search-error").hide();
+ displayResults();
+ });
+
+ $(window).bind('pjax:timeout', function() {
+ searchTimeout();
+ return false;
+ });
+
+ $(window).bind('pjax:error', function() {
+ searchError();
+ return false;
+ });
+
+ // Search errors
+
+ var searchTimeout = function() {
+ $("#results").hide();
+ $("#search-error").show();
+ $("#search-error .timeout").show();
+ $("#search-error .error").hide();
+ }
+
+ var searchError = function() {
+ $("#results").hide();
+ $("#search-error").show();
+ $("#search-error .error").show();
+ $("#search-error .timeout").hide();
+ }
+
// Keyboard nav
Mousetrap.bind(['down'], function(e) {
View
13 app/assets/stylesheets/search.sass
@@ -43,6 +43,19 @@
font-size: 11px
padding-right: 5px
+ #search-error.display
+
+ display: none
+
+ .error
+ color: #D8000C
+ background: #FFBABA
+ border: 1px solid
+ .timeout
+ color: #9F6000
+ background: #FEEFB3
+ border: 1px solid
+
.command_bar_form
padding-right: 0px
View
2  app/models/gist.rb
@@ -86,7 +86,7 @@ def reindex(gists = scoped)
end
def search_cache_key(user, q)
- "#{Rails.env.production? ? CACHE_VERSION : SecureRandom.hex}-user_id:#{user.id}-updated_at:#{user.last_gh_fetch ? user.last_gh_fetch.to_i : "never"}-#{q}"
+ "#{CACHE_ACTIVE ? CACHE_VERSION : "no-cache"}-user_id:#{user.id}-updated_at:#{user.last_gh_fetch ? user.last_gh_fetch.to_i : "never"}-#{q}"
end
def with_cache(key)
View
3  app/views/gists/search.html.haml
@@ -7,6 +7,9 @@
= text_field_tag(:q, params[:q], :size => 50, :class => "mousetrap", :id => "command-bar", :placeholder => "Search your gists: this AND that \"this phrase\" -excluding +including", :tabindex => "1", :autocomplete => "off")
#results.display(style="display: #{!params[:q].blank? ? "block" : "none"};")
= render :partial => 'results'
+ #search-error.display
+ .error There was an error with your search. For this, <a href="https://twitter.com/rwdaigle">@rwdaigle</a> will mercilessly flog himself.
+ .timeout It seems your search is taking longer than <a href="https://twitter.com/rwdaigle">@rwdaigle</a> finds acceptable. Hold on, it's here somewhere...
.indicator(style="visibility: hidden;")
%span.loading
Please sign in to comment.
Something went wrong with that request. Please try again.