render_async 2.1.4 → 2.1.5

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
  SHA256:
3
- metadata.gz: 35597f0995adbebc4f107d97119e8788124cab866f374fbf17145aed6d9b4cab
4
- data.tar.gz: bdf3a4230bed1b7a16f06247320e382afef3429bb807a5372cd42352f0350f31
3
+ metadata.gz: 5a8461ed53ed7d4d5518985762d2f76dec99936336c4cbfaffb1ff5e3ae91e10
4
+ data.tar.gz: e5b013fb3116c1edf08532cff838c54b51042e88c40ef1354b0d2ad211c4793b
5
5
  SHA512:
6
- metadata.gz: 4177b9a1a8c89b8a02059d0d2b2e871191ff5fec2f141de5321897884d5628dbc95468b5783f6ef8ae988c3a0fb82519d5a68c120fd4cbca88c22ea8209579b4
7
- data.tar.gz: '0149a06b46673296d341a253aa50219778216208ec1a7a30da869128d8380080652cd74dd40684cb97389107d551df0ec9fe71f1dfc9daa8cb62e0826a15132b'
6
+ metadata.gz: 75f915f3bdb92d38f5b9085377923423fbaa9bd72f39632d819c1114fa283bdd953a1fe3649d3c27f3609b01de951e24e8db008b8c9e5edf1ff2d1a792b809eb
7
+ data.tar.gz: d78fa9d2c96ee3676542ec6b4c0db93ad606917c225fd6386ed400c6ec9287ec70a9328eb2ae7e880faa04cb96e59e0ff3fc2ef3f469a3ee01bd720f8fe14849
@@ -177,6 +177,25 @@
177
177
  "contributions": [
178
178
  "code"
179
179
  ]
180
+ },
181
+ {
182
+ "login": "lipsumar",
183
+ "name": "Emmanuel Pire",
184
+ "avatar_url": "https://avatars3.githubusercontent.com/u/1191418?v=4",
185
+ "profile": "http://blog.lipsumarium.com",
186
+ "contributions": [
187
+ "code",
188
+ "doc"
189
+ ]
190
+ },
191
+ {
192
+ "login": "maximgeerinck",
193
+ "name": "Maxim Geerinck",
194
+ "avatar_url": "https://avatars1.githubusercontent.com/u/615509?v=4",
195
+ "profile": "https://github.com/maximgeerinck",
196
+ "contributions": [
197
+ "code"
198
+ ]
180
199
  }
181
200
  ],
182
201
  "repoType": "github"
