使用turbolinks实现局部页面刷新

turbolinks安装和使用:
  1. Add gem 'turbolinks' to your Gemfile.
  2. Run bundle install.
  3. Add //= require turbolinks to your Javascript manifest file (usually found at app/assets/javascripts/application.js). If your manifest requires both turbolinks and jQuery, make sure turbolinks is listed after jQuery.
  4. Restart your server and you're now using turbolinks!
  5. rails4.0默认引入turbolinks的,不需要你的配置。
  6. 使用scaffold生成person的crud 
    class CreatePeople < ActiveRecord::Migration
      def change
        create_table :people do |t|
          t.string :name
          t.string :sex
          t.integer :age
          t.string :phone
          t.text :address
    
          t.timestamps
        end
      end
    end

  7. 修改people_controller
    def person_params
          params.require(:person).permit(:name, :age, :sex, :phone, :address)
        end
  8. 修改index.html.erb
    <h1>Listing people</h1>
    
    <table>
      <thead>
        <tr>
          <th>姓名</th>
          <th>性别</th>
          <th>年龄</th>
          <th>电话</th>
          <th>地址</th>
        </tr>
      </thead>
    
      <tbody>
        <% @people.each do |person| %>
          <tr>
            <td><%= person.name %></td>
            <td><%= person.sex %></td>
            <td><%= person.age %></td>
            <td><%= person.phone %></td>
            <td><%= person.address %></td>
            <td><%= link_to 'Show', person %></td>
            <td><%= link_to 'Edit', edit_person_path(person) %></td>
            <td><%= link_to 'Destroy', person, method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
    
    <br>
    
    <%= link_to 'New Person', new_person_path %>
    <br><br>
    <%= link_to 'testTurbolinds', '/people/1' %> <br>
    <%= link_to 'testTurbolinds', '/people/2' %> <br>
    <%= link_to 'NoUseTurbolinds', '/people/3','data-no-turbolink'=>true %> <br>
  9. 修改show.html.erb
    <p id="notice"><%= notice %></p>
    
    
    <p>
      <strong>Title:</strong>
      <%= @person.name %>
    </p>
    
    <p>
      <strong>Title:</strong>
      <%= @person.name %>
    </p>
    
    <p>
      <strong>Title:</strong>
      <%= @person.sex %>
    </p>
    
    <p>
      <strong>Title:</strong>
      <%= @person.age %>
    </p>
    
    <p>
      <strong>Title:</strong>
      <%= @person.phone %>
    </p>
    
    <p>
      <strong>Title:</strong>
      <%= @person.address %>
    </p>
    
    
    <%= link_to 'Edit', edit_person_path(@person) %> |
    <%= link_to 'Back', people_path %>
  10. 修改_form.html.erb
    <%= form_for(@person) do |f| %>
      <% if @person.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:</h2>
    
          <ul>
          <% @person.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>
    
        <div class="field">
          <%= f.label :name %><br>
          <%= f.text_field :name %>
        </div>
        <div class="field">
          <%= f.label :sex %><br>
          <%= f.text_field :sex %>
        </div>
        <div class="field">
          <%= f.label :age %><br>
          <%= f.text_field :age %>
        </div>
        <div class="field">
          <%= f.label :phone %><br>
          <%= f.text_field :phone %>
        </div>
        <div class="field">
          <%= f.label :address %><br>
          <%= f.text_area :address %>
        </div>
    
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
  11. 如果application.js中报错cannot resolve symbol 'turbolinks' 不要理会
  12. 修改routes
    resources :people
    
      # The priority is based upon order of creation: first created -> highest priority.
      # See how all your routes lay out with "rake routes".
    
      # You can have the root of your site routed with "root"
      root 'people#index'
  13. localhost:3000访问              
  14. 前两个使用turbolinks链接访问只加载title和body,最后一个没有使用turbolinks的吧全部的css,js都加载了  
  15. 网络监控
  16. 第一个链接的请求
  17. 第二个链接的请求
  18. 第三个链接的请求
  19. 我们可以从github上turbolinks文档上看到

      Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body and the title in the head. Think CGI vs persistent process.

This is similar to pjax, but instead of worrying about what element on the page to replace, and tailoring the server-side response to fit, we replace the entire body. This means that you get the bulk of the speed benefits from pjax (no recompiling of the JavaScript or CSS) without having to tailor the server-side response. It just works.

Do note that this of course means that you'll have a long-running, persistent session with maintained state. That's what's making it so fast. But it also means that you may have to pay additional care not to leak memory or otherwise bloat that long-running state. That should rarely be a problem unless you're doing something really funky, but you do have to be aware of it. Your memory leaking sins will not be swept away automatically by the cleansing page change any more.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值