Moe Zecko
2007-Mar-13 12:17 UTC
RJS, Prototype and finding out if object exists in the DOM
i asked this allready in german on rubyforum.com and also on
#rubyonrails (thanks again for your time, njero)
http://forum.ruby-portal.de/viewtopic.php?t=4067
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.
generally, objects can be fetched via getElementById, which is the same
as $(''elementid'') in prototype. but this seems to be useless,
since it
is always creating an ActionView::Helpers::JavaScriptVariableProxy - so
i cannot check if the object _exists_
here is the two ways i tried
1. RJS and Prototype
# the rjs
thename = ''person''+params[''id''].to_s
if page[thename] then
p "YES"
else
p "NO"
end
# this is in the html
<div id="person105" class="searchlist">
<div class="searchlist_img" style="float: left;">
<img src="/images/icons/person_f.png">
</div>
2. Custom Javascript
thename = ''person''+params[''id''].to_s
page << ("function existing(invar){ if
(document.getElementById(invar)
!= null) {return true; }else {return false;} }")
r=page.call :existing,thename
if r then p "yes"
else p "no"
end
any more ideas how to solve that problem? 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?
thanks!
zecko
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Mar-13 13:37 UTC
Re: 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 ;-) 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ed Hickey
2007-Mar-13 13:42 UTC
Re: RJS, Prototype and finding out if object exists in the DOM
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
On 3/13/07, Bill Walton
<bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org>
wrote:>
>
> 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
;-) 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
>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Moe Zecko
2007-Mar-13 14:28 UTC
Re: RJS, Prototype and finding out if object exists in the D
Hi Bill,> 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.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. for both ways i did not succeed in finding any answers. thanks for any hints, mz. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Mar-13 15:36 UTC
Re: RJS, Prototype and finding out if object exists in the D
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---