简体   繁体   English

优化前端代码

[英]Optimizing Front-end code

I have a view on Ruby on Rails, and it works fine, but my problem is that it takes quite sometime before the page loads. 我对Ruby on Rails有一个看法,并且工作正常,但是我的问题是页面加载之前需要花费一些时间。 This is some of the code: 这是一些代码:

Main tab 主标签

<div class="tab-content">
                 <div class="tab-pane fade in active" id="tabconstituent">
                     <h3>Constituent Database</h3>
                     <ul class="nav nav-tabs">
                      <li class="active"><a href="#tabconstinbox" data-toggle="tab">Inbox</a></li>
                      <li><a href="#tabconstsent" data-toggle="tab">Sent</a></li>

                    </ul>

                  <div class="tab-content"> 
                      <div role="presentation" class="tab-pane fade in active" id="tabconstinbox">
                          <div class="container-fluid">
                              <br />
                              <div class="col-lg-6 pull-right">
                                <div class="input-group pull-right" id="searchDrafts">
                                  <input type="text" class="form-control"  placeholder="Search for">
                                  <span class="input-group-btn">
                                    <button class="btn btn-default"  type="button">
                                        <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                                    </button>
                                  </span>
                                </div><!-- /input-group -->

                                <div class="dropdown">
                                     <button class="btn btn-default dropdown-toggle pull-right" type="button" id="dropdownDrafts" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                        Filter by
                                        <span class="caret"></span>
                                     </button>
                                  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                                    <li><a href="#">Sent to</a></li>
                                    <li><a href="#">Subject</a></li>
                                    <li><a href="#">Date</a></li>
                                  </ul>
                                </div>
                             </div><!-- /.col-lg-6 -->
                             <%= will_paginate @received_messages %>
                              <%= form_tag destroy_many_received_messages_path, method: :post do %>
                             <table class="table table-striped">
                                 <thead>
                                    <tr>
                                      <th><input type="checkbox" id="checkbox_all" /></th>
                                      <th data-field="sender" data-sortable="true">Sender</th>
                                      <th data-field="complaint" data-sortable="true">Complaint</th>
                                      <th data-field="location" data-sortable="true">Location</th>
                                      <th data-field="subject" data-sortable="true">Subject</th>
                                      <th data-field="department" data-sortable="true">Department</th>
                                      <th data-field="action_taken" data-sortable="true">Action Taken</th>
                                      <th data-field="status" data-sortable="true">Status</th>
                                      <th data-field="date" data-sortable="true">Date</th>
                                    </tr>
                                 </thead>
                                 <tbody>

                                    <% @received_messages.each do |received_message| %>
                                      <tr>
                                        <td><%= check_box_tag "message_ids[]", received_message.id%></td>
                                        <td><%= received_message.name%></td>
                                        <td><%= received_message.message %></td>
                                        <td><%= received_message.district %> <%= received_message.barangay %></td>
                                        <td><%= received_message.subject %></td>
                                        <td><% received_message.departments.uniq.each do |department|%>
                                              <%= department.name %> 
                                            <% end -%></td>
                                        <td><%= received_message.action_taken %></td>
                                        <td><%= received_message.status %></td>
                                        <td><%= received_message.created_at %></td>
                                      </tr>
                                    <% end %>
                                 </tbody>
                             </table>  

                             <%= submit_tag "Delete", id:"buttonConstInbox_Delete", :class =>"btn btn-primary"%>
                             <%= submit_tag "Update", id:"buttonConstInbox_Update", :class =>"btn btn-primary", remote: true, 'data-toggle' => "modal", 'data-target' => "#constSentModal" %>
                             <%= submit_tag "Attach", id:"buttonConstInbox_Attach", :class =>"btn btn-primary" %>
                             <% end %>

                         </div><!-- /.container-fluid -->
                      </div>
                      <div role="presentation" class="tab-pane fade" id="tabconstsent">
                          <div class="container-fluid">
                              <br />
                              <div class="col-lg-6 pull-right">
                                <div class="input-group pull-right" id="searchDrafts">
                                  <input type="text" class="form-control"  placeholder="Search for">
                                  <span class="input-group-btn">
                                    <button class="btn btn-default"  type="button">
                                        <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                                    </button>
                                  </span>
                                </div><!-- /input-group -->

                                <div class="dropdown">
                                     <button class="btn btn-default dropdown-toggle pull-right" type="button" id="dropdownDrafts" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                        Filter by
                                        <span class="caret"></span>
                                     </button>
                                  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                                    <li><a href="#">Sent to</a></li>
                                    <li><a href="#">Subject</a></li>
                                    <li><a href="#">Date</a></li>
                                  </ul>
                                </div>
                             </div><!-- /.col-lg-6 -->
                             <%= will_paginate @send_messages %>
                             <%= form_tag destroy_many_received_messages_path, method: :post do %>
                             <table class="table table-striped">
                                 <thead>
                                    <tr>
                                      <th><input type="checkbox" id="checkbox_all" /></th>
                                      <th data-field="sent_to" data-sortable="true">Sent To</th>
                                      <th data-field="message" data-sortable="true">Message</th>
                                      <th data-field="status" data-sortable="true">Status</th>
                                      <th data-field="date" data-sortable="true">Date</th>
                                    </tr>
                                 </thead>

                                  <tbody>
                                    <% @send_messages.each do |send_message| %>
                                      <tr>
                                        <td><%= check_box_tag "message_ids[]", send_message.id%></td>
                                        <td><%= send_message.mobile_number%></td>
                                        <td><%= send_message.message %></td>
                                        <td><%= send_message.status %></td>
                                        <td><%= send_message.created_at %></td>
                                      </tr>
                                    <% end %>
                                 </tbody>

                             </table>    
                            <% end %>
                             <button type = "button" id="buttonConstSent_Delete" class = "btn btn-primary">Delete</button>
                         </div><!-- /.container-fluid -->
                      </div>
                  </div><!-- /.tab-content inbox/sent/trash-->

There are 5 more tabs that are identical to that, and I have a feeling the reason why it takes a while to load is because, each time a load that page, it loads everything, not the tab I'm currently on. 还有5个与此选项卡相同的选项卡,我感觉需要花一些时间来加载是因为每次加载该页面时,它都会加载所有内容,而不是我当前位于的选项卡。 This becomes a big problem especially when I want to paginate the data and each page loads all tabs. 这成为一个大问题,尤其是当我想对数据进行分页并且每个页面都加载所有选项卡时。 Is there any way to better optimize this code? 有什么办法可以更好地优化此代码? Thank you. 谢谢。

The best solution for this is to make partial for each tab and load those partials through ajax when you click on that tab. 最好的解决方案是为每个选项卡创建部分,并在单击该选项卡时通过ajax加载这些部分。

First, render the main page and add remote: true to tab links. 首先,呈现主页并添加remote: true到选项卡链接。 When you click on tab link render partial through js. 当您单击选项卡链接时,将通过js渲染部分内容。

I hopes this will help you. 希望对您有帮助。

create a partial with the name _tab.html.erb and use 创建名称为_tab.html.erb的局部文件并使用

remote: true

To use ajax which actually works to render the JS code where you can do whatever you that is the actual workflow. 要使用实际上可以渲染JS代码的ajax,您可以在其中执行实际工作流程中的任何操作。

If you want to call your partial just render it by 如果您想调用您的部分,只需渲染它

render "tab.html.erb"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM