Multiple (AJAX) Observers on the Same Field and MSIE

I have been using multiple observers, i.e., observe_field(), on the same
input field and relying on them to execute in the same order that they
appear in the page. This has been working fine in FireFox, but it does
not seem to work in MSIE; the requests come in and are processed in a
different order. Now, I’ve always been a little hesitant about using
this technique, but it always seemed to work. My question is, is there a
better way to do this?

Hi Jason,

Jason F. wrote:

I have been using multiple observers, i.e., observe_field(),
on the same input field and relying on them to execute in
the same order that they appear in the page.

You’ve got me curious… Why multiple observers on the same field? I
assume you’re observing for the same event given your “relying on them
to
execute…” statement. If so, then the question would be “why not put
all
the code in one place in the order you need it to execute?”

Best regards,
Bill

Bill W. wrote:

You’ve got me curious… Why multiple observers on the same field? I
assume you’re observing for the same event given your “relying on them
to execute…” statement. If so, then the question would be “why not put
all the code in one place in the order you need it to execute?”

I am using the multiple observers so that I can update multiple sections
of the page when the input field changes, e.g., user checks a checkbox
and both DIV1 and DIV2 are updated, but the update logic for DIV2
depends on changes performed in the action that handles updating DIV1.
The multiple observer method was a bit messy but always seemed to work
(except in MSIE). – However, I think I’ve figured out a better way to
do it after doing some more digging.

There is an update_element_function Prototype helper that I can embed in
the partial returned by code that updates DIV1, that, when reached, will
execute a second AJAX call to update DIV2. I think this will reliably
“chain” my updates together in a way that will work in all browsers.
Thoughts?

Thanks for the response!

Regards,
Jason

Hi Jason,

Jason F. wrote:

I am using the multiple observers so that I can
update multiple sections of the page when the
input field changes

Thoughts?

I’m thinking you’re using the :update option on observe_field. If
that’s
right, I’m thinking … Don’t. Use RJS (pick up Cody F.'s $10 PDF
book(let) on O’Reilly). Then you’ll only need one observer and you can
update the page to your heart’s content from the RJS template. Or maybe
I’m
missing something about what you’re trying to accomplish. In which
case, I
apologize for my density :wink:

Best regards,
Bill

I think this link will solve the problem.
and it is nice coding style too ;).

http://rails.techno-weenie.net/tip/2006/6/20/cascading_select_boxes_with_rjs_even_on_ie

Yours,
Retonator

Hi

I have three drop downs (State, City, Locality) and would like to
populate
City based on State selection and Locality based on City selection.

I have observers on two fields (State, and City). The observer_field on
state
works fine and it populates city based on state selection. But the
observer
on city doesn’t populate Locality. Now sure whats wrong with my code.

here is the code snippet

RHTML Code:

<% @states.each do |state| %> <%= state.state_name %> <% end %>
Select City
<%= observe_field("state[id]", :update => "city_container", :url => { :action => :select_city }, :with => "'id='+value", :on => "changed") %>
Select Locality
<%= observe_field("city_id", :update => "locality_container", :url => { :action => :select_locality } :with => "'id='+value", :on => "changed") %>

Controller Code

def select_city
@cities = City.find_all_by_state_id(@params[“id”])
@html = “”
@html += “Select City”
@cities.each do |@city|
@html += “#{@city.city_name}”
end
@html += “”
render_text @html
end

def select_locality
@localities = Locality.find_all_by_city_id(@params[“id”])
@html = “”
@html += “Select Locality”
@localities.each do |@locality|
@html += “#
{@locality.locality_name}”
end
@html += “”

render_text “test”

   render_text @html

end

I tried with render_text “test” just to make sure that i get the text
“test”
back on selecting City. But that also doesn’t work. Is there a work
around?

Thx for your help,
Ramanan

Jason F. wrote:

Anyways, thanks for the pointer! It really helped!

You’re welcome. And congrats !!!

Best regards,
Bill

Gerben van de Wiel wrote:

I think this link will solve the problem.
and it is nice coding style too ;).

http://rails.techno-weenie.net/tip/2006/6/20/cascading_select_boxes_with_rjs_even_on_ie

Yours,
Retonator

I could not make it to work. May be I am doing something wrong. The
comments for application_helper.rb says that “It assumes that the
function update_select_options is already in the open document”. What
does this mean?

Do I need to explicitly include “update_select_options” code in my
document or is this code is generated by application_helper.rb. Can
somebody tell me how to include “update_select_options” in my document.

Thx.
Ramanan

Bill W. wrote:

I’m thinking you’re using the :update option on observe_field. If
that’s right, I’m thinking … Don’t. Use RJS (pick up Cody F.'s $10 PDF
book(let) on O’Reilly). Then you’ll only need one observer and you can
update the page to your heart’s content from the RJS template. Or maybe
I’m missing something about what you’re trying to accomplish. In which
case, I apologize for my density :wink:

Yes, I was using the :update option on observe_field. – I had played
around with RJS templates before but could never get them to work for
some reason. I took your advice, though, and purchased the PDF from
O’Reilly on RJS templates.

After a quick read-through I was able to consolidate my code and really
clean things up nicely! Whatever problems I had in the past with RJS
templates are now gone. I think it might have had something to do with
me not knowing to run the rake rails:update script. RJS templates are a
very nice addition to the Rails framework.

Anyways, thanks for the pointer! It really helped!

Regards,
Jason