I have an RJS script that renders a partial with some javacript that runs a calendar script. Basically: var cal_#{id} = new CalendarPopup(); cal_#{id}.setReturnFunction("setMultipleCalDateValues_#{id}"); But this doesn''t seem to get executed within the browser. In fact, if I view the "generated source" in Firefox the JS portion doesn''t even show up. If I use "page << "[javascript source]"" it doesn''t work either. If I render the partial from within a view it works fine. If I call the RJS directly I can see the javascript within a try block. Can anyone help me understand what is going on? -- 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 -~----------~----~----~----~------~----~------~--~---
RJS is Ruby code that generates Javascript. So just typing out straight Javascript gets interpreted as Ruby, but if it''s just a string it gets discarded as an unused local variable. page << "javascript" is what you want. Can we see the entirety of the RJS file? Jason On 7/10/07, Ryan Belcher <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I have an RJS script that renders a partial with some javacript that > runs a calendar script. Basically: > > var cal_#{id} = new CalendarPopup(); > cal_#{id}.setReturnFunction("setMultipleCalDateValues_#{id}"); > > But this doesn''t seem to get executed within the browser. In fact, if I > view the "generated source" in Firefox the JS portion doesn''t even show > up. If I use "page << "[javascript source]"" it doesn''t work either. > If I render the partial from within a view it works fine. If I call the > RJS directly I can see the javascript within a try block. > > Can anyone help me understand what is going on? > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> RJS is Ruby code that generates Javascript. So just typing out straight > Javascript gets interpreted as Ruby, but if it''s just a string it gets > discarded as an unused local variable. > > page << "javascript" is what you want. Can we see the entirety of the > RJS > file? > > Jasonpage << "var cal_#{@entry_number} = new CalendarPopup(); cal_#{@entry_number}.setReturnFunction(\"setMultipleCalDateValues_#{@entry_number}\");" page.insert_html :before, ''total_row'',:partial => ''entry_line'', :locals => {:entry_number => @entry_number, :time_entry => @time_entry} page.replace ''add_link'', :partial => ''add_link'', :object => @entry_number page.visual_effect :highlight, ''total_row'', :duration => 3 That''s the RJS file. But like I said. Even if that javascript code is in the ''entry_line'' partial, it still doesn''t seem to get executed. -- 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 -~----------~----~----~----~------~----~------~--~---
Do you have Firebug installed? There''s probably an error in the code somewhere that''s halting execution. Jason On 7/10/07, Ryan Belcher <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Jason Roelofs wrote: > > RJS is Ruby code that generates Javascript. So just typing out straight > > Javascript gets interpreted as Ruby, but if it''s just a string it gets > > discarded as an unused local variable. > > > > page << "javascript" is what you want. Can we see the entirety of the > > RJS > > file? > > > > Jason > > page << "var cal_#{@entry_number} = new CalendarPopup(); > cal_#{@entry_number}.setReturnFunction(\"setMultipleCalDateValues_#{@entry > _number}\");" > page.insert_html :before, ''total_row'',:partial => ''entry_line'', :locals > => {:entry_number => @entry_number, :time_entry => @time_entry} > page.replace ''add_link'', :partial => ''add_link'', :object => > @entry_number > page.visual_effect :highlight, ''total_row'', :duration => 3 > > That''s the RJS file. But like I said. Even if that javascript code is > in the ''entry_line'' partial, it still doesn''t seem to get executed. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> Do you have Firebug installed? There''s probably an error in the code > somewhere that''s halting execution. > > JasonI do now :-) The console doesn''t show any errors. Is there somewhere else I should look? -- 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 -~----------~----~----~----~------~----~------~--~---
update: If I replace my javascript with "alert(\"test\");" I get the alert. So I think it''s trying to execute but is not for some reason. Since the javascript works when called as a partial within a view, could it have something to do with how it is serialized for the ajax response? -- 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 -~----------~----~----~----~------~----~------~--~---
another update: The error I''m actually getting is that cal_[id] (the one that I''m creating with ''var cal_[id] = new CalendarPopup();'') is not defined when I click on a image that references cal_[id] in it''s onClick. Is variable created, but in a different context? -- 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 -~----------~----~----~----~------~----~------~--~---
Ryan Belcher wrote:> > another update: > > The error I''m actually getting is that cal_[id]I don''t know what your general issue is, but ''cal_[id]'' is not a valid HTML id code. They can''t have a [] in them. That''s why Rails emits tags with dual identifiers; a name like ''cal_[id]'', and an id like ''cal__id''. So if you really mean an id there... -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phlip wrote:> Ryan Belcher wrote: >> >> another update: >> >> The error I''m actually getting is that cal_[id] > > I don''t know what your general issue is, but ''cal_[id]'' is not a valid > HTML id code. They can''t have a [] in them. That''s why Rails emits > tags with dual identifiers; a name like ''cal_[id]'', and an id like > ''cal__id''. > > So if you really mean an id there... > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!Yeah, I meant an id as in cal_2 or cal_3. Doing some more testing I figured out than I can alert cal_[id] in the javascript in the RJS file and it sees it as an object. So the variable is getting defined, but when I reference it in an onClick on the page it''s not defined. Which leads me to believe it''s getting defined/executed in a different context than the main window. Is that supposed to be the case? -- 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 -~----------~----~----~----~------~----~------~--~---
It''s very possible. I can''t remember what scoping Javascript does for different browsers, but your RJS code is executed as such: try { [ your code here ] } catch { alert("[your code here again]"); // in development mode } So you''re defining the variables in a try/catch block, which might be causing scoping issues. Jason On 7/11/07, Ryan Belcher <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Phlip wrote: > > Ryan Belcher wrote: > >> > >> another update: > >> > >> The error I''m actually getting is that cal_[id] > > > > I don''t know what your general issue is, but ''cal_[id]'' is not a valid > > HTML id code. They can''t have a [] in them. That''s why Rails emits > > tags with dual identifiers; a name like ''cal_[id]'', and an id like > > ''cal__id''. > > > > So if you really mean an id there... > > > > -- > > Phlip > > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! > > Yeah, I meant an id as in cal_2 or cal_3. > > Doing some more testing I figured out than I can alert cal_[id] in the > javascript in the RJS file and it sees it as an object. So the variable > is getting defined, but when I reference it in an onClick on the page > it''s not defined. Which leads me to believe it''s getting > defined/executed in a different context than the main window. > > Is that supposed to be the case? > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> It''s very possible. I can''t remember what scoping Javascript does for > different browsers, but your RJS code is executed as such: > > try { > [ your code here ] > } catch { > alert("[your code here again]"); // in development mode > } > > So you''re defining the variables in a try/catch block, which might be > causing scoping issues. > > JasonThat''s it. I was using the var keyword when declaring cal_[id]. If I remove var then I can access the variable in the rest of the page. Thanks to everyone for helping me work through this. -- 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 -~----------~----~----~----~------~----~------~--~---
Ryan Belcher wrote:> Jason Roelofs wrote: >> It''s very possible. I can''t remember what scoping Javascript does for >> different browsers, but your RJS code is executed as such: >> >> try { >> [ your code here ] >> } catch { >> alert("[your code here again]"); // in development mode >> } >> >> So you''re defining the variables in a try/catch block, which might be >> causing scoping issues. >> >> Jason > > That''s it. I was using the var keyword when declaring cal_[id]. If I > remove var then I can access the variable in the rest of the page. > > Thanks to everyone for helping me work through this.Also for anyone else interested in this problem: I was also defining a function by name "function fname_1 (){}" and I could not access this function elsewhere in the document. I needed to assign the function to a variable "fname_1 = function () {}" -- 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 -~----------~----~----~----~------~----~------~--~---
> page << "var cal_#{@entry_number} = new CalendarPopup(); > cal_#{@entry_number}.setReturnFunction(\"setMultipleCalDateValues_#{@entry_number}\");" > page.insert_html :before, ''total_row'',:partial => ''entry_line'', :locals > => {:entry_number => @entry_number, :time_entry => @time_entry} > page.replace ''add_link'', :partial => ''add_link'', :object => > @entry_number > page.visual_effect :highlight, ''total_row'', :duration => 3 > > That''s the RJS file. But like I said. Even if that javascript code is > in the ''entry_line'' partial, it still doesn''t seem to get executed.I encountered this problem before and it seems that escaping the double quotes is the problem. must be a bug in rjs or something. -- 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 -~----------~----~----~----~------~----~------~--~---