Forum: Ruby on Rails RJS and collection size

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ilya G. (Guest)
on 2006-05-12 22:02
I need to implement a conditional in my RJS template which looks
something like:

   if ('row1').first != null)
  	page << "new TableRow.MoveAfter('row1', 'newrow');"
  	page << "new TableRow.MoveAfter('row2', 'newrow');"

Now, dumb question.. My 'if' doesnt work, how should I properly check if
the returned collection found row1 or not? (Note: row1 is unique)
Michael T. (Guest)
on 2006-05-12 22:34
(Received via mailing list)
What about using length?  Such as:

if'row1').length == 0
// etc..

Ilya G. (Guest)
on 2006-05-12 22:52
Nope, that converts it to:

And complains that no such method exists.

If I do raw javascript, it seems to work, ex:
   page << "var d = $(\"#{element_row_id(@chk_edit)}\"); alert(d);"

With that I catch the row when it's present and 'null' when it's not
there. But I'm still trying to figure out how to either incorporate that
or fix the clause.
Ilya G. (Guest)
on 2006-05-12 23:19
Alright, I got it to work with raw JavaScript.. using one long ugly line
of code:

page << "if (document.getElementById('#{row1')) { new
TableRow.MoveAfter('row1', 'newrow'); } else { new
TableRow.MoveAfter('row2', 'newrow'); }"

But I'm still curious, is there a nicer way to do this with

Dan W. (Guest)
on 2006-05-12 23:44
(Received via mailing list)
Nope there isn't at the moment.  I've been thinking about adding some
kind of support for conditions in my unobtrusive javascript plugin
(which isn't released yet).  I was thinking something like:

page.if('thing > 4') do

because using a real ruby if statement is not possible.  What do you

Dominic S. (Guest)
on 2006-05-13 00:24
(Received via mailing list)

Sounds really good.  Do you have a timeline or a subversion repository
browsing on this plugin?


Ilya G. (Guest)
on 2006-05-13 00:53
I second that, it would be nice to have the ability to add control logic
based on javascript eval's.

Waiting for your plugin now! :)

Dan W. (Guest)
on 2006-05-13 03:14
(Received via mailing list)
It very close (I've been saying that for about 2 months now) but there
are a few show stoppers to sort out with it.  Also, I'm trying to
refine it by putting it into production use at the moment.  The main
purpose of the plugin is to enable unobtrusive style scripting (where
no JavaScript is put into the body of the html) but I've ended
implementing almost a total rewrite of the JavaScript Generator and
proxy classes to get it done.  Here's a preview of some of the new

page.onready do
   # execute some RJS as soon as the DOM is ready (executes before
end"input.thing", :as => :field) do |field|
   field.onkeypress do |event|
       page.remote :update => 'item'
end # get inputs as fields (so you can call Field methods and assign
an event handler with RJS

page.function :func_name, 'arg1', 'arg2' do |arg1, arg2|
   arg1.thing = arg2
   # create functions which can be assigned to event handlers etc.

page.collection([1,2,3,4,5,6]).each do |item|
end # iterate over collections

and yes, finally some kind of condition statement but im still
puzzling over the API for that.

It's definately not a replacement for javascript but should give you a
little more power and also gives you the flexibility to use other
library code.

Let me know what you think...and you can expect to see the plugin over
the next 2 or 3 weeks.  I'm definately going to have something to show
at RailsConf.


This topic is locked and can not be replied to.