簡體   English   中英

獲取HTML和javascript以正確的順序在Rails中加載

[英]Getting html and javascript to load in correct order in rails

我的菜單中有一個link_to:remote ajax調用,單擊該鏈接可動態呈現每個頁面的內容。 一切都應按其應有的方式被調用和替換,但是當我第二次單擊菜單鏈接時,D3和vticker僅綁定到它們各自的div。 這使我相信html / js的加載順序不正確。 因此,當它第一次加載時,沒有div可以綁定到它。

此外,在替換的內容內,我有一個內部ajax查詢,它根據單擊的d3工具提示來更新新聞行情自動收錄機,替換內容后,行情自動收錄機ajax查詢不再起作用,我得到了:

GET http://localhost:3000/render_bubble_partial?data=The+Environment 404 (Not Found)

這很奇怪,因為它之前已經起作用,並且我已經設置了路由:

bubbles_render_bubble_partial GET /bubbles/render_bubble_partial(.:format) bubbles#render_bubble_partial
bubbles_render_poll_partial   GET /bubbles/render_poll_partial(.:format)   bubbles#render_poll_partial
bubbles_render_bubble_content GET /bubbles/render_bubble_content(.:format) bubbles#render_bubble_content

菜單中的li部分:

<li id='panel-3'><%= link_to image_tag('keyBubble.png'), bubbles_render_bubble_content_path, :remote => true %></li>

我渲染的部分內容:

<div class="col span_5_of_7">
    <section id="infographic">
        <div id ="infographicContent">
            <input id="slider" style="position:inherit;top:4px;" type="range" min="0" max="62" value="0"> </input>
            <div id="tooltip">
            </div>
            <%= javascript_include_tag "http://d3js.org/d3.v3.min.js" %>
            <%= javascript_include_tag "bubbles", "vticker" %>
            <script>
                $('.col.span_5_of_7').click(function(){
                      var text = $("#tooltip b").text();
                        if(text == ""){text = null}
                        var params = {data:text};
                        console.log(params)
                        $.ajax({
                          url: "render_bubble_partial",
                          data: params, 
                          success: function(result) {
                               console.log("finished ajax callback");
                          }
                        });
              });
            </script>   
        </div>
    </section>  
</div>
<div class="col span_1_of_7">
    <section id="feed">
        <div id ="infoFeed">
            <ul style="font: 10px sans-serif;text-align: justify;">
                <% bubble_query(nil) %>
                <% @bubble_story.each do |story| %>
                    <li style="border-style:solid;border-top:thick double #000;">
                        <p><h4 style="text-align:left;"><%= link_to story.title, story.url, :target => "_blank" %>
                            </h4> <%= story.description %> <h7><br /><br /><%= story.source %><br /><%= story.date.to_date %><%= story.issue %></h7></p>
                    </li>
                <% end %>
            </ul>
        </div>
    </section>
    <script> $(function() { $('#infoFeed').vTicker();});</script>
</div>

我不確定該如何解決...有人可以提供一些建議嗎?

您是否正在調用ajax從本身由ajax更新的部分中重新加載部分? 如果是這樣,請嘗試將其移出。 您可以嘗試將JavaScript移至應用程序布局或application.js文件。

但是在部分布局和應用程序布局之間移動javascript_include_tags,我能夠使其正確加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM