Comments (6)
Yes, I think is vital to know the element where the response was renderer. I think this should be passed to this event.
So we can do something like:
document.addEventListener("users-loaded", function(element) {
$('a', element).click(function(){...})
});
(just a random example)
from render_async.
Hey @neohunter, I've been trying to make some progress on this issue and I have couple of ideas.
As of current implementation, you are in the control of what element get's rendered on the page. What I mean by that is that the placeholder element is replaced by the content you return from the server. That's why we're dispatching the event from the document
element, instead of a specific element inside the document.
So, I'm suggesting that you return some content that you can easily get in your event callback function.
Let's say you return this from the server:
<p id='user-comment'>a comment</p>
You could then get the rendered element in your event callback like this:
document.addEventListener("users-loaded", function() {
var loadedElement = $('#user-comment')
});
How does this sound to you?
from render_async.
Thanks for checking out the previous solution I posted @filipewl!
Another approach that we could do by changing the VanillaJS is to use innerHTML
instead of outerHTML
. With innerHTML
we will have the reference to the parent object, but this would be a breaking change for most of the users (I think) since parent element won't get replaced.
We could also create an use case where gem will use innerHTML
with some configuration option passed to it. We would then leave outerHTML
to be the default behaviour and then slowly progress to innerHTML
solution as versions progress further :)
from render_async.
Hey @neohunter, thanks for submitting the issue.
It's not possible to access the element where the request was rendered.
Can you explain a bit more what is your use case? Do you want to know ID of the element that shows the response data?
from render_async.
The above approach should work, I've just tested it.
From what I've been reading, we don't have a way to send what's been rendered to the event in the vanilla JS part of the code. We replace the placeholder element with the request's response using outerHTML
, but according to this docs we don't have a reference to the rendered elements:
var p = document.getElementsByTagName("p")[0];
console.log(p.nodeName); // shows: "P"
p.outerHTML = "<div>This div replaced a paragraph.</div>";
console.log(p.nodeName); // still "P";
The only way to get a reference to the rendered elements is to query the DOM for them afterwards.
from render_async.
I think we answered this question in couple of ways, if there are some more inquiries about it, I'll reopen it.
from render_async.
Related Issues (20)
- 2.1.5 breaks manual trigger of click event HOT 13
- preventDefault() in toggle code interferes with other UI functionality HOT 2
- Feature idea: Render async with ActiveJob & ActionCable HOT 2
- Render_async on header HOT 2
- Duplication of injected JS for nested async templates HOT 1
- Start polling immediately option when toggle is specified HOT 1
- Manual retry based on JS event HOT 3
- Idea: only fire the AJAX request on scroll (Lazy Partials) HOT 1
- Event driven reloads. HOT 3
- Polling: vanilla js has code that runs before document has finished loading HOT 2
- Feature request: Refresh with event HOT 5
- Feature: Append or prepend content on toggle HOT 1
- Content flashes on page revisit even with Turbolinks enabled HOT 6
- render_async issues with container_id and turbolinks : not reloading or JS not firing on new visit depending on configuration HOT 5
- Add Support for Turbo HOT 3
- Add delay before performing async call HOT 4
- content_for :render_async does nothing. HOT 1
- With Caching enabled, event_name is not triggered as content_for is missing on page reload HOT 5
- Turbo: Toggable selector not executing the event_listener function set by _setUpToggle on a new visit HOT 1
- JS scripts not loading synchronously HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from render_async.