@@ -0,0 +1,3 @@
1
+ github: nikolalsvk
2
+ tidelift: rubygems/render_async
3
+ custom: "https://www.paypal.me/nikolalsvk/10"
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  [![Build Status](https://semaphoreci.com/api/v1/renderedtext/render_async/branches/master/shields_badge.svg)](https://semaphoreci.com/renderedtext/render_async)
2
- [![All Contributors](https://img.shields.io/badge/all_contributors-18-orange.svg?style=flat-square)](#contributors)
2
+ [![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors)
3
3
  [![Gem Version](https://badge.fury.io/rb/render_async.svg)](https://badge.fury.io/rb/render_async)
4
+ [![Donate](https://img.shields.io/badge/$-support-green.svg)](https://www.paypal.me/nikolalsvk/10)
4
5
  [![Code Climate](https://codeclimate.com/github/renderedtext/render_async/badges/gpa.svg)](https://codeclimate.com/github/renderedtext/render_async)
5
6
  [![Test Coverage](https://codeclimate.com/github/renderedtext/render_async/badges/coverage.svg)](https://codeclimate.com/github/renderedtext/render_async/coverage)
6
7
  [![Help Contribute to Open Source](https://www.codetriage.com/renderedtext/render_async/badges/users.svg)](https://www.codetriage.com/renderedtext/render_async)
@@ -79,6 +80,7 @@ Advanced usage includes information on different options, such as:
79
80
  - [Passing in an HTML element name](#passing-in-an-html-element-name)
80
81
  - [Passing in a placeholder](#passing-in-a-placeholder)
81
82
  - [Passing in an event name](#passing-in-an-event-name)
83
+ - [Using default events](#using-default-events)
82
84
  - [Retry on failure](#retry-on-failure)
83
85
  - [Toggle event](#toggle-event)
84
86
  - [Polling](#polling)
@@ -133,21 +135,23 @@ Rendered code in the view:
133
135
  `html_options` is an optional hash that gets passed to a Rails'
134
136
  `javascript_tag`, to drop HTML tags into the `script` element.
135
137
 
136
- Example of utilizing `html_options` with a `nonce`:
138
+ Example of utilizing `html_options` with a [nonce](https://edgeguides.rubyonrails.org/security.html#content-security-policy):
137
139
  ```erb
138
- <%= render_async users_path, html_options: { nonce: 'lWaaV6eYicpt+oyOfcShYINsz0b70iR+Q1mohZqNaag=' } %>
140
+ <%= render_async users_path, html_options: { nonce: true } %>
139
141
  ```
140
142
 
141
143
  Rendered code in the view:
142
144
  ```html
143
- <div id="render_async_18b8a6cd161499117471">
144
- </div>
145
-
146
- <script nonce="lWaaV6eYicpt+oyOfcShYINsz0b70iR+Q1mohZqNaag=">
145
+ <script nonce="2x012CYGxKgM8qAApxRHxA==">
147
146
  //<![CDATA[
148
147
  ...
149
148
  //]]>
150
149
  </script>
150
+
151
+ ...
152
+
153
+ <div id="render_async_18b8a6cd161499117471" class="">
154
+ </div>
151
155
  ```
152
156
 
153
157
  ### Passing in an HTML element name
@@ -207,6 +211,9 @@ event after it's done with fetching and rendering request content to HTML.
207
211
  This can be useful to have if you want to add some JavaScript functionality
208
212
  after your partial is loaded through `render_async`.
209
213
 
214
+ You can also access the associated container (DOM node) in the event object
215
+ that gets emitted.
216
+
210
217
  Example of passing it to `render_async`:
211
218
  ```erb
212
219
  <%= render_async users_path, event_name: "users-loaded" %>
@@ -226,16 +233,49 @@ Rendered code in view:
226
233
  </script>
227
234
  ```
228
235
 
229
- Then, in your JS, you could do something like this:
236
+ Then, in your JavaScript code, you could do something like this:
230
237
  ```javascript
231
- document.addEventListener("users-loaded", function() {
232
- console.log("Users have loaded!");
238
+ document.addEventListener("users-loaded", function(event) {
239
+ console.log("Users have loaded!", event.container); // Access the container which loaded the users
233
240
  });
234
241
  ```
235
242
 
236
243
  NOTE: Dispatching events is also supported for older browsers that don't
237
244
  support Event constructor.
238
245
 
246
+ ### Using default events
247
+
248
+ `render_async` will fire the event `render_async_load` when an async partial
249
+ has loaded and rendered on page.
250
+
251
+ In case there is an error, the event `render_async_error` will fire instead.
252
+
253
+ This event will fire for all `render_async` partials on the page. For every
254
+ event, the associated container (DOM node) will be passed along.
255
+
256
+ This can be useful to apply JavaScript to content loaded after the page is
257
+ ready.
258
+
259
+ Example of using events:
260
+
261
+ ```js
262
+ // Vanilla javascript
263
+ document.addEventListener('render_async_load', function(event) {
264
+ console.log('Async partial loaded in this container:', event.container);
265
+ });
266
+ document.addEventListener('render_async_error', function(event) {
267
+ console.log('Async partial could not load in this container:', event.container);
268
+ });
269
+
270
+ // with jQuery
271
+ $(document).on('render_async_load', function(event, container) {
272
+ console.log('Async partial loaded in this container:', container);
273
+ });
274
+ $(document).on('render_async_error', function(event, container) {
275
+ console.log('Async partial could not load in this container:', container);
276
+ });
277
+ ```
278
+
239
279
  ### Retry on failure
240
280
 
241
281
  `render_async` can retry your requests if they fail for some reason.
@@ -246,7 +286,7 @@ this:
246
286
  <%= render_async users_path, retry_count: 5, error_message: "Couldn't fetch it" %>
247
287
  ```
248
288
 
249
- Now render_async will retry `users_path` for 5 times. If it succedes in
289
+ Now render_async will retry `users_path` for 5 times. If it succeeds in
250
290
  between, it will stop with dispatching requests. If it fails after 5 times,
251
291
  it will show an [error message](#handling-errors) which you need to specify.
252
292
 
@@ -267,7 +307,7 @@ do this by doing the following:
267
307
  ```
268
308
 
269
309
  This will trigger `render_async` to load the `comments_path` when you click the `#details-button` element.
270
- If you wannt to remove event once it's triggered, you can pass `once: true` in the toggle options.
310
+ If you want to remove event once it's triggered, you can pass `once: true` in the toggle options.
271
311
  The `once` option is false by default.
272
312
 
273
313
  You can also pass in a placeholder before the `render_async` is triggered. That
@@ -388,7 +428,7 @@ away from, and then back to, a page with a `render_async` call on it. This will
388
428
  likely show up as an empty div.
389
429
 
390
430
  If you're using Turbolinks 5 or higher, you can resolve this by setting Turbolinks
391
- configurtion of `render_async` to true:
431
+ configuration of `render_async` to true:
392
432
 
393
433
  ```rb
394
434
  RenderAsync.configure do |config|
@@ -512,7 +552,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
512
552
  | [<img src="https://avatars2.githubusercontent.com/u/3028124?v=4" width="100px;"/><br /><sub><b>Nikola Đuza</b></sub>](https://nikolalsvk.github.io)<br />[💬](#question-nikolalsvk "Answering Questions") [💻](https://github.com/renderedtext/render_async/commits?author=nikolalsvk "Code") [📖](https://github.com/renderedtext/render_async/commits?author=nikolalsvk "Documentation") [👀](#review-nikolalsvk "Reviewed Pull Requests") | [<img src="https://avatars0.githubusercontent.com/u/3866868?v=4" width="100px;"/><br /><sub><b>Colin</b></sub>](http://www.colinxfleming.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=colinxfleming "Code") [📖](https://github.com/renderedtext/render_async/commits?author=colinxfleming "Documentation") [💡](#example-colinxfleming "Examples") | [<img src="https://avatars2.githubusercontent.com/u/334273?v=4" width="100px;"/><br /><sub><b>Kasper Grubbe</b></sub>](http://kaspergrubbe.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=kaspergrubbe "Code") | [<img src="https://avatars2.githubusercontent.com/u/163584?v=4" width="100px;"/><br /><sub><b>Sai Ram Kunala</b></sub>](https://sairam.xyz/)<br />[📖](https://github.com/renderedtext/render_async/commits?author=sairam "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3065882?v=4" width="100px;"/><br /><sub><b>Josh Arnold</b></sub>](https://github.com/nightsurge)<br />[💻](https://github.com/renderedtext/render_async/commits?author=nightsurge "Code") [📖](https://github.com/renderedtext/render_async/commits?author=nightsurge "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/107798?v=4" width="100px;"/><br /><sub><b>Elad Shahar</b></sub>](https://eladshahar.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=SaladFork "Code") [💡](#example-SaladFork "Examples") | [<img src="https://avatars3.githubusercontent.com/u/232392?v=4" width="100px;"/><br /><sub><b>Sasha</b></sub>](http://www.revzin.co.il)<br />[💻](https://github.com/renderedtext/render_async/commits?author=sasharevzin "Code") [📖](https://github.com/renderedtext/render_async/commits?author=sasharevzin "Documentation") |
513
553
  | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
514
554
  | [<img src="https://avatars3.githubusercontent.com/u/50223?v=4" width="100px;"/><br /><sub><b>Ernest Surudo</b></sub>](http://elsurudo.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=elsurudo "Code") | [<img src="https://avatars1.githubusercontent.com/u/334809?v=4" width="100px;"/><br /><sub><b>Kurtis Rainbolt-Greene</b></sub>](https://kurtis.rainbolt-greene.online)<br />[💻](https://github.com/renderedtext/render_async/commits?author=krainboltgreene "Code") | [<img src="https://avatars2.githubusercontent.com/u/59744?v=4" width="100px;"/><br /><sub><b>Richard Schneeman</b></sub>](https://www.schneems.com)<br />[📖](https://github.com/renderedtext/render_async/commits?author=schneems "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/75705?v=4" width="100px;"/><br /><sub><b>Richard Venneman</b></sub>](https://www.cityspotters.com)<br />[📖](https://github.com/renderedtext/render_async/commits?author=richardvenneman "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/381395?v=4" width="100px;"/><br /><sub><b>Filipe W. Lima</b></sub>](https://github.com/filipewl)<br />[📖](https://github.com/renderedtext/render_async/commits?author=filipewl "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/3135638?v=4" width="100px;"/><br /><sub><b>Jesús Eduardo Clemens Chong</b></sub>](https://github.com/eclemens)<br />[💻](https://github.com/renderedtext/render_async/commits?author=eclemens "Code") | [<img src="https://avatars3.githubusercontent.com/u/1935686?v=4" width="100px;"/><br /><sub><b>René Klačan</b></sub>](https://github.com/reneklacan)<br />[💻](https://github.com/renderedtext/render_async/commits?author=reneklacan "Code") |
515
- | [<img src="https://avatars1.githubusercontent.com/u/1313442?v=4" width="100px;"/><br /><sub><b>Gil Gomes</b></sub>](http://gilgomes.com.br)<br />[📖](https://github.com/renderedtext/render_async/commits?author=gil27 "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/6081795?v=4" width="100px;"/><br /><sub><b>Khoa Nguyen</b></sub>](https://github.com/ThanhKhoaIT)<br />[💻](https://github.com/renderedtext/render_async/commits?author=ThanhKhoaIT "Code") [📖](https://github.com/renderedtext/render_async/commits?author=ThanhKhoaIT "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/8645918?v=4" width="100px;"/><br /><sub><b>Preet Sethi</b></sub>](https://www.linkedin.com/in/preetsethila/)<br />[💻](https://github.com/renderedtext/render_async/commits?author=preetsethi "Code") | [<img src="https://avatars3.githubusercontent.com/u/11586335?v=4" width="100px;"/><br /><sub><b>fangxing</b></sub>](https://github.com/fffx)<br />[💻](https://github.com/renderedtext/render_async/commits?author=fffx "Code") |
555
+ | [<img src="https://avatars1.githubusercontent.com/u/1313442?v=4" width="100px;"/><br /><sub><b>Gil Gomes</b></sub>](http://gilgomes.com.br)<br />[📖](https://github.com/renderedtext/render_async/commits?author=gil27 "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/6081795?v=4" width="100px;"/><br /><sub><b>Khoa Nguyen</b></sub>](https://github.com/ThanhKhoaIT)<br />[💻](https://github.com/renderedtext/render_async/commits?author=ThanhKhoaIT "Code") [📖](https://github.com/renderedtext/render_async/commits?author=ThanhKhoaIT "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/8645918?v=4" width="100px;"/><br /><sub><b>Preet Sethi</b></sub>](https://www.linkedin.com/in/preetsethila/)<br />[💻](https://github.com/renderedtext/render_async/commits?author=preetsethi "Code") | [<img src="https://avatars3.githubusercontent.com/u/11586335?v=4" width="100px;"/><br /><sub><b>fangxing</b></sub>](https://github.com/fffx)<br />[💻](https://github.com/renderedtext/render_async/commits?author=fffx "Code") | [<img src="https://avatars3.githubusercontent.com/u/1191418?v=4" width="100px;"/><br /><sub><b>Emmanuel Pire</b></sub>](http://blog.lipsumarium.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=lipsumar "Code") [📖](https://github.com/renderedtext/render_async/commits?author=lipsumar "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/615509?v=4" width="100px;"/><br /><sub><b>Maxim Geerinck</b></sub>](https://github.com/maximgeerinck)<br />[💻](https://github.com/renderedtext/render_async/commits?author=maximgeerinck "Code") |
516
556
  <!-- ALL-CONTRIBUTORS-LIST:END -->
517
557
 
518
558
  This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
@@ -5,8 +5,27 @@ if (window.jQuery) {
5
5
  return;
6
6
  }
7
7
  <% end %>
8
+ function createEvent(name, container) {
9
+ var event = undefined;
10
+ if (typeof(Event) === 'function') {
11
+ event = new Event(name);
12
+ } else {
13
+ event = document.createEvent('Event');
14
+ event.initEvent(name, true, true);
15
+ }
16
+ event.container = container
17
+ return event;
18
+ }
8
19
 
9
- var _makeRequest = function(currentRetryCount) {
20
+ function _runAfterDocumentLoaded(callback) {
21
+ <% if turbolinks %>
22
+ $(document).one('turbolinks:load', callback);
23
+ <% else %>
24
+ $(document).ready(callback);
25
+ <% end %>
26
+ }
27
+
28
+ function _makeRequest(currentRetryCount) {
10
29
  var headers = <%= headers.to_json.html_safe %>;
11
30
  var csrfTokenElement = document.querySelector('meta[name="csrf-token"]')
12
31
  if (csrfTokenElement)
@@ -18,21 +37,19 @@ if (window.jQuery) {
18
37
  data: "<%= escape_javascript(data.to_s.html_safe) %>",
19
38
  headers: headers
20
39
  }).done(function(response) {
40
+ var container = $("#<%= container_id %>");
21
41
  <% if interval %>
22
- $("#<%= container_id %>").empty();
23
- $("#<%= container_id %>").append(response);
42
+ container.empty();
43
+ container.append(response);
24
44
  <% else %>
25
- $("#<%= container_id %>").replaceWith(response);
45
+ container.replaceWith(response);
26
46
  <% end %>
27
47
 
48
+ var loadEvent = createEvent('render_async_load', container);
49
+ document.dispatchEvent(loadEvent);
50
+
28
51
  <% if event_name.present? %>
29
- var event = undefined;
30
- if (typeof(Event) === 'function') {
31
- event = new Event("<%= event_name %>");
32
- } else {
33
- event = document.createEvent('Event');
34
- event.initEvent('<%= event_name %>', true, true);
35
- }
52
+ var event = createEvent("<%= event_name %>", container)
36
53
  document.dispatchEvent(event);
37
54
  <% end %>
38
55
  }).fail(function(response) {
@@ -43,23 +60,22 @@ if (window.jQuery) {
43
60
 
44
61
  if (skipErrorMessage) return;
45
62
 
46
- $("#<%= container_id %>").replaceWith("<%= error_message.try(:html_safe) %>");
63
+ var container = $("#<%= container_id %>");
64
+ container.replaceWith("<%= error_message.try(:html_safe) %>");
65
+
66
+ var errorEvent = createEvent('render_async_error', container);
67
+ errorEvent.container = container;
68
+ document.dispatchEvent(errorEvent);
47
69
 
48
70
  <% if error_event_name.present? %>
49
- var event = undefined;
50
- if (typeof(Event) === 'function') {
51
- event = new Event("<%= error_event_name %>");
52
- } else {
53
- event = document.createEvent('Event');
54
- event.initEvent('<%= error_event_name %>', true, true);
55
- }
71
+ var event = createEvent("<%= error_event_name %>", container)
56
72
  document.dispatchEvent(event);
57
73
  <% end %>
58
74
  });
59
75
  };
60
76
 
61
77
  <% if retry_count > 0 %>
62
- var retry = function(currentRetryCount) {
78
+ function retry(currentRetryCount) {
63
79
  if (typeof(currentRetryCount) === 'number') {
64
80
  if (currentRetryCount >= <%= retry_count %>)
65
81
  return false;
@@ -84,21 +100,21 @@ if (window.jQuery) {
84
100
  <% end %>
85
101
 
86
102
  <% if toggle %>
87
- $(document).<%= toggle[:once] ? 'one' : 'on' %>('<%= toggle[:event] || 'click' %>', '<%= toggle[:selector] %>', function(event) {
88
- event.preventDefault();
89
- if (typeof(_interval) === 'number') {
90
- clearInterval(_interval);
91
- _interval = undefined;
92
- } else {
93
- _renderAsyncFunction();
94
- }
95
- });
96
- <% end %>
103
+ function _setUpToggle() {
104
+ $(document).<%= toggle[:once] ? 'one' : 'on' %>('<%= toggle[:event] || 'click' %>', '<%= toggle[:selector] %>', function(event) {
105
+ event.preventDefault();
106
+ if (typeof(_interval) === 'number') {
107
+ clearInterval(_interval);
108
+ _interval = undefined;
109
+ } else {
110
+ _renderAsyncFunction();
111
+ }
112
+ });
113
+ }
97
114
 
98
- <% if turbolinks && !toggle %>
99
- $(document).one('turbolinks:load', _renderAsyncFunction);
115
+ _runAfterDocumentLoaded(_setUpToggle)
100
116
  <% elsif !toggle %>
101
- $(document).ready(_renderAsyncFunction);
117
+ _runAfterDocumentLoaded(_renderAsyncFunction)
102
118
  <% end %>
103
119
  }(jQuery));
104
120
  } else {
@@ -4,8 +4,30 @@
4
4
  return;
5
5
  }
6
6
  <% end %>
7
+ function createEvent(name, container) {
8
+ var event = undefined;
9
+ if (typeof(Event) === 'function') {
10
+ event = new Event(name);
11
+ } else {
12
+ event = document.createEvent('Event');
13
+ event.initEvent(name, true, true);
14
+ }
15
+ event.container = container
16
+ return event;
17
+ }
7
18
 
8
- var _makeRequest = function(currentRetryCount) {
19
+ function _runAfterDocumentLoaded(callback) {
20
+ <% if turbolinks %>
21
+ document.addEventListener("turbolinks:load", function(e) {
22
+ e.target.removeEventListener(e.type, arguments.callee);
23
+ callback();
24
+ });
25
+ <% else %>
26
+ document.addEventListener("DOMContentLoaded", callback);
27
+ <% end %>
28
+ }
29
+
30
+ function _makeRequest(currentRetryCount) {
9
31
  var request = new XMLHttpRequest();
10
32
  var asyncRequest = true;
11
33
  var SUCCESS = 200;
@@ -32,14 +54,11 @@
32
54
  container.outerHTML = request.response;
33
55
  <% end %>
34
56
 
57
+ var loadEvent = createEvent('render_async_load', container);
58
+ document.dispatchEvent(loadEvent);
59
+
35
60
  <% if event_name.present? %>
36
- var event = undefined;
37
- if (typeof(Event) === 'function') {
38
- event = new Event("<%= event_name %>");
39
- } else {
40
- event = document.createEvent('Event');
41
- event.initEvent('<%= event_name %>', true, true);
42
- }
61
+ var event = createEvent('<%= event_name %>', container);
43
62
  document.dispatchEvent(event);
44
63
  <% end %>
45
64
  } else {
@@ -51,16 +70,14 @@
51
70
  if (skipErrorMessage) return;
52
71
 
53
72
  var container = document.getElementById('<%= container_id %>');
54
- container.outerHTML = "<%= error_message.try(:html_safe) %>";
73
+ container.outerHTML = '<%= error_message.try(:html_safe) %>';
74
+
75
+ console.log(container)
76
+ var errorEvent = createEvent('render_async_error', container);
77
+ document.dispatchEvent(errorEvent);
55
78
 
56
79
  <% if error_event_name.present? %>
57
- var event = undefined;
58
- if (typeof(Event) === 'function') {
59
- event = new Event("<%= error_event_name %>");
60
- } else {
61
- event = document.createEvent('Event');
62
- event.initEvent('<%= error_event_name %>', true, true);
63
- }
80
+ var event = createEvent('<%= error_event_name %>', container);
64
81
  document.dispatchEvent(event);
65
82
  <% end %>
66
83
  }
@@ -72,7 +89,7 @@
72
89
  };
73
90
 
74
91
  <% if retry_count > 0 %>
75
- var retry = function(currentRetryCount) {
92
+ function retry(currentRetryCount) {
76
93
  if (typeof(currentRetryCount) === 'number') {
77
94
  if (currentRetryCount >= <%= retry_count %>)
78
95
  return false;
@@ -97,31 +114,28 @@
97
114
  <% end %>
98
115
 
99
116
  <% if toggle %>
100
- var selectors = document.querySelectorAll('<%= toggle[:selector] %>');
101
- var handler = function(event) {
102
- event.preventDefault();
103
- if (typeof(_interval) === 'number') {
104
- clearInterval(_interval);
105
- _interval = undefined;
106
- } else {
107
- _renderAsyncFunction();
108
- }
109
- <% if toggle[:once] %>
110
- this.removeEventListener(event.type, handler);
111
- <% end %>
112
- };
117
+ function _setUpToggle() {
118
+ var selectors = document.querySelectorAll('<%= toggle[:selector] %>'), i;
119
+ var handler = function(event) {
120
+ event.preventDefault();
121
+ if (typeof(_interval) === 'number') {
122
+ clearInterval(_interval);
123
+ _interval = undefined;
124
+ } else {
125
+ _renderAsyncFunction();
126
+ }
127
+ <% if toggle[:once] %>
128
+ this.removeEventListener(event.type, handler);
129
+ <% end %>
130
+ };
113
131
 
114
- [...selectors].forEach(function(selector) {
115
- selector.addEventListener('<%= toggle[:event] || 'click' %>', handler)
116
- });
117
- <% end %>
132
+ for (i = 0; i < selectors.length; ++i) {
133
+ selectors[i].addEventListener('<%= toggle[:event] || 'click' %>', handler)
134
+ }
135
+ }
118
136
 
119
- <% if turbolinks && !toggle %>
120
- document.addEventListener("turbolinks:load", function (e) {
121
- e.target.removeEventListener(e.type, arguments.callee);
122
- _renderAsyncFunction();
123
- });
137
+ _runAfterDocumentLoaded(_setUpToggle);
124
138
  <% elsif !toggle %>
125
- document.addEventListener("DOMContentLoaded", _renderAsyncFunction);
139
+ _runAfterDocumentLoaded(_renderAsyncFunction);
126
140
  <% end %>
127
141
  })();
@@ -1,3 +1,3 @@
1
1
  module RenderAsync
2
- VERSION = "2.1.4".freeze
2
+ VERSION = "2.1.5".freeze
3
3
  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: 2.1.4
4
+ version: 2.1.5
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: 2019-11-11 00:00:00.000000000 Z
12
+ date: 2020-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -61,6 +61,7 @@ extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
63
  - ".all-contributorsrc"
64
+ - ".github/FUNDING.yml"
64
65
  - ".gitignore"
65
66
  - ".gitmodules"
66
67
  - ".rspec"