I render a partial after an ajax call:

*show.js.erb*

     $("#notice_feed").html("<%=j render 'shared/notice_feed' %>")

*_notice_feed.html.erb*

     <ol class="notices" id="notice_list">
       <%= render @notices %>
     </ol>

This ends up rendering a list of `@notices`. The names of each notice are
listed on the right and I have some jquery to scroll down to the relevant
rendered `@notice` when you click on the name:

     name.on('click', function() {
       $("#absolute-div").animate({scrollTop: $('#notice_' + this.className).
offset().top -200 }, 500);
     });

When you click on the name it correctly scrolls down, but only sometimes
stops scrolling on the correct `@notice`, and gets nowhere near for other
`@notices`. I suspect the problem is that the jquery is called before the
whole partial list has finished rendering. I've tried surrounding the
javascript with

     $(window).on("load", function() {
       ...
     };

but this doesn't fire, presumably because it already fired when the
original page was loaded.

How do I check that a partial following an ajax call has fully loaded using
jquery?

Or is there a better way to identify a div and scroll to it accurately?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/305ca173-aad3-4401-ad07-0c06433b06e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Walter Lee Davis at Dec 11, 2015 at 2:14 am

    On Dec 10, 2015, at 5:11 PM, Bazley wrote:

    I render a partial after an ajax call:

    show.js.erb

    $("#notice_feed").html("<%=j render 'shared/notice_feed' %>")

    _notice_feed.html.erb

    <ol class="notices" id="notice_list">
    <%= render @notices %>
    </ol>

    This ends up rendering a list of `@notices`. The names of each notice are listed on the right and I have some jquery to scroll down to the relevant rendered `@notice` when you click on the name:

    name.on('click', function() {
    $("#absolute-div").animate({scrollTop: $('#notice_' + this.className).offset().top -200 }, 500);
    });

    When you click on the name it correctly scrolls down, but only sometimes stops scrolling on the correct `@notice`, and gets nowhere near for other `@notices`. I suspect the problem is that the jquery is called before the whole partial list has finished rendering. I've tried surrounding the javascript with

    $(window).on("load", function() {
    ...
    };

    but this doesn't fire, presumably because it already fired when the original page was loaded.

    How do I check that a partial following an ajax call has fully loaded using jquery?

    Or is there a better way to identify a div and scroll to it accurately?
    One trick you may try is adding your script at the bottom of that partial, so it has to wait naturally until the stuff above it is in the page.

    Walter
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/305ca173-aad3-4401-ad07-0c06433b06e3%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/59EE1814-B778-4FAE-9CB9-B68C7A6248C9%40wdstudio.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 10, '15 at 10:12p
activeDec 11, '15 at 2:14a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Walter Lee Davis: 1 post Bazley: 1 post

People

Translate

site design / logo © 2021 Grokbase