How could I check the value of an javascript object''s attribute?
<div id="new_item_details"
style="display:none"></div>
<%= link_to_function("Show details...", nil, :id =>
"show_details") do
|page|
page.visual_effect(:toggle_blind, :new_item_details)
if (page[:new_item_details].style.visibility == "none")
<----------------------------------------?????
page[:show_details].replace_html "Hide details"
else
page[:show_details].replace_html "Show details"
end
end %>
It tries to call style().visibility().== and this throws a javascript
error
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On Aug 24, 10:48 pm, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How could I check the value of an javascript object''s attribute? > <div id="new_item_details" style="display:none"></div> > > <%= link_to_function("Show details...", nil, :id => "show_details") do > |page| > page.visual_effect(:toggle_blind, :new_item_details) > > if (page[:new_item_details].style.visibility == "none") > <----------------------------------------????? > page[:show_details].replace_html "Hide details" > else > page[:show_details].replace_html "Show details" > end > > end %> >short answer: you can''t use a ruby if. you need to generate the appropriate javascript yourself. long answer: http://www.spacevatican.org/2008/5/26/conditional-rjs-explained Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eduardo Yáñez Parareda
2008-Aug-25 16:08 UTC
Re: link_to_function check javascript attribute
Thanks Frederick!, the long answer is what I needed :), very useful indeed. I also tried to write a RJS partial embedding javascript, then render it within the view, but I only got an HTML page not rendered. On 25 ago, 10:57, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8@public.gmane.orgm> > wrote: > > > How could I check the value of an javascript object''s attribute? > > <div id="new_item_details" style="display:none"></div> > > > <%= link_to_function("Show details...", nil, :id => "show_details") do > > |page| > > page.visual_effect(:toggle_blind, :new_item_details) > > > if (page[:new_item_details].style.visibility == "none") > > <----------------------------------------????? > > page[:show_details].replace_html "Hide details" > > else > > page[:show_details].replace_html "Show details" > > end > > > end %> > > short answer: you can''t use a ruby if. you need to generate the > appropriate javascript yourself. > long answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained > > Fred--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eduardo Yáñez Parareda
2008-Aug-25 16:39 UTC
Re: link_to_function check javascript attribute
In the end, what I want to do is toggle a div, and replace an element
HTML with some content when the div is hidden, and with another
content when the div is showed:
Initially, the div is hidden with style=display:none
<%= link_to_function("Show details...", nil, :id =>
"show_details") do |page|
page.visual_effect :toggle_blind, :new_item_details
if page[:new_item_details].visible
page[:show_details].replace_html "Hide details"
else
page[:show_details].replace_html "Show details"
end
end %>
so when user clicks the link, text is changed from ''Show
details'' to
''Hide details'', but I never get to change ''Hide
details'' to ''Show
details'', it seems that ''new_item_details'' div
is always visible when it isn''t.
On 25 ago, 18:08, Eduardo Yáñez Parareda
<eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Thanks Frederick!, the long answer is what I needed :), very useful
> indeed.
> I also tried to write a RJS partial embedding javascript, then render
> it within the view, but
> I only got an HTML page not rendered.
>
> On 25 ago, 10:57, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > On Aug 24, 10:48 pm, Eduardo Yáñez Parareda
<eduardo.ya...@gmail.com>
> > wrote:
>
> > > How could I check the value of an javascript object''s
attribute?
> > > <div id="new_item_details"
style="display:none"></div>
>
> > > <%= link_to_function("Show details...", nil, :id
=> "show_details") do
> > > |page|
> > > page.visual_effect(:toggle_blind, :new_item_details)
>
> > > if (page[:new_item_details].style.visibility ==
"none")
> > > <----------------------------------------?????
> > > page[:show_details].replace_html "Hide
details"
> > > else
> > > page[:show_details].replace_html "Show
details"
> > > end
>
> > > end %>
>
> > short answer: you can''t use a ruby if. you need to generate
the
> > appropriate javascript yourself.
> > long
answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained
>
> > Fred
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Eduardo Yáñez Parareda
2008-Aug-25 17:44 UTC
Re: link_to_function check javascript attribute
Well, as I realized I need to generate the javascript by myself, I''ve
written a RJS partial:
_toggle_details.rjs
page << "function toggle_details() {"
page.visual_effect :toggle_blind, ''new_item_details''
page << "if ($(''new_item_details'').style.display ==
''none'') {"
page[''show_details''].replace_html "Hide details"
page << "else"
page[''show_details''].replace_html "Show details"
page << "}"
then in my view (which is an .erb partial) I render it:
<script type="text/javascript">
<%= render :partial => ''toggle_details'', :type =>
''rjs'' %>
</script>
But I don''t get the page rendered fine, I only get the HTML as plain
text, but no errors. What am i doing wrong?
On 25 ago, 18:08, Eduardo Yáñez Parareda
<eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Thanks Frederick!, the long answer is what I needed :), very useful
> indeed.
> I also tried to write a RJS partial embedding javascript, then render
> it within the view, but
> I only got an HTML page not rendered.
>
> On 25 ago, 10:57, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > On Aug 24, 10:48 pm, Eduardo Yáñez Parareda
<eduardo.ya...@gmail.com>
> > wrote:
>
> > > How could I check the value of an javascript object''s
attribute?
> > > <div id="new_item_details"
style="display:none"></div>
>
> > > <%= link_to_function("Show details...", nil, :id
=> "show_details") do
> > > |page|
> > > page.visual_effect(:toggle_blind, :new_item_details)
>
> > > if (page[:new_item_details].style.visibility ==
"none")
> > > <----------------------------------------?????
> > > page[:show_details].replace_html "Hide
details"
> > > else
> > > page[:show_details].replace_html "Show
details"
> > > end
>
> > > end %>
>
> > short answer: you can''t use a ruby if. you need to generate
the
> > appropriate javascript yourself.
> > long
answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained
>
> > Fred
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote:> > Well, as I realized I need to generate the javascript by myself, I''ve > written a RJS partial: > > _toggle_details.rjs > > page << "function toggle_details() {" > page.visual_effect :toggle_blind, ''new_item_details'' > page << "if ($(''new_item_details'').style.display == ''none'') {" > page[''show_details''].replace_html "Hide details" > page << "else" > page[''show_details''].replace_html "Show details" > page << "}" > > then in my view (which is an .erb partial) I render it: > > <script type="text/javascript"> > <%= render :partial => ''toggle_details'', :type => ''rjs'' %> > </script> > > But I don''t get the page rendered fine, I only get the HTML as plain > text, but no errors. What am i doing wrong?You''re creating syntactically invalid javascript: the curly brace you''re opening on the if line is never closed. Fred> > > On 25 ago, 18:08, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> Thanks Frederick!, the long answer is what I needed :), very useful >> indeed. >> I also tried to write a RJS partial embedding javascript, then render >> it within the view, but >> I only got an HTML page not rendered. >> >> On 25 ago, 10:57, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >> >>> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda >>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>> wrote: >> >>>> How could I check the value of an javascript object''s attribute? >>>> <div id="new_item_details" style="display:none"></div> >> >>>> <%= link_to_function("Show details...", nil, :id => >>>> "show_details") do >>>> |page| >>>> page.visual_effect(:toggle_blind, :new_item_details) >> >>>> if (page[:new_item_details].style.visibility == "none") >>>> <----------------------------------------????? >>>> page[:show_details].replace_html "Hide details" >>>> else >>>> page[:show_details].replace_html "Show details" >>>> end >> >>>> end %> >> >>> short answer: you can''t use a ruby if. you need to generate the >>> appropriate javascript yourself. >>> long answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained >> >>> Fred > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eduardo Yáñez Parareda
2008-Aug-25 20:39 UTC
Re: link_to_function check javascript attribute
Thanks again, but that''s not the problem, because if I fix the syntax
error or leave the partial RJS file empty.
My fixed partial is:
page << "function toggle_details() {"
page.visual_effect :toggle_blind, ''new_item_details''
page << "if ($(''new_item_details'').style.display ==
''none'') {"
page[''show_details''].replace_html "Hide details"
page << "} else {"
page[''show_details''].replace_html "Show details"
page << "}"
page << "}"
but the page is not rendered fine. So I''ve chosen an easy solution,
I''ve written the function into application.js as javascript, and all
works fine now.
Thank you Frederick for your help!
On 25 ago, 19:51, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote:
>
>
>
>
>
> > Well, as I realized I need to generate the javascript by myself,
I''ve
> > written a RJS partial:
>
> > _toggle_details.rjs
>
> > page << "function toggle_details() {"
> > page.visual_effect :toggle_blind, ''new_item_details''
> > page << "if
($(''new_item_details'').style.display ==
''none'') {"
> > page[''show_details''].replace_html "Hide
details"
> > page << "else"
> > page[''show_details''].replace_html "Show
details"
> > page << "}"
>
> > then in my view (which is an .erb partial) I render it:
>
> > <script type="text/javascript">
> > <%= render :partial => ''toggle_details'',
:type => ''rjs'' %>
> > </script>
>
> > But I don''t get the page rendered fine, I only get the HTML
as plain
> > text, but no errors. What am i doing wrong?
>
> You''re creating syntactically invalid javascript: the curly brace
> you''re opening on the if line is never closed.
>
> Fred
>
>
>
> > On 25 ago, 18:08, Eduardo Yáñez Parareda
<eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > wrote:
> >> Thanks Frederick!, the long answer is what I needed :), very
useful
> >> indeed.
> >> I also tried to write a RJS partial embedding javascript, then
render
> >> it within the view, but
> >> I only got an HTML page not rendered.
>
> >> On 25 ago, 10:57, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> >> wrote:
>
> >>> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda
> >>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> >>> wrote:
>
> >>>> How could I check the value of an javascript
object''s attribute?
> >>>> <div id="new_item_details"
style="display:none"></div>
>
> >>>> <%= link_to_function("Show details...", nil,
:id =>
> >>>> "show_details") do
> >>>> |page|
> >>>> page.visual_effect(:toggle_blind,
:new_item_details)
>
> >>>> if (page[:new_item_details].style.visibility ==
"none")
> >>>> <----------------------------------------?????
> >>>> page[:show_details].replace_html "Hide
details"
> >>>> else
> >>>> page[:show_details].replace_html "Show
details"
> >>>> end
>
> >>>> end %>
>
> >>> short answer: you can''t use a ruby if. you need to
generate the
> >>> appropriate javascript yourself.
> >>> long
answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained
>
> >>> Fred
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 25 Aug 2008, at 21:39, Eduardo Yáñez Parareda wrote:> > Thanks again, but that''s not the problem, because if I fix the syntax > error or leave the partial RJS file empty. > My fixed partial is: > > page << "function toggle_details() {" > page.visual_effect :toggle_blind, ''new_item_details'' > page << "if ($(''new_item_details'').style.display == ''none'') {" > page[''show_details''].replace_html "Hide details" > page << "} else {" > page[''show_details''].replace_html "Show details" > page << "}" > page << "}" > > but the page is not rendered fine. So I''ve chosen an easy solution, > I''ve written the function into application.js as javascript, and all > works fine now.Hard to say without seeing what was inserted into the page, but if the javascript that is created included " (which it probably does) then crazy stuff would happen. Wrapping the render :partial in a javascript_tag would probably have helped. Fred> > Thank you Frederick for your help! > > On 25 ago, 19:51, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote: >> >> >> >> >> >>> Well, as I realized I need to generate the javascript by myself, >>> I''ve >>> written a RJS partial: >> >>> _toggle_details.rjs >> >>> page << "function toggle_details() {" >>> page.visual_effect :toggle_blind, ''new_item_details'' >>> page << "if ($(''new_item_details'').style.display == ''none'') {" >>> page[''show_details''].replace_html "Hide details" >>> page << "else" >>> page[''show_details''].replace_html "Show details" >>> page << "}" >> >>> then in my view (which is an .erb partial) I render it: >> >>> <script type="text/javascript"> >>> <%= render :partial => ''toggle_details'', :type => ''rjs'' %> >>> </script> >> >>> But I don''t get the page rendered fine, I only get the HTML as plain >>> text, but no errors. What am i doing wrong? >> >> You''re creating syntactically invalid javascript: the curly brace >> you''re opening on the if line is never closed. >> >> Fred >> >> >> >>> On 25 ago, 18:08, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>> wrote: >>>> Thanks Frederick!, the long answer is what I needed :), very useful >>>> indeed. >>>> I also tried to write a RJS partial embedding javascript, then >>>> render >>>> it within the view, but >>>> I only got an HTML page not rendered. >> >>>> On 25 ago, 10:57, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>> wrote: >> >>>>> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda >>>>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>> wrote: >> >>>>>> How could I check the value of an javascript object''s attribute? >>>>>> <div id="new_item_details" style="display:none"></div> >> >>>>>> <%= link_to_function("Show details...", nil, :id => >>>>>> "show_details") do >>>>>> |page| >>>>>> page.visual_effect(:toggle_blind, :new_item_details) >> >>>>>> if (page[:new_item_details].style.visibility == "none") >>>>>> <----------------------------------------????? >>>>>> page[:show_details].replace_html "Hide details" >>>>>> else >>>>>> page[:show_details].replace_html "Show details" >>>>>> end >> >>>>>> end %> >> >>>>> short answer: you can''t use a ruby if. you need to generate the >>>>> appropriate javascript yourself. >>>>> long answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained >> >>>>> Fred > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eduardo Yáñez Parareda
2008-Aug-25 21:57 UTC
Re: link_to_function check javascript attribute
Yes, may be that double quotes broke the page. Anyway it''s more clear to have javascript in a javascript file :), but I was trying to do it with RJS because I''m not used to use it. On 25 ago, 22:52, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 25 Aug 2008, at 21:39, Eduardo Yáñez Parareda wrote: > > > > > > > Thanks again, but that''s not the problem, because if I fix the syntax > > error or leave the partial RJS file empty. > > My fixed partial is: > > > page << "function toggle_details() {" > > page.visual_effect :toggle_blind, ''new_item_details'' > > page << "if ($(''new_item_details'').style.display == ''none'') {" > > page[''show_details''].replace_html "Hide details" > > page << "} else {" > > page[''show_details''].replace_html "Show details" > > page << "}" > > page << "}" > > > but the page is not rendered fine. So I''ve chosen an easy solution, > > I''ve written the function into application.js as javascript, and all > > works fine now. > > Hard to say without seeing what was inserted into the page, but if the > javascript that is created included " (which it probably does) then > crazy stuff would happen. Wrapping the render :partial in a > javascript_tag would probably have helped. > > Fred > > > > > Thank you Frederick for your help! > > > On 25 ago, 19:51, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote: > > >>> Well, as I realized I need to generate the javascript by myself, > >>> I''ve > >>> written a RJS partial: > > >>> _toggle_details.rjs > > >>> page << "function toggle_details() {" > >>> page.visual_effect :toggle_blind, ''new_item_details'' > >>> page << "if ($(''new_item_details'').style.display == ''none'') {" > >>> page[''show_details''].replace_html "Hide details" > >>> page << "else" > >>> page[''show_details''].replace_html "Show details" > >>> page << "}" > > >>> then in my view (which is an .erb partial) I render it: > > >>> <script type="text/javascript"> > >>> <%= render :partial => ''toggle_details'', :type => ''rjs'' %> > >>> </script> > > >>> But I don''t get the page rendered fine, I only get the HTML as plain > >>> text, but no errors. What am i doing wrong? > > >> You''re creating syntactically invalid javascript: the curly brace > >> you''re opening on the if line is never closed. > > >> Fred > > >>> On 25 ago, 18:08, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>> wrote: > >>>> Thanks Frederick!, the long answer is what I needed :), very useful > >>>> indeed. > >>>> I also tried to write a RJS partial embedding javascript, then > >>>> render > >>>> it within the view, but > >>>> I only got an HTML page not rendered. > > >>>> On 25 ago, 10:57, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>>> wrote: > > >>>>> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda > >>>>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>>>> wrote: > > >>>>>> How could I check the value of an javascript object''s attribute? > >>>>>> <div id="new_item_details" style="display:none"></div> > > >>>>>> <%= link_to_function("Show details...", nil, :id => > >>>>>> "show_details") do > >>>>>> |page| > >>>>>> page.visual_effect(:toggle_blind, :new_item_details) > > >>>>>> if (page[:new_item_details].style.visibility == "none") > >>>>>> <----------------------------------------????? > >>>>>> page[:show_details].replace_html "Hide details" > >>>>>> else > >>>>>> page[:show_details].replace_html "Show details" > >>>>>> end > > >>>>>> end %> > > >>>>> short answer: you can''t use a ruby if. you need to generate the > >>>>> appropriate javascript yourself. > >>>>> long answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained > > >>>>> Fred--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eduardo Yáñez Parareda
2008-Aug-25 22:13 UTC
Re: link_to_function check javascript attribute
Although it already works fine into application.js, I''ve tried one
last attempt with RJS. My partial now is:
page << ''function toggle_details() {''
page.visual_effect :toggle_blind, ''new_item_details''
page << "if ($(''new_item_details'').style.display ==
''none'') {"
page[''show_details''].replace_html ''Hide
details''
page << ''} else {''
page[''show_details''].replace_html ''Show
details''
page << ''} }''
And the generated code is:
<script type=''text/javascript''>
try {
function toggle_details() {
Effect.toggle("new_item_details",''blind'',{});
if ($(''new_item_details'').style.display ==
''none'') {
$("show_details").update("Hide details");
} else {
$("show_details").update("Show details");
}
}
} catch (e) {
alert(''RJS error:\n\n'' + e.toString());
alert(''function toggle_details() {\n
Effect.toggle(\"new_item_details\",\''blind\'',{});\n
if ($(\''new_item_details\'').style.display ==
\''none\'') {\n
$(\"show_details\").update(\"Hide details\");\n
} else {\n
$(\"show_details\").update(\"Show details\");\n
}
}'');
throw e }
</script>
but still doesn''t render fine. Moreover, if I leave the partial empty
(nothing into the RJS), it doesn''t work!!!
On 25 ago, 23:57, Eduardo Yáñez Parareda
<eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Yes, may be that double quotes broke the page. Anyway it''s more
clear
> to have javascript in a javascript file :), but I was trying to do it
> with RJS because
> I''m not used to use it.
>
> On 25 ago, 22:52, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > On 25 Aug 2008, at 21:39, Eduardo Yáñez Parareda wrote:
>
> > > Thanks again, but that''s not the problem, because if I
fix the syntax
> > > error or leave the partial RJS file empty.
> > > My fixed partial is:
>
> > > page << "function toggle_details() {"
> > > page.visual_effect :toggle_blind,
''new_item_details''
> > > page << "if
($(''new_item_details'').style.display ==
''none'') {"
> > > page[''show_details''].replace_html "Hide
details"
> > > page << "} else {"
> > > page[''show_details''].replace_html "Show
details"
> > > page << "}"
> > > page << "}"
>
> > > but the page is not rendered fine. So I''ve chosen an
easy solution,
> > > I''ve written the function into application.js as
javascript, and all
> > > works fine now.
>
> > Hard to say without seeing what was inserted into the page, but if the
> > javascript that is created included " (which it probably does)
then
> > crazy stuff would happen. Wrapping the render :partial in a
> > javascript_tag would probably have helped.
>
> > Fred
>
> > > Thank you Frederick for your help!
>
> > > On 25 ago, 19:51, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > >> On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote:
>
> > >>> Well, as I realized I need to generate the javascript by
myself,
> > >>> I''ve
> > >>> written a RJS partial:
>
> > >>> _toggle_details.rjs
>
> > >>> page << "function toggle_details() {"
> > >>> page.visual_effect :toggle_blind,
''new_item_details''
> > >>> page << "if
($(''new_item_details'').style.display ==
''none'') {"
> > >>> page[''show_details''].replace_html
"Hide details"
> > >>> page << "else"
> > >>> page[''show_details''].replace_html
"Show details"
> > >>> page << "}"
>
> > >>> then in my view (which is an .erb partial) I render it:
>
> > >>> <script type="text/javascript">
> > >>> <%= render :partial =>
''toggle_details'', :type => ''rjs'' %>
> > >>> </script>
>
> > >>> But I don''t get the page rendered fine, I only
get the HTML as plain
> > >>> text, but no errors. What am i doing wrong?
>
> > >> You''re creating syntactically invalid javascript:
the curly brace
> > >> you''re opening on the if line is never closed.
>
> > >> Fred
>
> > >>> On 25 ago, 18:08, Eduardo Yáñez Parareda
<eduardo.ya...@gmail.com>
> > >>> wrote:
> > >>>> Thanks Frederick!, the long answer is what I needed
:), very useful
> > >>>> indeed.
> > >>>> I also tried to write a RJS partial embedding
javascript, then
> > >>>> render
> > >>>> it within the view, but
> > >>>> I only got an HTML page not rendered.
>
> > >>>> On 25 ago, 10:57, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > >>>> wrote:
>
> > >>>>> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda
> > >>>>>
<eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > >>>>> wrote:
>
> > >>>>>> How could I check the value of an javascript
object''s attribute?
> > >>>>>> <div id="new_item_details"
style="display:none"></div>
>
> > >>>>>> <%= link_to_function("Show
details...", nil, :id =>
> > >>>>>> "show_details") do
> > >>>>>> |page|
> > >>>>>> page.visual_effect(:toggle_blind,
:new_item_details)
>
> > >>>>>> if
(page[:new_item_details].style.visibility == "none")
> > >>>>>>
<----------------------------------------?????
> > >>>>>> page[:show_details].replace_html
"Hide details"
> > >>>>>> else
> > >>>>>> page[:show_details].replace_html
"Show details"
> > >>>>>> end
>
> > >>>>>> end %>
>
> > >>>>> short answer: you can''t use a ruby if.
you need to generate the
> > >>>>> appropriate javascript yourself.
> > >>>>> long
answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained
>
> > >>>>> Fred
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 25 Aug 2008, at 23:13, Eduardo Yáñez Parareda wrote:> > Although it already works fine into application.js, I''ve tried one > last attempt with RJS. My partial now is: > > page << ''function toggle_details() {'' > page.visual_effect :toggle_blind, ''new_item_details'' > page << "if ($(''new_item_details'').style.display == ''none'') {" > page[''show_details''].replace_html ''Hide details'' > page << ''} else {'' > page[''show_details''].replace_html ''Show details'' > page << ''} }'' > > And the generated code is: > > <script type=''text/javascript''> > try { > function toggle_details() { > Effect.toggle("new_item_details",''blind'',{}); > if ($(''new_item_details'').style.display == ''none'') { > $("show_details").update("Hide details"); > } else { > $("show_details").update("Show details"); > } > } > } catch (e) { > alert(''RJS error:\n\n'' + e.toString()); > alert(''function toggle_details() {\n > Effect.toggle(\"new_item_details\",\''blind\'',{});\n > if ($(\''new_item_details\'').style.display == \''none\'') {\n > $(\"show_details\").update(\"Hide details\");\n > } else {\n > $(\"show_details\").update(\"Show details\");\n > } > }''); > throw e } > </script> > > but still doesn''t render fine. Moreover, if I leave the partial empty > (nothing into the RJS), it doesn''t work!!! >because of the try/catch stuff that''s still there even if the partial is empty. The javascript_tag helper will wrap this in a CDATA section which should make everything happy. Fred> > On 25 ago, 23:57, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> Yes, may be that double quotes broke the page. Anyway it''s more clear >> to have javascript in a javascript file :), but I was trying to do it >> with RJS because >> I''m not used to use it. >> >> On 25 ago, 22:52, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >> >>> On 25 Aug 2008, at 21:39, Eduardo Yáñez Parareda wrote: >> >>>> Thanks again, but that''s not the problem, because if I fix the >>>> syntax >>>> error or leave the partial RJS file empty. >>>> My fixed partial is: >> >>>> page << "function toggle_details() {" >>>> page.visual_effect :toggle_blind, ''new_item_details'' >>>> page << "if ($(''new_item_details'').style.display == ''none'') {" >>>> page[''show_details''].replace_html "Hide details" >>>> page << "} else {" >>>> page[''show_details''].replace_html "Show details" >>>> page << "}" >>>> page << "}" >> >>>> but the page is not rendered fine. So I''ve chosen an easy solution, >>>> I''ve written the function into application.js as javascript, and >>>> all >>>> works fine now. >> >>> Hard to say without seeing what was inserted into the page, but if >>> the >>> javascript that is created included " (which it probably does) then >>> crazy stuff would happen. Wrapping the render :partial in a >>> javascript_tag would probably have helped. >> >>> Fred >> >>>> Thank you Frederick for your help! >> >>>> On 25 ago, 19:51, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>> wrote: >>>>> On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote: >> >>>>>> Well, as I realized I need to generate the javascript by myself, >>>>>> I''ve >>>>>> written a RJS partial: >> >>>>>> _toggle_details.rjs >> >>>>>> page << "function toggle_details() {" >>>>>> page.visual_effect :toggle_blind, ''new_item_details'' >>>>>> page << "if ($(''new_item_details'').style.display == ''none'') {" >>>>>> page[''show_details''].replace_html "Hide details" >>>>>> page << "else" >>>>>> page[''show_details''].replace_html "Show details" >>>>>> page << "}" >> >>>>>> then in my view (which is an .erb partial) I render it: >> >>>>>> <script type="text/javascript"> >>>>>> <%= render :partial => ''toggle_details'', :type => ''rjs'' %> >>>>>> </script> >> >>>>>> But I don''t get the page rendered fine, I only get the HTML as >>>>>> plain >>>>>> text, but no errors. What am i doing wrong? >> >>>>> You''re creating syntactically invalid javascript: the curly brace >>>>> you''re opening on the if line is never closed. >> >>>>> Fred >> >>>>>> On 25 ago, 18:08, Eduardo Yáñez Parareda >>>>>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>>> wrote: >>>>>>> Thanks Frederick!, the long answer is what I needed :), very >>>>>>> useful >>>>>>> indeed. >>>>>>> I also tried to write a RJS partial embedding javascript, then >>>>>>> render >>>>>>> it within the view, but >>>>>>> I only got an HTML page not rendered. >> >>>>>>> On 25 ago, 10:57, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>>>> wrote: >> >>>>>>>> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda >>>>>>>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>>>>> wrote: >> >>>>>>>>> How could I check the value of an javascript object''s >>>>>>>>> attribute? >>>>>>>>> <div id="new_item_details" style="display:none"></div> >> >>>>>>>>> <%= link_to_function("Show details...", nil, :id => >>>>>>>>> "show_details") do >>>>>>>>> |page| >>>>>>>>> page.visual_effect(:toggle_blind, :new_item_details) >> >>>>>>>>> if (page[:new_item_details].style.visibility == >>>>>>>>> "none") >>>>>>>>> <----------------------------------------????? >>>>>>>>> page[:show_details].replace_html "Hide details" >>>>>>>>> else >>>>>>>>> page[:show_details].replace_html "Show details" >>>>>>>>> end >> >>>>>>>>> end %> >> >>>>>>>> short answer: you can''t use a ruby if. you need to generate the >>>>>>>> appropriate javascript yourself. >>>>>>>> long answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained >> >>>>>>>> Fred > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 25 Aug 2008, at 23:13, Eduardo Yáñez Parareda wrote:> > Although it already works fine into application.js, I''ve tried one > last attempt with RJS. My partial now is: >Forgot to say - I think this is absolutely the right way to go - there''s no point trying to shoehorn stuff into rjs which is more easily written as just plain javascript. But it''s always good to understand why something didn''t work. Fred> On 25 ago, 23:57, Eduardo Yáñez Parareda <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> Yes, may be that double quotes broke the page. Anyway it''s more clear >> to have javascript in a javascript file :), but I was trying to do it >> with RJS because >> I''m not used to use it. >> >> On 25 ago, 22:52, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >> >>> On 25 Aug 2008, at 21:39, Eduardo Yáñez Parareda wrote: >> >>>> Thanks again, but that''s not the problem, because if I fix the >>>> syntax >>>> error or leave the partial RJS file empty. >>>> My fixed partial is: >> >>>> page << "function toggle_details() {" >>>> page.visual_effect :toggle_blind, ''new_item_details'' >>>> page << "if ($(''new_item_details'').style.display == ''none'') {" >>>> page[''show_details''].replace_html "Hide details" >>>> page << "} else {" >>>> page[''show_details''].replace_html "Show details" >>>> page << "}" >>>> page << "}" >> >>>> but the page is not rendered fine. So I''ve chosen an easy solution, >>>> I''ve written the function into application.js as javascript, and >>>> all >>>> works fine now. >> >>> Hard to say without seeing what was inserted into the page, but if >>> the >>> javascript that is created included " (which it probably does) then >>> crazy stuff would happen. Wrapping the render :partial in a >>> javascript_tag would probably have helped. >> >>> Fred >> >>>> Thank you Frederick for your help! >> >>>> On 25 ago, 19:51, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>> wrote: >>>>> On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote: >> >>>>>> Well, as I realized I need to generate the javascript by myself, >>>>>> I''ve >>>>>> written a RJS partial: >> >>>>>> _toggle_details.rjs >> >>>>>> page << "function toggle_details() {" >>>>>> page.visual_effect :toggle_blind, ''new_item_details'' >>>>>> page << "if ($(''new_item_details'').style.display == ''none'') {" >>>>>> page[''show_details''].replace_html "Hide details" >>>>>> page << "else" >>>>>> page[''show_details''].replace_html "Show details" >>>>>> page << "}" >> >>>>>> then in my view (which is an .erb partial) I render it: >> >>>>>> <script type="text/javascript"> >>>>>> <%= render :partial => ''toggle_details'', :type => ''rjs'' %> >>>>>> </script> >> >>>>>> But I don''t get the page rendered fine, I only get the HTML as >>>>>> plain >>>>>> text, but no errors. What am i doing wrong? >> >>>>> You''re creating syntactically invalid javascript: the curly brace >>>>> you''re opening on the if line is never closed. >> >>>>> Fred >> >>>>>> On 25 ago, 18:08, Eduardo Yáñez Parareda >>>>>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>>> wrote: >>>>>>> Thanks Frederick!, the long answer is what I needed :), very >>>>>>> useful >>>>>>> indeed. >>>>>>> I also tried to write a RJS partial embedding javascript, then >>>>>>> render >>>>>>> it within the view, but >>>>>>> I only got an HTML page not rendered. >> >>>>>>> On 25 ago, 10:57, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>>>> wrote: >> >>>>>>>> On Aug 24, 10:48 pm, Eduardo Yáñez Parareda >>>>>>>> <eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>>>>> wrote: >> >>>>>>>>> How could I check the value of an javascript object''s >>>>>>>>> attribute? >>>>>>>>> <div id="new_item_details" style="display:none"></div> >> >>>>>>>>> <%= link_to_function("Show details...", nil, :id => >>>>>>>>> "show_details") do >>>>>>>>> |page| >>>>>>>>> page.visual_effect(:toggle_blind, :new_item_details) >> >>>>>>>>> if (page[:new_item_details].style.visibility == >>>>>>>>> "none") >>>>>>>>> <----------------------------------------????? >>>>>>>>> page[:show_details].replace_html "Hide details" >>>>>>>>> else >>>>>>>>> page[:show_details].replace_html "Show details" >>>>>>>>> end >> >>>>>>>>> end %> >> >>>>>>>> short answer: you can''t use a ruby if. you need to generate the >>>>>>>> appropriate javascript yourself. >>>>>>>> long answer:http://www.spacevatican.org/2008/5/26/conditional-rjs-explained >> >>>>>>>> Fred > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---