render_async 0.4.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c054d37191e7387bd9fadc6c8ab5879c01db45a5
4
- data.tar.gz: afaf4b5b8a78464a776a8bf8c0bb3c21e15bac6e
3
+ metadata.gz: 4cb50b82faad4e5b955dee426984e606277e5000
4
+ data.tar.gz: 30b577cb393842086bb39b0b52c3913d62cec3c0
5
5
  SHA512:
6
- metadata.gz: 859086a41d3f75868629eb650639a24bd28aa48c74966a2152545800f6adb0c37dfa3e7d1db7c2e1a90f03cbdeb56777d8f9fb0411527245a97e34124a9213b0
7
- data.tar.gz: 750ecc2a1fff9dbdfe518f986084b30dc19633e4859e60ba68ca33d24a75bcd9ba12479f29e9eab51a3b2ec2fba7034011fd712108e35ba371508b8398b0e508
6
+ metadata.gz: cefdcad421559d06858b1b13e9e70f6c4c79781d7dbb8255ea6265c4e09079733092ea25238c7610c0e803c326b11f8fe34ff50bd953582cdf3ca4fcf4a1bea6
7
+ data.tar.gz: e01ae7a2d71bec7d25f853e0edc640b3f83786e1d3143943e0eee46ab549584fa9b1280f19f5359b52105525cc19c066658a753524735672a6ae2aca5e234e70
data/README.md CHANGED
@@ -12,9 +12,9 @@ your partial.
12
12
 
13
13
  Workflow:
14
14
 
15
- 1. user visits a page =>
16
- 2. AJAX request on the controller action =>
17
- 3. controller renders a partial =>
15
+ 1. user visits a page
16
+ 2. AJAX request on the controller action
17
+ 3. controller renders a partial
18
18
  4. partials renders in the place where you put `render_async` helper
19
19
 
20
20
  Javascript is injected into `<%= content_for :render_async %>` so you choose
@@ -77,12 +77,16 @@ And then execute:
77
77
 
78
78
  ## Advanced usage
79
79
 
80
+ ### Passing in HTML options
81
+
80
82
  `render_async` takes two arguments, `path` and `html_options`.
81
83
 
82
- * `path` is the ajax-capable controller action you're looking to call via `get`. e.g. `comments_stats_path`, `posts_path`, etc.
83
- * `html_options` is an optional hash that gets passed to a rails `javascript_tag`, to drop html tags into the `script` element.
84
+ * `path` is the AJAX-capable controller action you're looking to call via
85
+ `GET`. e.g. `comments_stats_path`, `posts_path`, etc.
86
+ * `html_options` is an optional hash that gets passed to a rails
87
+ `javascript_tag`, to drop html tags into the `script` element.
84
88
 
85
- Example utilizing `html_options` with a `nonce`:
89
+ Example of utilizing `html_options` with a `nonce`:
86
90
  ```erb
87
91
  <%= render_async users_path, nonce: 'lWaaV6eYicpt+oyOfcShYINsz0b70iR+Q1mohZqNaag=' %>
88
92
  ```
@@ -90,34 +94,56 @@ Example utilizing `html_options` with a `nonce`:
90
94
  Rendered code in the view:
91
95
  ```html
92
96
  <div id="render_async_18b8a6cd161499117471">
93
- <div id="render_async_18b8a6cd161499117471_spinner" class="sk-spinner sk-spinner-double-bounce">
94
- <div class="sk-double-bounce1"></div>
95
- <div class="sk-double-bounce2"></div>
96
- </div>
97
97
  </div>
98
98
 
99
99
  <script nonce="lWaaV6eYicpt+oyOfcShYINsz0b70iR+Q1mohZqNaag=">
100
100
  //<![CDATA[
101
101
 
102
102
  (function($){
103
- $.ajax({
104
- url: "/users",
105
- })
106
- .done(function(response, status) {
107
- $("#render_async_18b8a6cd161499117471").html(response);
108
- })
109
- .fail(function(response, status) {
110
- $("#render_async_18b8a6cd161499117471").html(response);
111
- })
112
- .always(function(response, status) {
113
- $("#render_async_18b8a6cd161499117471_spinner").hide();
114
- });
103
+ $.ajax({ url: "/users" }).always(function(response) {
104
+ $("#render_async_18b8a6cd161499117471").replaceWith(response);
105
+ });
115
106
  }(jQuery));
116
107
 
117
108
  //]]>
118
109
  </script>
119
110
  ```
120
111
 
112
+ ### Passing in a placeholder
113
+
114
+ `render_async` can be called with a block that will act as a placeholder before
115
+ your AJAX call finishes.
116
+
117
+ Example of passing in a block:
118
+
119
+ ```erb
120
+ <%= render_async users_path do %>
121
+ <h1>Users are loading...</h1>
122
+ <% end %>
123
+ ```
124
+
125
+ Rendered code in the view:
126
+ ```html
127
+ <div id="render_async_14d7ac165d1505993721">
128
+ <h1>Users are loading...</h1>
129
+ </div>
130
+
131
+ <script>
132
+ //<![CDATA[
133
+
134
+ (function($){
135
+ $.ajax({ url: "/users" }).always(function(response) {
136
+ $("#render_async_14d7ac165d1505993721").replaceWith(response);
137
+ });
138
+ }(jQuery));
139
+
140
+ //]]>
141
+ </script>
142
+ ```
143
+
144
+ After AJAX is finished, placeholder will be replaced with the request's
145
+ response.
146
+
121
147
  ## Caching
122
148
 
123
149
  `render_async` can utilize view fragment caching to avoid extra AJAX calls.
@@ -1,15 +1,12 @@
1
- <div id="<%= container_name %>">
2
- <div id="<%= container_name %>_spinner" class="sk-spinner sk-spinner-double-bounce">
3
- <div class="sk-double-bounce1"></div>
4
- <div class="sk-double-bounce2"></div>
5
- </div>
1
+ <div id="<%= container_id %>">
2
+ <%= placeholder %>
6
3
  </div>
7
4
 
8
5
  <% content_for :render_async do %>
9
6
  <%= javascript_tag html_options do %>
10
7
  (function($){
11
- $.ajax({ url: "<%= path %>"}).always(function(response) {
12
- $("#<%= container_name %>").replaceWith(response);
8
+ $.ajax({ url: "<%= path %>" }).always(function(response) {
9
+ $("#<%= container_id %>").replaceWith(response);
13
10
  });
14
11
  }(jQuery));
15
12
  <% end %>
@@ -1,3 +1,3 @@
1
1
  module RenderAsync
2
- VERSION = "0.4.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -16,12 +16,14 @@ module RenderAsync
16
16
  end
17
17
  end
18
18
 
19
- def render_async(path, html_options = {})
20
- container_name = "render_async_#{SecureRandom.hex(5)}#{Time.now.to_i}"
19
+ def render_async(path, html_options = {}, &placeholder)
20
+ container_id = "render_async_#{SecureRandom.hex(5)}#{Time.now.to_i}"
21
+ placeholder = capture(&placeholder) if block_given?
21
22
 
22
- render "render_async/render_async", :container_name => container_name,
23
- :path => path,
24
- :html_options => html_options
23
+ render 'render_async/render_async', container_id: container_id,
24
+ path: path,
25
+ html_options: html_options,
26
+ placeholder: placeholder
25
27
  end
26
28
 
27
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_async
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Grubbe
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-09-07 00:00:00.000000000 Z
12
+ date: 2017-09-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler