All threads / How to use Rails Request.js Discussion
Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to use Rails Request.js Discussion

Hey Chris, this looks like a great addition to rails and thanks for adding the turbo stream support!

I'm working on an infinite scroll where at the end of the page a stimulus controller gets the next 20 results and uses a turbo stream to append them to the page (using a fetch request). I've basically adapted the following example:

https://www.stefanwienert.de/blog/2021/04/17/endless-scroll-with-turbo-streams/

I want to make some changes after the response comes back, but as far as I can tell, I can't use async/await. And I thought turbo-streams can't process get requests. I started rails at the beginning of the year (and am a rank amateur at programming in general) so may be wrong on both these points :)

Is it possible to use the Request.js library to do the get request and process a turbo stream?

Rookie mistake - forgot the damn 'async' before my js function...

So you can use request.js for the above and it's easy to do. I'm using pagy for pagination and followed Chris's other video on how to know when to trigger the next page load (https://gorails.com/episodes/infinite-scroll-stimulus-js) instead of the approach in the link in my original post.

One thing I found was that the turbo stream response was quick and often I'd get the next page loading twice. To get around this I used a simple setTimeout function before changing the loading variable to false:

const request = new FetchRequest("get", url, {responseKind: "turbo-stream"})
const response = await request.perform()
if (response.ok) {
  setTimeout(() => this.loading = false, 200)
}
Reply

Hey Chris - How would you re-render a partial after publish/unpublish? Add a turbo frame tag to the partial? I want to re-render the edit page.

Reply

hey chris, does hotwire replace ajax?

Reply
Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 54,919+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.