Code Monkey home page Code Monkey logo

render_async's Introduction

Build Status All Contributors Gem Version Code Climate Test Coverage

render_async

render_async

Speed up rendering Rails pages with this gem.

render_async renders partials to your views asynchronously. This is done through adding Javascript code that does AJAX request to your controller which then renders your partial into a Rails view.

Workflow:

  1. user visits a Rails page
  2. AJAX request on the controller action
  3. controller renders a partial
  4. partials renders in the place where you put render_async helper

Javascript is injected into <%= content_for :render_async %> so you choose where to put it.

Installation

Add this line to your application's Gemfile:

gem 'render_async'

And then execute:

$ bundle install

Usage

  1. Include render_async view helper somewhere in your views:

    # app/views/comments/show.html.erb
    
    <%= render_async comment_stats_path %>
  2. Then create a route that will config/routes.rb

    # config/routes.rb
    
    get :comment_stats, :controller => :comments
  3. Fill in the logic in your controller

    # app/controllers/comments_controller.rb
    
    def comment_stats
      @stats = Comment.get_stats
    
      render :partial => "comment_stats"
    end
  4. Create a partial that will render

    # app/views/comments/_comment_stats.html.erb
    
    <div class="col-md-6">
      <%= @stats %>
    </div>
  5. Add content_for in your base view file

    # application.html.erb
    
    <%= content_for :render_async %>

Advanced usage

Passing in HTML options

render_async takes two arguments, path and html_options.

  • path is the AJAX-capable controller action you're looking to call via GET. e.g. comments_stats_path, posts_path, etc.
  • html_options is an optional hash that gets passed to a rails javascript_tag, to drop html tags into the script element.

Example of utilizing html_options with a nonce:

<%= render_async users_path, nonce: 'lWaaV6eYicpt+oyOfcShYINsz0b70iR+Q1mohZqNaag=' %>

Rendered code in the view:

<div id="render_async_18b8a6cd161499117471">
</div>

<script nonce="lWaaV6eYicpt+oyOfcShYINsz0b70iR+Q1mohZqNaag=">
//<![CDATA[
  ...
//]]>
</script>

Passing in a placeholder

render_async can be called with a block that will act as a placeholder before your AJAX call finishes.

Example of passing in a block:

<%= render_async users_path do %>
  <h1>Users are loading...</h1>
<% end %>

Rendered code in the view:

<div id="render_async_14d7ac165d1505993721">
  <h1>Users are loading...</h1>
</div>

<script>
//<![CDATA[
  ...
//]]>
</script>

After AJAX is finished, placeholder will be replaced with the request's response.

Passing in an event name

render_async can receive :event_name option which will emit Javascript event after it's done with fetching and rendering request content to HTML.

This can be useful to have if you want to add some Javascript functionality after your partial is loaded through render_async.

Example of passing it to render_async:

<%= render_async users_path, :event_name => "users-loaded" %>

Rendered code in view:

<div id="render_async_04229e7abe1507987376">
</div>

<script>
//<![CDATA[
  ...
    document.dispatchEvent(new Event("users-loaded"));
  ...
//]]>
</script>

Then, in your JS, you could do something like this:

document.addEventListener("users-loaded", function() {
  console.log("Users have loaded!");
});

Caching

render_async can utilize view fragment caching to avoid extra AJAX calls.

In your views:

# app/views/comments/show.html.erb

# note 'render_async_cache' instead of standard 'render_async'
<%= render_async_cache comment_stats_path %>
# app/views/comments/_comment_stats.html.erb

<% cache render_async_cache_key(request.path), :skip_digest => true do %>
  <div class="col-md-6">
    <%= @stats %>
  </div>
<% end %>
  • The first time the page renders, it will make the AJAX call.
  • Any other times (until the cache expires), it will render from cache instantly, without making the AJAX call.
  • You can expire cache simply by passing :expires_in in your view where you cache the partial

Using with Turbolinks

On Turbolinks applications, you may experience caching issues when navigating away from, and then back to, a page with a render_async call on it. This will likely show up as an empty div.

To resolve, tell turbolinks to reload your render_async call as follows:

  <%= render_async events_path, 'data-turbolinks-track': 'reload' %>

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/renderedtext/render_async.

License

The gem is available as open source under the terms of the MIT License.

Contributors

Thanks goes to these wonderful people (emoji key):


Nikola ฤuza

๐Ÿ’ฌ ๐Ÿ› ๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿ‘€

Colin

๐Ÿ’ป ๐Ÿ“–

Kasper Grubbe

๐Ÿ’ป

Sai Ram Kunala

๐Ÿ“–

Josh Arnold

๐Ÿ’ป ๐Ÿ“–

Elad Shahar

๐Ÿ’ป

Sasha

๐Ÿ’ป ๐Ÿ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

render_async's People

Contributors

nikolalsvk avatar surgevelocityllc avatar colinxfleming avatar sasharevzin avatar kaspergrubbe avatar saladfork avatar sairam avatar

Watchers

Ernest Surudo avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.