我有一个有大量Javascript (特别是jquery)的表单。当我手动输入它的url导航到页面时,或者当我从登录页面重定向到它时,所有的javascript元素都加载得很好。但是,当我从索引中的链接导航到页面时,除非我刷新,否则javascript都不会加载。(我不知道这是否是索引特有的问题;索引只是碰巧包含到表单的唯一链接)。
我把我所有的JavaScript合并成init.js,这是我的application.js中的required。其内容如下:
jQuery(document).ready(function($) {
// scolling accelleration
$(".anchor_link").click(function(event) {
event.preventDefault();
$('html,body').animate( { scrollTop:$(this.hash).offset().top } , 1000);
} );
$('#save').click(function(){
$('#save_alert').animate({"bottom":"100px"}, "slow").delay(800).animate({"bottom":"50px"}, "slow");
});
//styling radio buttons
function radStyle(entity){
$(entity).find('input[type="radio"]').css("display", "none");
$(entity).find('input[type="radio"]').parent().prepend('<span class="radiospan fa-stack">')
$(entity).find("span.radiospan").append('<i class="fa fa-circle fa-stack-2x fa-inverse">')
$(entity).find('input[type="radio"]:checked').siblings("span.radiospan").append('<i class="fa fa-circle fa-stack-1x">');
}
//calling radio buttons on document ready
radStyle(document);
//adding interactive radio button functionality (delegate to provide functionality to dynamically generated fields)
$('div.form-right').delegate('input[type="radio"]', 'click', function(){
if($(this).is(':checked')){
$(this).siblings("span.radiospan").append('<i class="fa fa-circle fa-stack-1x">');
$(this).closest('span.radio').siblings('span.radio').find('i.fa.fa-circle.fa-stack-1x').remove();
}
});
//select menus on initial load
$('select').customSelect();
//styling select menus and radio buttons upon dynamic content generation
var nests = [ "dynamic-officer", "dynamic-director", "dynamic-contractor-person", "dynamic-contractor-org", "dynamic-ip", "dynamic-shareholder"];
nests.forEach(function(nest){
$("#"+nest).on("cocoon:after-insert", function(e, added_item){
added_item.find('select').customSelect();
radStyle(added_item);
});
});
} );再记几个音符
init.js中的所有内容复制到我认为的嵌入式<script>部分中,它就会加载得很好。无论如何,这完全有可能是rails的工作方式,我需要在我的视图中使用脚本标记(我以为我读过它不是,但谁知道)。任何关于这件事的建议都将不胜感激;事先感谢任何人在这件事上给我开诚布公的意见。
如果它与错误有关,我的index.html.erb是
<div class="container-fluid">
<h1>Incorporation#index</h1>
<table class="table table-hover">
<% @incorporations.each do |incorporation| %>
<tr>
<td class="name">
<% begin %>
<%= link_to incorporation.company.names.first.name_string, edit_incorporation_path(incorporation) %>
<% rescue %>
<%= link_to "Untitled Company", edit_incorporation_path(incorporation) %>
<% end %>
</td>
<td class="email"><%= link_to incorporation.user.email, incorporation %></td>
<td class="created_at"><%= link_to incorporation.created_at, edit_incorporation_path(incorporation) %></td>
<td class="generate"><%= button_to "Generate Documents", incorpgenerate_incorporation_path(incorporation), :remote => true, :method => :post %></td>
</tr>
<% end %>
</table>
</div>发布于 2015-02-11 01:45:47
这似乎是一个与吐鲁林克创业板相关的问题。默认情况下,这个gem附带Rails 4,并且已知会导致大量的javascript调用问题,例如‘(文档).ready(函数($)’)。这是因为Turbolink创业板避免在页面间导航时完全加载所有应用程序资产(JS和CSS)。
发布于 2015-02-11 02:22:35
我想是涡轮机。
读这个:Jquery Turbolinks Gem
https://stackoverflow.com/questions/28445120
复制相似问题