Forum: Ruby on Rails Capturing arbitrary events in client-side DOM

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.
2c8df79b2a68a7532af01d8851f7fa2d?d=identicon&s=25 drjflam (Guest)
on 2005-11-18 15:27
(Received via mailing list)
I was wondering if there was any support a'la observe_field for
capturing
arbitrary events and triggering an AJAX postback.

For example, let's consider this chunk of HTML:

<div onclick="handle_any_click_on_child_element">
<div id="div_button1">Button 1</div>
<div id="div_button2">Button 2</div>
<div id="div_button3">Button 3</div>
</div>

...

function handle_any_click_on_child_element() {
element = get_element_that_was_clicked_on();
ajax_post_back(element.id <http://element.id>);
}

Thanks,
-John
http://www.iunknown.com
A4b45b0467e28925b5103b50f7d39e5d?d=identicon&s=25 beattie.mark (Guest)
on 2005-11-18 16:31
(Received via mailing list)
On Friday 18 November 2005 10:58 pm, John Lam wrote:
> I was wondering if there was any support a'la observe_field for capturing
> arbitrary events and triggering an AJAX postback.

Add some JavaScript triggers?:
http://www.alistapart.com/articles/scripttriggers/
(A List Apart's CMS is actually powered by Rails)

Rails leans towards beautiful rhtml, but the observe_field helper drops
javascript bang in the middle of your html. MVC is one thing, but for
separate presentation, behavioural, and structural layers on the browser
side
of things, try the JavaScript triggers article and the excellent
accessible
pop-up links article:
http://www.alistapart.com/articles/popuplinks/

To apply this to rails, the prototype library gives you $('element_id')
to
supplant the getElementById function used in the articles, and you can
either
place your triggers in an external JS file and link it in with a
javascript_include_tag in your layout, or use

<script type="text/javascript"><%= @content_for_page_scripts %></script>

in your layout and

<% content_for("page_scripts") do -%>
/* insert JS here */
<% end -%>

in your templates to customise events by controller/action.

Maybe examine the code generated by the observe_field helper and apply
some A
List Apart goodness to it. Haven't tried it personally, but am meaning
to do
so in the near future... Any comments Mr Dan Benjamin?
http://www.alistapart.com/authors/b/danbenjamin/

--

Mark Beattie
Easy Schedule Management
http://easy-online-schedule.com
This topic is locked and can not be replied to.