Ruby on Rails Thursday, December 10, 2015

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.

No comments:

Post a Comment