Rob

Joined

4,120 Experience
41 Lessons Completed
0 Questions Solved

Activity

Posted in How to use Rails Request.js Discussion

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)
}

Posted in 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?