render_async 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -6
- data/app/views/render_async/_render_async.html.erb +4 -0
- data/lib/render_async/version.rb +1 -1
- data/lib/render_async/view_helper.rb +7 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34364781d19ea77161a9be8861be690efc034fd1
|
4
|
+
data.tar.gz: 2cc3906d7c0c680f663694dc9d9224daf0091fc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
|
data/lib/render_async/version.rb
CHANGED
@@ -7,22 +7,25 @@ module RenderAsync
|
|
7
7
|
"render_async_#{path}"
|
8
8
|
end
|
9
9
|
|
10
|
-
def render_async_cache(path,
|
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,
|
16
|
+
render_async(path, options)
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
def render_async(path,
|
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:
|
27
|
+
html_options: options,
|
28
|
+
event_name: event_name,
|
26
29
|
placeholder: placeholder
|
27
30
|
end
|
28
31
|
|