Permalink
Browse files

Use PJAX instead of Turbolinks to load results

  • Loading branch information...
1 parent 4f134b4 commit e6975615705af9690c36fc470bcac826e6ef8d50 @rwdaigle committed Oct 19, 2012
View
@@ -28,7 +28,7 @@ gem 'kanshi', :require => false
gem 'jquery-rails'
gem 'haml-rails'
-gem 'turbolinks'
+gem 'pjax_rails'
group :assets do
gem 'sass-rails', '~> 3.2.3'
View
@@ -114,6 +114,8 @@ GEM
oauth2 (~> 0.8.0)
omniauth (~> 1.0)
pg (0.14.1)
+ pjax_rails (0.3.3)
+ jquery-rails
polyglot (0.3.3)
queue_classic (2.0.1)
pg (~> 0.14.0)
@@ -173,7 +175,6 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
- turbolinks (0.5.1)
tzinfo (0.3.33)
uglifier (1.3.0)
execjs (>= 0.3.0)
@@ -199,6 +200,7 @@ DEPENDENCIES
omniauth
omniauth-github
pg
+ pjax_rails
queue_classic
rack-worker
rails (= 3.2.8)
@@ -207,7 +209,6 @@ DEPENDENCIES
scrolls
thin
tire
- turbolinks
uglifier (>= 1.0.3)
will_paginate
yajl-ruby
@@ -12,5 +12,5 @@
//
//= require jquery
//= require jquery_ujs
-//= require turbolinks
+//= require jquery.pjax
//= require_tree .
@@ -5,19 +5,20 @@ $(function() {
});
// Subsequent loads
-$(window).bind('page:load', function() {
+$(window).bind('pjax:end', function() {
setInitialElementStates();
});
var setInitialElementStates = function() {
+
searchField().select();
- // Pass form submits through Turbolinks
- $("#top_search_form").submit(function() {
- form = $(this);
- Turbolinks.visit(form.attr('action') + '?' + form.serialize());
- return false;
- });
+ // Make sure results html is displayed
+ if($("#results .choice").size() > 0) {
+ $("#results").show();
+ } else {
+ $("#results").hide();
+ }
};
var searchField = function() {
@@ -28,13 +29,23 @@ var wireSearch = function() {
var selectedClass = 'selected';
+ // Pass form submits through PJAX
+ $("#top_search_form").submit(function() {
+ form = $(this);
+ $.pjax({
+ url: form.attr('action') + '?' + form.serialize(),
+ container: '#results'
+ })
+ return false;
+ });
+
Mousetrap.bind(['down'], function(e) {
- navigateList('down', $(".display .choice:first"));
+ navigateList('down', $("#results .choice:first"));
haltEvent(e);
});
Mousetrap.bind(['up'], function(e) {
- navigateList('up', $(".display .choice:last"));
+ navigateList('up', $("#results .choice:last"));
haltEvent(e);
});
@@ -48,7 +59,7 @@ var wireSearch = function() {
Mousetrap.bind(['/'], function(e) {
executeOutsideInputFields(e, function() {
searchField().focus().select();
- $(".display .choice.selected").removeClass(selectedClass);
+ $("#results .choice.selected").removeClass(selectedClass);
haltEvent(e);
});
});
@@ -61,7 +72,7 @@ var wireSearch = function() {
// direction == up|down
var navigateList = function(direction, defaultEl) {
- previouslySelectedEl = $(".display .choice.selected");
+ previouslySelectedEl = $("#results .choice.selected");
if(previouslySelectedEl.size() <= 0) {
defaultEl.addClass(selectedClass);
@@ -77,7 +88,7 @@ var wireSearch = function() {
}
var goToSelectedResult = function() {
- selectedEl = $(".display .choice.selected");
+ selectedEl = $("#results .choice.selected");
if(selectedEl.size() >= 1) {
location.href = selectedEl.find("a.gist-url").attr('href');
}
@@ -1,5 +1,3 @@
-require "queue_classic"
-
class GistsController < ApplicationController
before_filter :force_user_login
@@ -15,7 +13,8 @@ def search
@results = Gist.search(current_user, normalized_query)
if(feeling_lucky_directive? && lucky_result = @results.first)
redirect_to lucky_result.url
- log({ns: self.class, fn: __method__, measure: true, at: 'auto-jump', query: params[:q]}, {:'redirect-to' => lucky_result.url}, current_user)
+ elsif(pjax?)
+ render :partial => "results"
end
end
end
@@ -28,6 +27,7 @@ def refresh
private
# Strip navigational search directives ("!") without modifying original query param
+ # This is at the controller level b/c it's a navigational command, not a search command
def normalized_query
@normalized_query ||= feeling_lucky_directive? ? params[:q][0..-2] : params[:q]
end
@@ -37,7 +37,11 @@ def feeling_lucky_directive?
end
def search_etag
- "#{CACHE_VERSION}-#{current_user.id}-#{params[:q]}"
+ "#{CACHE_VERSION}-#{current_user.id}-pjax:#{pjax?}-#{params[:q]}"
+ end
+
+ def pjax?
+ !request.headers['X-PJAX'].blank?
end
end
@@ -0,0 +1,11 @@
+- if @results.size > 0
+ - for result in @results
+ .choice
+ %span.command= search_result_link(result)
+ %span.description= pluralize(result.comment_count, "comments")
+ .hints
+ %em keyboard:
+ ↑ = up, ↓ = down, enter = follow link, / = search
+- else
+ .no-results
+ %span.command= "No results found. Bummer."
@@ -5,21 +5,8 @@
= form_tag(search_gists_path, :method => "get", :id => "top_search_form", :class => "command_bar_form") do
.commandbar.focused
= text_field_tag(:q, params[:q], :size => 50, :class => "mousetrap", :id => "command-bar", :placeholder => "Search your gists", :tabindex => "1", :autocomplete => "off")
- .display(style="display: #{!params[:q].blank? ? "block" : "none"};")
- - if @results.size > 0
- - for result in @results
- .choice
- %span.command= search_result_link(result)
- %span.description= pluralize(result.comment_count, "comments")
- .hints
- %em keyboard:
- ↑ = up, ↓ = down, enter = follow link, / = search
- - else
- .choice
- %span.command= "No results found. Bummer."
-
- / = submit_tag("Search")
- / = "(last indexed: #{current_user.last_gh_fetch ? current_user.last_gh_fetch.to_formatted_s(:short) : "not yet"})"
+ #results.display(style="display: #{!params[:q].blank? ? "block" : "none"};")
+ = render :partial => 'results'
.avatar
%a(href="#{current_user.gh_url}")
@@ -1 +1 @@
-CACHE_VERSION="v001"
+CACHE_VERSION="v002"

0 comments on commit e697561

Please sign in to comment.