Forum: Ruby on Rails Complex Javascipt Stopped Working after AJAX Call

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.
Ee9644e44791ba22fc93ebb54eca151f?d=identicon&s=25 Alat Meth (alatmeth)
on 2007-01-01 21:30
This has to be the weirdest problem ever.
I will be as specific as possible.

Let's say I have a partial "_profile.rhtml".
It is first displayed in "user.rhtml" using:

<...>
<div id="ajaxWrapper">
  <%= render :partial=>'profile' %>
</div>
<...>

All javascripts on the "_profile" partial works (more specifically, a
lightbox script (ibox) and a sortable table script). However, when the
profile is updated via an AJAX call, say via someMethod in my user
controller:

<...>
render :update do |page|
  page.replace_html 'ajaxContent', :partial => "profile"
end
<...>

The partial is read and rendered in the the correct div and everything
seems working except that all the "complex" javascripts stopped working.
(i.e. Table no longer sorts and lightbox doesn't appear) I've added a
line of "simple" javascript to "_profile.rhtml":

<a href="javascript:alert('hi')">simple</a>

And that worked both before and after the AJAX update.

What may be the issue here? Thanks.
Ee9644e44791ba22fc93ebb54eca151f?d=identicon&s=25 Alat Meth (alatmeth)
on 2007-01-01 22:53
:-)
Ee9644e44791ba22fc93ebb54eca151f?d=identicon&s=25 Alat Meth (alatmeth)
on 2007-01-02 01:31
Patiently waiting for the gurus :)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-01-02 01:42
(Received via mailing list)
Alat Meth wrote:

> Patiently waiting for the gurus :)

That's how this shop works.

While you wait, you could run in Firefox with Firebug installed. The FAQ
"My
Ajax stopped working" has the first answer "A browser will often decline
to
run any Javascript after an error". So trap the error and you might just
solve it yourself.

If you do, please remember to finish your own thread, so the archives
won't
have a false trail!

--
  Phlip
  http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Ee9644e44791ba22fc93ebb54eca151f?d=identicon&s=25 Alat Meth (alatmeth)
on 2007-01-02 01:53
Thanks Phlip, I've followed your advice but firebug has detected no JS
errors. Meanwhile, I've also commented out most of the stuff that is not
relavant but it seems that javascript just refuse to function after been
called via AJAX. (The AJAX update itself is working fine)

Any other possibilities? Could it be a configuration problem?
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-01-02 01:58
(Received via mailing list)
> Any other possibilities? Could it be a configuration problem?

Try the Rails-spinoffs mailing list (stupid name, I know) for the
interaction between Rails and the Script.aculo.us JS libraries.

--
  Phlip
  http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Ee9644e44791ba22fc93ebb54eca151f?d=identicon&s=25 Alat Meth (alatmeth)
on 2007-01-02 12:42
Still waiting for guru :)
523b892d499d1a2d1779d5d320404088?d=identicon&s=25 brian.estlin (Guest)
on 2007-01-02 17:10
(Received via mailing list)
I'm no guru, but I had a similar problem once so maybe my experience
will be helpful.

This was a while back, and I'm barely awake yet, so forgive my
vagueness.

In my situation, the JS in the partial was setting up some structures,
lets say a sortable list, but there was a single line of javascript at
the end of the page which "turned on" all the objects. When reloading a
partial, all the setup code got re-done, but the code outside the
partial was of course not re-loaded, so the objects remained inert.

HTH,
Brian
Ee9644e44791ba22fc93ebb54eca151f?d=identicon&s=25 Alat Meth (alatmeth)
on 2007-01-02 18:33
Hey thanks Brian. Any other suggestions?
Ee9644e44791ba22fc93ebb54eca151f?d=identicon&s=25 Alat Meth (alatmeth)
on 2007-01-02 19:09
Alat Meth wrote:
> Hey thanks Brian. Any other suggestions?

Thanks Brian, it worked like a charm.

For future reference, most JS libraries call to init itself by:
Event.observe(window, 'load', SortableTable.load);

But after an AJAX call, you might have to reload it manually, add this
to the link_to_remote or observe_field etc.
:complete => "SortableTable.load()"

Thanks.
This topic is locked and can not be replied to.