Last Updated: February 25, 2016
· manudwarf

PushState navigation and title issues

Hi guys,

For (written in RoR) I had a couple of issues to deal with Backbone and pushState.

Parenthesis, if you don't know about ghost views, check this post :

So, here is the issue : if you hit and then click on the "Récents" tab, the content will be the same as if you did directly type the URL in your browser, but the title didn't change.

I found out a little trick to fix that. Basically, I have a different template for "regular" and "pushState" requests.

When I load a new page, it's not just the HTML content but a JSON object passing the content, the title and the description.


layout :get_layout


def get_layout
  request.xhr? ? params[:includeContext] == "true" ? "ajax" : nil : "application"

I have to explicitly pass "includeContext=true" when I load a new page to avoid interfering with other AJAX requests.


def title(page_title)
  content_for :title, page_title.to_s

def description(description)
  content_for :description, description.to_s


  "content": "<%= yield.gsub("\n", " ").gsub("\"", "\\\"").html_safe %>",
  "description": "<%= yield :description %>",
  "title": "<%= content_for?(:title) ? yield(:title) + " - " : "" %>Horaires Bus-Tram Angers"

Don't forget to escape double quotes and remove line returns. Otherwise, your JSON will be incorrect.

On the JS side, here is my loading function :

$.get(url, {includeContext: true}, function(data) {
  var jsonData = JSON.parse(data);

  document.title = jsonData.title;
  $('meta[name=description]').attr("content", jsonData.description);

And that's it ! Here is what a view looks like :

<% title %>
<% description "Prochains passages bus tram pour l'arrêt #{} (#{@stop.city_name})." %>

<article id="showStop" class="stop">

2 Responses
Add your response

Instead of having 2 separate templates can you just embed those metadata into the rendered HTML itself?

over 1 year ago ·

I could, but then I would have to go through the DOM, take the content element, the title and the description (which is what does turbolinks if I'm correct).
It just doesn't feel right to me.

over 1 year ago ·