在创建一个Ruby on Rails / jQuery应用程序时,页面的某个部分生成需要耗费大量时间。
我想改变页面的加载方式,以便大部分页面可以立即加载,并为需要耗费时间的部分保留一个占位符,通过ajax / jQuery异步加载并在完成后注入到页面中。
现在我的实现方式(简化版):
app/views/sample/show.html.erb:
<div id="theResult">
<%= render :partial => 'calculate', :object => @org) %>
</div>
部分内容将使用@org的一些部分生成一些内容(调用另一个外部REST服务)。
app/views/sample/_calculate.html.erb
<%
# code to take org and turn it into content
%>
<!--...html to display results here -->
我意识到这可能违反了正确的MVC架构规则,因为我的partial似乎有太多的逻辑,我想清理一下...
所以我想我有两个问题:(1) 如何使它工作,(2) 如何清理代码以遵循良好的Ruby/Rails/MVC实践?