Does jquery load method provide ajax function equivalent to Rails replace_html method?

The jquery loading method loads HTML from a remote file and injects it into the DOM. For example, to load feeds.html into a div with feed ID, you would do the following:

$("#feeds").load("feeds.html"); 

Is this an alternative to calling partial with the Rails replace_html method or is it different from others?

 page.replace_html 'feeds', :partial => 'main/feeds', :locals => {:feed => @feed_data } 

[EDIT]: As Craig Stuntz points out, replace_html returns Javascript instead of HTML - what is the advantage / disadvantage of this? Does this mean that the snippet you return has more functionality just like a web page using Javascript is more efficient than a regular HTML page? Or is there another reason to return Javascript instead of HTML?

+6
jquery dom ajax ruby-on-rails
source share
1 answer

The end result is the same, but they work very differently. The download method means that your server returns an HTML fragment, and jQuery inserts it into the page. The replace_html method means that your server returns JavaScript instead of HTML, and this JavaScript is executed to insert an HTML fragment into the page. Thus, the final resulting HTML is the same, but the traffic between the client and the server is very different.

+6
source share

All Articles