# RJS and page.select collection size

**URL:** <https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374>\
**Category:** Rails\
**Created:** [May 12, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374 "2006-05-12T20:02:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 12, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/1 "2006-05-12T20:02:53Z")

</div>

I need to implement a conditional in my RJS template which looks  
something like:

if (page.select(‘row1’).first != null)  
page \<\< “new TableRow.MoveAfter(‘row1’, ‘newrow’);”  
else  
page \<\< “new TableRow.MoveAfter(‘row2’, ‘newrow’);”  
end

Now, dumb question… My ‘if’ doesnt work, how should I properly check if  
the returned collection found row1 or not? (Note: row1 is unique)

---

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 12, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/2 "2006-05-12T20:34:01Z")

</div>

What about using length? Such as:

if page.select(‘row1’).length == 0  
// etc…

Michael

---

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 12, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/3 "2006-05-12T20:52:59Z")

</div>

Nope, that converts it to:  
$$(“step-update-36-row”).length()

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 page.select clause.

---

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 12, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/4 "2006-05-12T21:19:52Z")

</div>

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 page.select?

Thanks.  
Ilya

---

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 12, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/5 "2006-05-12T22:24:42Z")

</div>

Dan,

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

Thanks

dom

---

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 12, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/6 "2006-05-12T21:44:52Z")

</div>

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  
page.alert("sdsd’)  
end

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

Dan

---

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 12, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/7 "2006-05-12T22:53:47Z")

</div>

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! 🙂

Cheers,  
Ilya

---

<div class="post-metadata">

**Author:** ![igrigorik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@igrigorik](https://www.ruby-forum.com/u/igrigorik)\
**Post date:** [May 13, 2006, 1:14am UTC](https://www.ruby-forum.com/t/rjs-and-page-select-collection-size/60374/8 "2006-05-13T01:14:34Z")

</div>

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  
stuff:

page.onready do

# execute some RJS as soon as the DOM is ready (executes before

onload)  
end

page.select(“input.thing”, :as =\> :field) do |field|  
field.onkeypress do |event|  
field.disable  
page.remote :update =\> ‘item’  
event.stop  
end  
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.

end

page.collection([1,2,3,4,5,6]).each do |item|  
page.alert(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.

Cheers,

Dan
