RJS, Prototype and finding out if object exists in the DOM

Hi Moe,

Moe Zecko wrote:

how is it possible to find out, if an object (div) exists in the DOM via RJS? i am trying to find out if an object exists before applying a visual action on it, so there is no javascript error produced.

Interesting question. I'll start by saying I still consider myself pretty new to Rails, js, and Ajax. I was out of hands-on development for 15 years and have only been working with Rails for a little over a year now. So I either don't understand your question, or your context, or both :wink: More below.

i am wondering why nobody seems to have had this problem allready. how do you guys prevent javascript errors? is there another way to have sort of safe RJS calls?

I think Rails probably eliminates most of the need for the kind of thing you're asking about. In general, I know that something exists in the DOM because I put it there. 99.9% of the time, the page content in a Rails app is rendered dynamically. If it's on the page, it's there because I just put it there; typically as a result of something that's stored in the database. In your case, it looks like you're probably doing or could be doing a find on your Person model. If the find returns a result, then there's a DOM element. If the situation were one where the Person record could exist in the database but I might or might not have rendered a DOM element, I'd either put a field in the database or use a session variable to track it, depending on the situation.

hth, Bill

The only way I’ve found to do IF statements in RJS is to write them in traditional JS, eg:

page << “if( $(element) ){ Element.remove(element); }”

Someone wrote a patch for RJS to include IF statements, but I don’t believe it was included. Search Trac for the code if you’re interested.

ed

Hi Moe,

Moe Zecko wrote:

the problem is the following: i know that the entry exists in the database, but i don't know if the representation of that item is currently displayed on the screen. there are 2 parts on the screen: on the left side is a search result (titles only), on the right side there is the detail view for a specific entry. if i am deleting an entry on the right side, it should also remove the representation, if it is there. since i don't know if it is there (the user might have changed the search-result via ajax-search) i have to check if that specific object exists before removing it. else there is a javascript-error popup appearing and javascript might not work for future actions. so there are 2 ways to solve this problem:

1. check if the object is existing in the left side-bar 2. run some sort of "safe-rjs-action", which does not fail if the object is not existing.

I see three ways, but to start I'd back up to your problem statement. The reason you don't know if the item is in the left side is because you're allowing the two sides to get disconnected. What does the visitor see on the right side immediately after their _first_ search? Is it blank (or some default content) until they click one of the items on the left to get details? Or is it the first item? Or something else? Whatever it is, my recommendation would be to treat every search the same way. Otherwise you're setting yourself up for a problem re: consistent-user-experience. So...

1) Every time the user does a search, change the right side to whatever default you choose.

If you insist on letting the disconnect occur...

2) Every time the user does a search, persist the results and whatever else you need to identify the DOM elements that'll be on the left side in a session variable. When the user deletes an item from the right side, in your delete_right_side method, check the persisted search results to see if the item is in there. If it is, delete the DOM element from the left.

Note that (2) still leaves you with a '?'. Independent of whether or not there's a corresponding item on the left, what's going to show up on the right side when they delete an item from there?

3) Don't use RJS for this. Like Rails, it's not intended to be a be-all-end-all. It's one tool that's very useful in the circumstances it was intended to address. This may or may not be one of those circumstances.

hth, Bill