Following the Ajax trail
At work, I’m helping to build a new application that utilizes ajax to the fullest. This is bar none the most ajax intensive application I’ve worked on. It’s awesome, though. I’m learning so much about Ruby and Rails. I knew I would if I were to do this stuff at work instead of trying to find the time between work, school, and hanging out with Amie. Anyway, this afternoon was the most severe case of ajax I’ve come across.
Sometimes it can be tricky to follow the variables and keep track of IDs and requests when you have multiple ajax calls without a page refresh. For instance, just today, I had to add the ability to post entries related to [a top secret something] and allow comments for those entries. Also, this is a multi-user application, so if the entry belongs to you, allow editing of the title and body, as well as your comments (if any) for that entry. Thankfully, Rails has tremendous support for ajax. This is all fairly straight forward, but the part I was having slight trouble with was if a user posted an entry, wanted to add a comment, edit the entry, add another comment, etc., all without refreshing the page (or upon return). Each time I came back from an ajax call, I had to keep track of the unique <div> IDs so the other ajax requests that were rendered in the previous response would still work and update the same locations as if it were a fresh visit. For instance, let’s say this happened without a page refresh: click to post a new entry (request 1), submit the entry (request 2), edit the entry body (request 3), view the comments (request 4), add a comment (request 5), edit the entry title (request 6), edit the comment (request 7), delete the comment (request 8), and finally, delete the entry (request 9). Essentially, that could happen. Even though it’s unlikely, it has to work just in case. It’s always those wacky situations that bite. So after tracing through the issues for a bit, it started coming together. I got it all worked out this afternoon (I still need some slight fixes on the comments) and it seems to be flawless so far. It always feels good to reach a good stopping point on a Friday, and it feels even better now that I have it commited in the repository :) Out of curiousity, what’s the craziest ajax trail you’ve had to follow?
Update: I fixed the comments tonight, and tested everything. It’s all working as planned. Sweet!
