Forum: Ruby on Rails RJS and page.select collection size

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Baa493762803518bf4adfbeb75d98e76?d=identicon&s=25 Ilya Grigorik (igrigorik)
on 2006-05-12 20:02
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)
455ac2a64d06dc8461f4d258d7f7e980?d=identicon&s=25 Michael Trier (Guest)
on 2006-05-12 20:34
(Received via mailing list)
What about using length?  Such as:

if page.select('row1').length == 0
// etc..

Michael
Baa493762803518bf4adfbeb75d98e76?d=identicon&s=25 Ilya Grigorik (igrigorik)
on 2006-05-12 20:52
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.
Baa493762803518bf4adfbeb75d98e76?d=identicon&s=25 Ilya Grigorik (igrigorik)
on 2006-05-12 21: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 page.select?

Thanks.
Ilya
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-05-12 21: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
  page.alert("sdsd')
end

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

Dan
6ff7cc79a09836e7930763a9d8b6ba9a?d=identicon&s=25 Dominic Sisneros (Guest)
on 2006-05-12 22:24
(Received via mailing list)
Dan,

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

Thanks

dom
Baa493762803518bf4adfbeb75d98e76?d=identicon&s=25 Ilya Grigorik (igrigorik)
on 2006-05-12 22: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! :)

Cheers,
Ilya
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-05-13 01: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
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
This topic is locked and can not be replied to.