observe_form, observe_field, dynamic forms help

177 views
Skip to first unread message

krunk-

unread,
Nov 28, 2006, 2:41:08 PM11/28/06
to Ruby on Rails: Spinoffs
I made a pretty lengthy post on what I'm doing here:
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/89a2b8e54a874dd9

Short version:

I have a radio_button group of two. If one is clicked another pair of
radio buttons is added, if the other is clicked it is removed.

observe_form does not work for elements added after initial page load.
observe_field does not work for radio_buttons except on the first
'click'

It was suggested that I stop observe_field before changes are made to
the form then restart it.

With much searching I found a Event.stopObserving method, but I'm not
certain how to apply this to my observe_form observer.

>From the source:
new Form.EventObserver('process_call_form', function(element, value)
{new Ajax.Request('/rails/assistant/add_element', {asynchronous:true,
evalScripts:true, parameters:'Form.serialize(process_call_form)=' +
value})})

>From this I did:

function = "function(element, value) {new
Ajax.Request('/rails/assistant/add_element', {asynchronous:true,
evalScripts:true, parameters:'Form.serialize(process_call_form)=' +
value})}"

page << "Event.stopObserving('process_call_form', 'change',
#{function})"

This doesn't throw an error, but it doesn't stop or "reset" the
observer as far as I can tell either. new element are still unobserved
and old elements still are.

I tried using 'click' instead of 'change' as well since the api docs
indicate that the default :on => option varies from buttons to other
elements.

Ken Snyder

unread,
Nov 28, 2006, 5:01:58 PM11/28/06
to rubyonrail...@googlegroups.com
krunk- wrote:
> ...

> >From the source:
> new Form.EventObserver('process_call_form', function(element, value)
> {new Ajax.Request('/rails/assistant/add_element', {asynchronous:true,
> evalScripts:true, parameters:'Form.serialize(process_call_form)=' +
> value})})
> ...

> page << "Event.stopObserving('process_call_form', 'change',
> #{function})"
>
The third parameter in Event.stopObserving() should be a handle from the
original Event.observe() call. For example:

var handle = Event.observe(element,'click',myFunc);
Event.stopObserving(element,'click',handle);

From what I can see in the source of the parent class
Abstract.EventObserver() there is no returning or caching of the
Event.observe() handles.

Perhaps this is a limitation or am I missing something?

--Ken


Reply all
Reply to author
Forward
0 new messages