RJS and page.select collection size

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)

What about using length? Such as:

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

Michael

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.

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

Dan,

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

Thanks

dom

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

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! :slight_smile:

Cheers,
Ilya

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