render_async 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aec789eda10aa5db3c538756631ee0e6963c6f5e
4
- data.tar.gz: 424668e8679890bbf1b8d37bf4ec1e56a7c7cbaf
3
+ metadata.gz: 34364781d19ea77161a9be8861be690efc034fd1
4
+ data.tar.gz: 2cc3906d7c0c680f663694dc9d9224daf0091fc4
5
5
  SHA512:
6
- metadata.gz: 6cd574849e185137bf16f02b7502ea4f3d0d18deedbf383b289e8f5ee0455c49d55a077f298b01a4338730e5198dda222dc2fbdb03ebe99964ba615b7763c555
7
- data.tar.gz: 508546ce17b2ffad8e730c1050dc484b266dfb894318c5c8b9693e8e48a53e3ff42555970840d1bec4ade0dcc6b029ca5349437d51d1d8f8f575551c213f4858
6
+ metadata.gz: 663f85b5338e9348745f4b515aa630b0e035fa7e598a6adfa7b7d8848e8fa25f20eec8eb1d65120f5885b3bc09314fa074cd617b4f002f028178b059757a1720
7
+ data.tar.gz: e5e3b70a41ae41946651c20204b7414bde2073cc70803fd8b1a1e46ab3a3766d0148d4267bc64f8d9d289bb289a69be4448b72b34abbb24a865efaad7be54ab1
data/README.md CHANGED
@@ -164,6 +164,54 @@ Rendered code in the view:
164
164
  After AJAX is finished, placeholder will be replaced with the request's
165
165
  response.
166
166
 
167
+ ### Passing in an event name
168
+
169
+ `render_async` can receive `:event_name` option which will emit Javascript
170
+ event after it's done with fetching and rendering request content to HTML.
171
+
172
+ This can be useful to have if you want to add some Javascript functionality
173
+ after your partial is loaded through `render_async`.
174
+
175
+ Example of passing it to `render_async`:
176
+ ```erb
177
+ <%= render_async users_path, :event_name => "users-loaded" %>
178
+ ```
179
+
180
+ Rendered code in view:
181
+ ```html
182
+ <div id="render_async_04229e7abe1507987376">
183
+ </div>
184
+
185
+ <script>
186
+ //<![CDATA[
187
+ (function() {
188
+ var request = new XMLHttpRequest();
189
+ var asyncRequest = true;
190
+ var SUCCESS = 200;
191
+ var ERROR = 400;
192
+ request.open("GET", "/users", asyncRequest);
193
+
194
+ request.onload = function() {
195
+ if (request.status >= SUCCESS && request.status < ERROR) {
196
+ document.getElementById("render_async_04229e7abe1507987376").outerHTML = request.responseText;
197
+
198
+ document.dispatchEvent(new Event("users-loaded"));
199
+ }
200
+ };
201
+
202
+ request.send();
203
+ })();
204
+ //]]>
205
+ </script>
206
+ ```
207
+
208
+ Then, in your JS, you could do something like this:
209
+ ```javascript
210
+ document.addEventListener("users-loaded", function() {
211
+ console.log("Users have loaded!");
212
+ });
213
+ ```
214
+
167
215
  ## Caching
168
216
 
169
217
  `render_async` can utilize view fragment caching to avoid extra AJAX calls.
@@ -210,12 +258,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
210
258
  `rake spec` to run the tests. You can also run `bin/console` for an interactive
211
259
  prompt that will allow you to experiment.
212
260
 
213
- To install this gem onto your local machine, run `bundle exec rake install`. To
214
- release a new version, update the version number in `version.rb`, and then run
215
- `bundle exec rake release`, which will create a git tag for the version, push
216
- git commits and tags, and push the `.gem` file to
217
- [rubygems.org](https://rubygems.org).
218
-
219
261
  ## Contributing
220
262
 
221
263
  Bug reports and pull requests are welcome on GitHub at https://github.com/renderedtext/render_async.
@@ -15,6 +15,10 @@
15
15
  request.onload = function() {
16
16
  if (request.status >= SUCCESS && request.status < ERROR) {
17
17
  document.getElementById("<%= container_id %>").outerHTML = request.responseText;
18
+
19
+ <% if event_name.present? %>
20
+ document.dispatchEvent(new Event("<%= event_name %>"));
21
+ <% end %>
18
22
  }
19
23
  };
20
24
 
@@ -1,3 +1,3 @@
1
1
  module RenderAsync
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -7,22 +7,25 @@ module RenderAsync
7
7
  "render_async_#{path}"
8
8
  end
9
9
 
10
- def render_async_cache(path, html_options = {})
10
+ def render_async_cache(path, options = {})
11
11
  cached_view = Rails.cache.read("views/#{render_async_cache_key(path)}")
12
+
12
13
  if cached_view.present?
13
14
  render :html => cached_view.html_safe
14
15
  else
15
- render_async(path, html_options)
16
+ render_async(path, options)
16
17
  end
17
18
  end
18
19
 
19
- def render_async(path, html_options = {}, &placeholder)
20
+ def render_async(path, options = {}, &placeholder)
20
21
  container_id = "render_async_#{SecureRandom.hex(5)}#{Time.now.to_i}"
22
+ event_name = options.delete(:event_name)
21
23
  placeholder = capture(&placeholder) if block_given?
22
24
 
23
25
  render 'render_async/render_async', container_id: container_id,
24
26
  path: path,
25
- html_options: html_options,
27
+ html_options: options,
28
+ event_name: event_name,
26
29
  placeholder: placeholder
27
30
  end
28
31
 
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: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Grubbe