I am passing a ruby array @myArray to a javascript function via rjs page << "{ $ (''my_calendar'').calendar_date_select.classForMatrix(''#{@calendar_matrix}'');}" the js function is the following : classForMatrix: function(calendar_matrix) { for (var i = 0; i<42; i++) { cell = this.calendar_day_grid[i]; console.log(calendar_matrix(i)); if (calendar_matrix(i) == 1 ) {cell.addClassName("closed");} } }, and I get the following js error TypeError: calendar_matrix is not a function where am I wrong ? thanks for your lights erwin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Aug-27 14:44 UTC
Re: How to pass a Ruby Array to a javascript function ?
On 27 Aug 2008, at 15:18, Erwin wrote:> > I am passing a ruby array @myArray to a javascript function via rjs > > page << "{ $ > (''my_calendar > '').calendar_date_select.classForMatrix(''#{@calendar_matrix}'');}" > > the js function is the following : > > classForMatrix: function(calendar_matrix) { > for (var i = 0; i<42; i++) { > cell = this.calendar_day_grid[i]; > console.log(calendar_matrix(i)); > if (calendar_matrix(i) == 1 ) {cell.addClassName("closed");} > } > }, > > and I get the following js error > TypeError: calendar_matrix is not a function > > where am I wrong ?if calendar_matrix is an array you need calendar_matrix[i] not calendar_matrix(i). On top of that you probably need to make it look like an array (ie enclose it in [], separate values with commas etc...). The array_or_string_for_javascript looks like it will do that for you. Fred> > > thanks for your lights > > erwin > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
thanks a lot I realize it using the @calendar_matrix.to_json send correctly the array and that''s it.... On 27 août, 16:44, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 27 Aug 2008, at 15:18,Erwinwrote: > > > > > > > I am passing a ruby array @myArray to a javascript function via rjs > > > page << "{ $ > > (''my_calendar > > '').calendar_date_select.classForMatrix(''#{@calendar_matrix}'');}" > > > the js function is the following : > > > classForMatrix: function(calendar_matrix) { > > for (var i = 0; i<42; i++) { > > cell = this.calendar_day_grid[i]; > > console.log(calendar_matrix(i)); > > if (calendar_matrix(i) == 1 ) {cell.addClassName("closed");} > > } > > }, > > > and I get the following js error > > TypeError: calendar_matrix is not a function > > > where am I wrong ? > > if calendar_matrix is an array you need calendar_matrix[i] not > calendar_matrix(i). > On top of that you probably need to make it look like an array (ie > enclose it in [], separate values with commas etc...). The > array_or_string_for_javascript looks like it will do that for you. > > Fred > > > > > thanks for your lights > > >erwin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---