- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 main.js
文件:
jQuery(function($) {
$('LI.tree-item-name').has('ul').click(function() {
if ($(this).hasClass('opened')) {
$(this).find('UL').slideUp();
$(this).removeClass('opened');
} else {
$(this).find('UL').slideDown();
$(this).addClass('opened');
}
return false;
});
$('LI.tree-item-name li').click(function(e) {
e.stopPropagation();
})
});
jQuery(window).load(function(){
jQuery('#video').removeClass('loading');
jQuery('#video .box').animate({'opacity' : '1'});
var $container = jQuery('#video');
$container.masonry({
columnWidth: 299,
itemSelector: '.box'
});
jQuery('.popupbox').click(function(){
jQuery('.popup:visible').fadeOut();
var id = '#'+jQuery(this).data('popup');
jQuery('#overlay').fadeIn();
jQuery(id).css('top', jQuery(window).height()/2 - jQuery(id).height()/2).fadeIn();
return false;
});
jQuery('.profile_popupbox').click(function(){
jQuery('.popup:visible').fadeOut();
var id = '#'+jQuery(this).data('popup');
jQuery('#overlay').fadeIn();
jQuery(id).css({'top': "20px", 'left': "-200px"}).fadeIn();
return false;
});
jQuery('#overlay').click(function(){
jQuery('.popup:visible').fadeOut();
jQuery(this).fadeOut();
});
});
我希望这对 Turbolinks 友好。
我试过:
var ready;
ready = jQuery(window).load(function(){
jQuery('#video').removeClass('loading');
jQuery('#video .box').animate({'opacity' : '1'});
var $container = jQuery('#video');
$container.masonry({
columnWidth: 299,
itemSelector: '.box'
});
jQuery('.popupbox').click(function(){
jQuery('.popup:visible').fadeOut();
var id = '#'+jQuery(this).data('popup');
jQuery('#overlay').fadeIn();
jQuery(id).css('top', jQuery(window).height()/2 - jQuery(id).height()/2).fadeIn();
return false;
});
jQuery('.profile_popupbox').click(function(){
jQuery('.popup:visible').fadeOut();
var id = '#'+jQuery(this).data('popup');
jQuery('#overlay').fadeIn();
// jQuery(id).css('top', jQuery(window).height() - jQuery(id).height()/2).fadeIn();
jQuery(id).css({'top': "20px", 'left': "-200px"}).fadeIn();
return false;
});
jQuery('#overlay').click(function(){
jQuery('.popup:visible').fadeOut();
jQuery(this).fadeOut();
});
});
$(document).ready(ready);
$(document).on('page:load', ready);
那是行不通的,也没有使这两个函数都对 Turbolinks 友好。
所以我只想让整个文件对 TL 友好。
编辑 1
我还有一个 upload.js.erb
,它有这个,在初始页面加载后不会执行:
$("#myVCModal").html("<%= escape_javascript(render 'videos/upload_video') %>");
$("#myModal").html("<%= escape_javascript(render 'videos/upload_video') %>");
$("#add-video-step-1").html("<%= escape_javascript(render 'videos/upload_video') %>");
$("#video-comment").html("<%= escape_javascript(render 'videos/upload_video') %>");
$('myModalPL').modal(show);
Ladda.bind('button');
我希望我的应用程序中的所有这些 JS 位都能恢复工作。
编辑2
所以现在我有 main.js
的东西在工作 - 谢谢 @User089247。但是其他执行 JS 的模态根本不起作用....即编辑 1 下的代码。
发生的事情是我点击了这个上传按钮:
<%= link_to "<i class='fa fa-film fa-lg'></i> Upload".html_safe, "#", class: "upload popupbox", data: { popup: "add-video-step-1"} %>
这是被触发的模式:
<div id="overlay"> </div>
<div class="popup" id="add-video-step-1">
<div class="titles clearfix">
<h2>Upload a Video</h2>
<p><i>Step 1 of 2 - TEST</i></p>
</div>
<div class="content">
<% if @family_tree %>
<%= simple_form_for([@family_tree, @video], :remote => true) do |f| %>
<div class="column">
<div class="f-row">
<%= f.input :title, label: "Title:" %>
</div>
<div class="f-row">
<%= f.input :description,label: "Description:" %>
</div>
<div class="f-row">
<%= f.input :circa, as: :datepicker, start_year: Date.today.year - 5, label: "Circa:" %>
</div>
<div class="f-row">
<label for="family">Family in this video:</label>
<%= f.collection_select :user_ids, @family_tree.members.order(:first_name), :id, :first_name, {}, {multiple: true} %>
</div>
</div>
<%= f.button :submit, "Add Video" %>
<% end %>
<% end %>
</div> <!-- //content -->
</div> <!-- //popup -->
然后当您按下“添加视频”时,它应该将您带到第二个模态:
<div class="bootstrap-styles">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Upload your Video</h3>
<p><i>Step 2 of 2 - TEST</i></p>
</div>
<div class="modal-body">
<div class="form">
<%= form_tag @upload_info[:url], :multipart => true do %>
<div>Step 2 of 2</div>
<%= hidden_field_tag :token, @upload_info[:token] %>
<%= file_field_tag :file, title: 'Choose video to upload' %>
<p class="uploader">
<button class="btn btn-success ladda-button" data-color="green" data-style="expand-left"><span class="ladda-label">Upload Video</span><span class="ladda-spinner"></span></button>
</p>
<% end %>
</div>
</div>
<div class="modal-footer">
</div>
</div>
我想问题是我没有看到根据我的 upload.js.erb
这第二个模态将如何执行,或者我是否遗漏了什么?
编辑3
以下是此上传视频操作完成后服务器日志的相关部分(为简洁起见被 chop ):
Started POST "/family_trees/1/videos" for 127.0.0.1 at 2014-10-28 02:16:48 -0500
Processing by VideosController#create as JS
Parameters: {"utf8"=>"✓", "video"=>{"title"=>"Hello there", "description"=>"Why hello there lady", "circa"=>"", "user_ids"=>[""]}, "commit"=>"Add Video", "family_tree_id"=>"1"}
User Load (3.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (1.2ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 1]]
ReadMark Load (1.0ms) SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."user_id" = $1 AND "read_marks"."readable_type" = 'PublicActivity::ORM::ActiveRecord::Activity' AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT 1 [["user_id", 1]]
FamilyTree Load (1.0ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."id" = $1 LIMIT 1 [["id", 1]]
(1.2ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
Membership Load (1.7ms) SELECT "memberships".* FROM "memberships" WHERE "memberships"."user_id" = 1 AND "memberships"."family_tree_id" = 1
(3.6ms) BEGIN
SQL (3.4ms) INSERT INTO "videos" ("created_at", "description", "title", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", "2014-10-28 07:16:48.340452"], ["description", "Why hello there lady"], ["title", "Hello there"], ["updated_at", "2014-10-28 07:16:48.340452"]]
(4.5ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 40ms (ActiveRecord: 20.6ms)
Started GET "/" for 127.0.0.1 at 2014-10-28 02:16:48 -0500
User Load (4.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Processing by DashboardController#index as JS
就其值(value)而言,即使在成功处理完此 GET "/"
请求之后,它实际上并没有重新加载/重定向浏览器。它只是停留在模态。
JS 控制台中的错误提示:
Failed to load resource: net::ERR_CACHE_MISS
这是VideoController#Create
def create
authorize! :read, @family_tree
@video = Video.new(video_params)
respond_to do |format|
if @video.save
format.html { redirect_to root_path }
format.json { render action: 'show', status: :created, location: @video }
else
format.html { render action: 'new' }
format.json { render json: @video.errors, status: :unprocessable_entity }
end
end
最佳答案
将您的 main.js 更改为此(我更喜欢这种方式,因为它很容易调试 jQuery 中的问题而不会迷失在方法的网络中,并且它与 turbolinks 一起工作得很好而无需为其添加 jquery-turbolinks gem ):
$(document).on("page:change", function(){
MainJS.init();
});
MainJS = {
init: function() {
MainJS.initializePage();
$('.popupbox').on('click', MainJS.popupBox);
$('.profile_popupbox').on('click', MainJS.profilePopupbox);
$('#overlay').on('click', MainJS.overLay);
$('LI.tree-item-name').has('ul').on('click', MainJS.treeItemName);
$('LI.tree-item-name li').on('click', function(ev) {
ev.stopPropagation();
});
},
initializePage: function(){
$('#video').removeClass('loading');
$('#video .box').animate({'opacity' : '1'});
var $container = $('#video');
$container.masonry({
columnWidth: 299,
itemSelector: '.box'
});
},
popupBox: function(){
$('.popup:visible').fadeOut();
var id = '#'+$(this).data('popup');
$('#overlay').fadeIn();
$(id).css('top', $(window).height()/2 - $(id).height()/2).fadeIn();
return false;
},
profilePopupbox: function(){
$('.popup:visible').fadeOut();
var id = '#'+$(this).data('popup');
$('#overlay').fadeIn();
// $(id).css('top', $(window).height() - $(id).height()/2).fadeIn();
$(id).css({'top': "20px", 'left': "-200px"}).fadeIn();
return false;
},
overLay: function(){
$('.popup:visible').fadeOut();
$(this).fadeOut();
},
treeItemName: function(){
if ($(this).hasClass('opened')) {
$(this).find('UL').slideUp();
$(this).removeClass('opened');
} else {
$(this).find('UL').slideDown();
$(this).addClass('opened');
}
return false;
}
}
现在,如果您可以轻松调试上面的代码以查看哪个部分工作/不工作,例如:
$(document).on("page:change", function(){
MainJS.init();
console.log('MainJS.init() was called successfully);
});
或:
MainJS = {
init: function() {
MainJS.initializePage();
$('.popupbox').on('click', MainJS.popupBox);
$('.profile_popupbox').on('click', MainJS.profilePopupbox);
$('#overlay').on('click', MainJS.overLay);
console.log('MainJS initialization was successful.');
},
等等。你明白了吧?
关于你的 upload.js.erb 问题,我不确定那里有什么问题,因为它应该工作(因为同样的事情对我有用),但是,你可以将你的代码包装起来:$(document).on("page:change", function(){
看看它是否有效,但我怀疑这不是问题所在。
关于javascript - 我如何使这个 JS Turbolinks 友好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26459182/
如果我有一个永远不应该被缓存并且应该总是从服务器检索的页面,当用户按下后退按钮时,我怎样才能让 turbolinks 做到这一点?它只会服从来自 http 响应的缓存控制指令吗? 最佳答案 这在 Tu
下面是显示 mdl 菜单的代码。他们单击编辑按钮,然后出现一个菜单。 edit change do something else 不幸的是,当用户单击后退按钮返回带有下拉菜单的页面时,下
我看到在 Gemfile 中加载了两个 gem,那么加载这两个 gem 还是只使用一个是否有意义? gem 'jquery-turbolinks' gem 'turbolinks' 谁能告诉我为什么使
我正在运行 Rails,我在 body 标签的末尾有这段代码。 :javascript var _urq = _urq || []; _urq.push(['initSite', 'mySit
首先,所有受限制的 javascript 都运行得很好。 但是,从我导航到另一个页面的那一刻起,任何事件都会被多次触发。如果我刷新页面,一切都会恢复正常。 在 jquery.turbolink 文档中
根据“跟随重定向”的 Turbolinks 5 文档 (https://github.com/turbolinks/turbolinks#following-redirects): When you
我已经正确连接了 hotwire/turbo 以在我页面的一个地方对单个模型进行 crud 操作,但我想在我页面的不同位置同时更新同一个模型出色地。我以为我可以只设置两个流,但它似乎不起作用。 根据我
有时,希望在页面访问之间保持滚动位置。 加载数据后,Turbolinks会重置滚动位置。 如何为特定元素禁用它? 最佳答案 我在ES6中的解决方案: const turbolinksPersistSc
我的脚本仅在刷新页面时才起作用,因此我尝试仅针对该页面禁用 Turbolinks。下面的代码不起作用。但是,如果我将“data-no-turbolink”属性直接添加到 application.htm
我认为是 Turbolinks 导致了此问题,但如果我在登录时位于我的 root_path 上并转到一堆不同的页面,然后返回到我的 root_path并且页面的大部分主要部分不会显示。 日志显示所有元
我在使用 Rails 中的 Turbolinks 时遇到了一些恼人的问题,我希望你们中的一些人能帮助我解决这个问题。 我正在使用 jQuery-address 将哈希值添加到我的 url,以便在出现特
我有一个具有两种布局的 Rails 4 应用程序,它使用 Turbolinks 和 Devise。用户注销时显示一种布局,登录时显示另一种布局。 在这两种布局中我使用不同的样式表。 问题是,当我打开两
我正在尝试创建一个 ruby on Rails 项目,作为该项目的一部分,我正在使用 this用于管理标签输入的 jQuery 插件。然而,标签字段在第一次刷新时有效,但在任何页面加载后都无效,我
在我的 Rails 5 应用程序上,我有一个应该在 scroll 事件上触发的脚本,但由于某种原因,我无法使其与turbolinks gem 一起使用。 我的代码: 第一次尝试: $("#side-b
我的页面上有一些图像、第三方脚本、插件(例如 Facebook 和 YouTube 小部件)和 iframe(例如 YouTube 视频),但仅当所有这些都完全加载时(页面之后),我才需要运行片段改变
我正在测试 Turbolinks 是否有效可以帮助我们的 Web 应用程序看起来更快一些,表现得“几乎像”单页应用程序。 由于我们已经对所有链接的所有点击都有一种“主钩子(Hook)”,所以我设置 d
我正在使用 Rails 4。我的 shared.js.coffee 中的这段代码在没有 Turbolinks 的情况下工作正常: jQuery -> $('input.autocomplete,t
我正在使用带有turbolinks 的Rails 5 和summernote,它是一个简单的所见即所得编辑器。我目前正在turbolinks:load 上加载插件,如下所示: $(document).
我有这个链接 该页面包含: $(document).on('turbolinks:load', function(){ // some correct javascript here });
我对当前默认的 turbolinks 推荐加载事件有问题: document.addEventListener('turbolinks:load', function() { … }); 发生的事
我是一名优秀的程序员,十分优秀!