Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the jquery load method provide ajax functionality equivalent to the Rails replace_html method?

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

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

Is this an alternative to calling a partial with the Rails replace_html method or is the functionality slightly different?

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's the advantage/disadvantage of this? Does it just mean that the fragment you return has more functional capabilities in the same way that a web page that uses Javascript is more powerful than a regular HTML page? Or is there some other reason for returning Javascript instead of HTML?

like image 667
pez_dispenser Avatar asked May 15 '09 15:05

pez_dispenser


1 Answers

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

like image 58
Craig Stuntz Avatar answered Oct 14 '22 08:10

Craig Stuntz