- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
解决方案
我有两个 users.js
和 users.coffee
在我的 Assets 管道中。显然 users.coffee
正在阻止 users.js
从被加载。确保删除它!
我正在尝试实现 this ——github repo here .
开始聊天需要两个 JS 文件 -- chat.js
和 users.js
.两者都包括 $(document).ready(ready);
功能。只有我才能让Rails执行chat.js
我已通过添加 console.log("confirm");
进行确认就像这样在两个函数中...
var ready = function () {
console.log("success") ...
这两种我都试过了...
$(document).on("page:load", ready);
$(document).on("page:change", ready);
我尝试将其添加到 application.html.erb
==> <%= javascript_include_tag "users", "data-turbolinks-track" => true %>
我试过添加 //= require users
至 application.js
我试过将它添加到 config/initializer/assets.rb
==> Rails.application.config.assets.precompile += %w( users.js )
为什么不是 ready
在 users.js
中执行的函数?
我的代码库是 here .
这里是 chat.js
...
/**
* Chat logic
*
* Most of the js functionality is inspired from anatgarg.com
* jQuery tag Module from the tutorial
* http://anantgarg.com/2009/05/13/gmail-facebook-style-jquery-chat/
*
*/
var chatboxFocus = new Array();
var chatBoxes = new Array();
var ready = function () {
console.log("success1");
chatBox = {
// console.log("success");
/**
* creates an inline chatbox on the page by calling the
* createChatBox function passing along the unique conversation_id
*
* @param conversation_id
*/
chatWith: function (conversation_id) {
console.log("success1");
chatBox.createChatBox(conversation_id);
$("#chatbox_" + conversation_id + " .chatboxtextarea").focus();
},
/**
* closes the chatbox by essentially hiding it from the page
*
* @param conversation_id
*/
close: function (conversation_id) {
$('#chatbox_' + conversation_id).css('display', 'none');
chatBox.restructure();
},
/**
* Plays a notification sound when a new chat message arrives
*/
notify: function () {
var audioplayer = $('#chatAudio')[0];
audioplayer.play();
},
/**
* Handles 'smart layouts' of the chatboxes. Like when new chatboxes are
* added or removed from the view, it restructures them so that they appear
* neatly aligned on the page
*/
restructure: function () {
align = 0;
for (x in chatBoxes) {
chatbox_id = chatBoxes[x];
if ($("#chatbox_" + chatbox_id).css('display') != 'none') {
if (align == 0) {
$("#chatbox_" + chatbox_id).css('right', '20px');
} else {
width = (align) * (280 + 7) + 20;
$("#chatbox_" + chatbox_id).css('right', width + 'px');
}
align++;
}
}
},
/**
* Takes in two parameters. It is responsible for fetching the specific conversation's
* html page and appending it to the body of our home page e.g if conversation_id = 1
*
* $.get("conversations/1, function(data){
* // rest of the logic here
* }, "html")
*
* @param conversation_id
* @param minimizeChatBox
*/
createChatBox: function (conversation_id, minimizeChatBox) {
console.log("success2");
if ($("#chatbox_" + conversation_id).length > 0) {
if ($("#chatbox_" + conversation_id).css('display') == 'none') {
$("#chatbox_" + conversation_id).css('display', 'block');
chatBox.restructure();
}
$("#chatbox_" + conversation_id + " .chatboxtextarea").focus();
return;
}
$("body").append('<div id="chatbox_' + conversation_id + '" class="chatbox"></div>')
$.get("conversations/" + conversation_id, function (data) {
$('#chatbox_' + conversation_id).html(data);
$("#chatbox_" + conversation_id + " .chatboxcontent").scrollTop($("#chatbox_" + conversation_id + " .chatboxcontent")[0].scrollHeight);
}, "html");
$("#chatbox_" + conversation_id).css('bottom', '0px');
chatBoxeslength = 0;
for (x in chatBoxes) {
if ($("#chatbox_" + chatBoxes[x]).css('display') != 'none') {
chatBoxeslength++;
}
}
if (chatBoxeslength == 0) {
$("#chatbox_" + conversation_id).css('right', '20px');
} else {
width = (chatBoxeslength) * (280 + 7) + 20;
$("#chatbox_" + conversation_id).css('right', width + 'px');
}
chatBoxes.push(conversation_id);
if (minimizeChatBox == 1) {
minimizedChatBoxes = new Array();
if ($.cookie('chatbox_minimized')) {
minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
}
minimize = 0;
for (j = 0; j < minimizedChatBoxes.length; j++) {
if (minimizedChatBoxes[j] == conversation_id) {
minimize = 1;
}
}
if (minimize == 1) {
$('#chatbox_' + conversation_id + ' .chatboxcontent').css('display', 'none');
$('#chatbox_' + conversation_id + ' .chatboxinput').css('display', 'none');
}
}
chatboxFocus[conversation_id] = false;
$("#chatbox_" + conversation_id + " .chatboxtextarea").blur(function () {
chatboxFocus[conversation_id] = false;
$("#chatbox_" + conversation_id + " .chatboxtextarea").removeClass('chatboxtextareaselected');
}).focus(function () {
chatboxFocus[conversation_id] = true;
$('#chatbox_' + conversation_id + ' .chatboxhead').removeClass('chatboxblink');
$("#chatbox_" + conversation_id + " .chatboxtextarea").addClass('chatboxtextareaselected');
});
$("#chatbox_" + conversation_id).click(function () {
if ($('#chatbox_' + conversation_id + ' .chatboxcontent').css('display') != 'none') {
$("#chatbox_" + conversation_id + " .chatboxtextarea").focus();
}
});
$("#chatbox_" + conversation_id).show();
},
/**
* Responsible for listening to the keypresses when chatting. If the Enter button is pressed,
* we submit our conversation form to our rails app
*
* @param event
* @param chatboxtextarea
* @param conversation_id
*/
checkInputKey: function (event, chatboxtextarea, conversation_id) {
if (event.keyCode == 13 && event.shiftKey == 0) {
event.preventDefault();
message = chatboxtextarea.val();
message = message.replace(/^\s+|\s+$/g, "");
if (message != '') {
$('#conversation_form_' + conversation_id).submit();
$(chatboxtextarea).val('');
$(chatboxtextarea).focus();
$(chatboxtextarea).css('height', '44px');
}
}
var adjustedHeight = chatboxtextarea.clientHeight;
var maxHeight = 94;
if (maxHeight > adjustedHeight) {
adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight);
if (maxHeight)
adjustedHeight = Math.min(maxHeight, adjustedHeight);
if (adjustedHeight > chatboxtextarea.clientHeight)
$(chatboxtextarea).css('height', adjustedHeight + 8 + 'px');
} else {
$(chatboxtextarea).css('overflow', 'auto');
}
},
/**
* Responsible for handling the growth of chatboxes as they increase on the page
* Keeps track of the minimized chatboxes etc
*
* @param conversation_id
*/
toggleChatBoxGrowth: function (conversation_id) {
if ($('#chatbox_' + conversation_id + ' .chatboxcontent').css('display') == 'none') {
var minimizedChatBoxes = new Array();
if ($.cookie('chatbox_minimized')) {
minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
}
var newCookie = '';
for (i = 0; i < minimizedChatBoxes.length; i++) {
if (minimizedChatBoxes[i] != conversation_id) {
newCookie += conversation_id + '|';
}
}
newCookie = newCookie.slice(0, -1)
$.cookie('chatbox_minimized', newCookie);
$('#chatbox_' + conversation_id + ' .chatboxcontent').css('display', 'block');
$('#chatbox_' + conversation_id + ' .chatboxinput').css('display', 'block');
$("#chatbox_" + conversation_id + " .chatboxcontent").scrollTop($("#chatbox_" + conversation_id + " .chatboxcontent")[0].scrollHeight);
} else {
var newCookie = conversation_id;
if ($.cookie('chatbox_minimized')) {
newCookie += '|' + $.cookie('chatbox_minimized');
}
$.cookie('chatbox_minimized', newCookie);
$('#chatbox_' + conversation_id + ' .chatboxcontent').css('display', 'none');
$('#chatbox_' + conversation_id + ' .chatboxinput').css('display', 'none');
}
}
}
/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
}
$(document).ready(ready);
$(document).on("page:load", ready);
这里是 users.js
...
var ready = function () {
console.log("success");
/**
* When the send message link on our home page is clicked
* send an ajax request to our rails app with the sender_id and
* recipient_id
*/
$('.start-conversation').click(function (e) {
e.preventDefault();
console.log("success");
var sender_id = $(this).data('sid');
var recipient_id = $(this).data('rip');
$.post("/conversations", { sender_id: sender_id, recipient_id: recipient_id }, function (data) {
chatBox.chatWith(data.conversation_id);
});
});
/**
* Used to minimize the chatbox
*/
$(document).on('click', '.toggleChatBox', function (e) {
e.preventDefault();
console.log("success1");
var id = $(this).data('cid');
chatBox.toggleChatBoxGrowth(id);
});
/**
* Used to close the chatbox
*/
$(document).on('click', '.closeChat', function (e) {
e.preventDefault();
console.log("success2");
var id = $(this).data('cid');
chatBox.close(id);
});
/**
* Listen on keypress' in our chat textarea and call the
* chatInputKey in chat.js for inspection
*/
$(document).on('keydown', '.chatboxtextarea', function (event) {
console.log("success3");
var id = $(this).data('cid');
chatBox.checkInputKey(event, $(this), id);
});
/**
* When a conversation link is clicked show up the respective
* conversation chatbox
*/
$('a.conversation').click(function (e) {
e.preventDefault();
console.log("success4");
var conversation_id = $(this).data('cid');
chatBox.chatWith(conversation_id);
});
}
$(document).ready(ready);
$(document).on("page:load", ready);
application.js
看起来像这样……
//= require jquery
//= require jquery_ujs
//= require transition.js
//= require alert.js
//= require button.js
//= require carousel.js
//= require collapse.js
//= require dropdown.js
//= require modal.js
//= require tooltip.js
//= require scrollspy.js
//= require tab.js
//= require bootstrap.min
//= require private_pub
//= require chat
//= require turbolinks
//= require_tree .
我在 <head>
里面有这个application.html.erb
的标签
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag '//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<%= javascript_include_tag "users", "data-turbolinks-track" => true %>
哦,这是 HTML 页面(所有代码请参见 repo)
<div class="row">
<!-- Not implemented on tutorial -->
<div class="col-md-4">
<div style="height: 300px; overflow-y: auto;">
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Registered Users</div>
<!-- Table -->
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each_with_index do |user, index| %>
<tr>
<td><%= index +=1 %></td>
<td><%= user.email %></td>
<td>
<%= link_to "Send Message", "#", class: "btn btn-success btn-xs start-conversation",
"data-sid" => current_user.id, "data-rip" => user.id %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<hr>
<h3>Your Conversations</h3>
<div style="height: 400px; overflow-y: auto;">
<% if @conversations.any? %>
<ul class="media-list">
<% @conversations.each do |conversation| %>
<li class="media">
<%= image_tag("http://placehold.it/50x50", class: "media-object pull-left") %>
<div class="media-body">
<%= link_to "", conversation_path(conversation), class: "conversation", "data-cid" => conversation.id %>
<h4 class="media-heading"><%= conversation_interlocutor(conversation).email %></h4>
<p><%= conversation.messages.last.nil? ? "No messages" : truncate(conversation.messages.last.body, length: 45) %></p>
</div>
</li>
<% end %>
</ul>
<% else %>
<p>You have no conversations. Click send message with any user to create one.</p>
<% end %>
</div>
</div>
<div class="col-md-4">
</div>
</div>
最佳答案
关于javascript - Rails 4 不执行 $(document).ready(ready);功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38447565/
根据 jQueryDoc在术语中,.ready() 被称为位于 jQuery.prototype 中的查询选择方法。对于前 $(document).ready() 作为jQueryDoc说:$ 命名空
我见过一些代码,他们只是这样做: $().ready(function() { ... }); 这比执行文档选择器要短,但它是同一件事吗? 最佳答案 略有变化: $(document).rea
各个处理程序的回调函数何时执行? 最佳答案 在主要过程中 当您的应用完成初始化并准备打开浏览器窗口时,将触发app上的“就绪”事件。由于在此之前您无法打开窗口,因此可以使用回调函数来创建Browser
最近发现了head.js图书馆和男孩,我对此感到满意,尽管我仍然对一件事感到有点困惑。 来自 headjs.com: The “DOM ready” event such as $(document)
我在我的代码库中发现代码在另一个 $(document).ready(function() { ... 例如 $(document).ready(function() { // 20
我已经养成了从ready函数开始jquery编码的习惯 $(function(){...}); 并将所有从ready调用的函数放入ready中。 然后我意识到,放入就绪函数中的一些函数可能不需要在那里
在浏览旧代码库时,我发现了以前的软件开发人员正在使用的地方 $(function(a) {}(A || (A = {}))); 即使页面尚未准备好,它仍在执行。在我删除传递的全局变量后它开始工作。代码
我已经看到了两种方式,但哪种更好或者并不重要。 我觉得包装每个语句可能会更干净,但只是想知道如果您有 50 个语句,每个语句都有自己的 document.ready 事件处理程序,是否会有更多回调?
这个问题已经有答案了: Four variations of jQuery ready() -- what's the difference? (4 个回答) 已关闭 4 年前。 我正在编写一些我没有
我们最近遇到了一个错误,jquery document.ready 似乎在 DOM 加载之前触发。事实证明,在调用的第一个项目之后有一些错误的代码 $.ready(function(){}); 这条语
什么应该在 jQuery.ready() 中,什么应该在 jQuery.ready() 之外? 从性能角度来看,我在某处读到,将所有代码包装在 jQuery.ready() 中并不是一种有效的方法。
我实现了一个带有选项列表(工作类别)的页面,单击该页面时应显示数据(工作描述)。我正在使用 BBQ 来处理后台堆栈。 一切正常,除了在用户首次导航到页面时设置初始选择。我的代码被调用到 addClas
解决方案 我有两个 users.js和 users.coffee在我的 Assets 管道中。显然 users.coffee正在阻止 users.js从被加载。确保删除它! 我正在尝试实现 this
我正在研究一个指令,但我不想玩 $document或 $window , 只有 element本身。 之前我有: angular.element($document).ready(function()
我想在移动设备方向改变时使用 $(window).resize 来调用一些函数,我在 $(document).ready 中编写了所有代码,这在我使用 Android 设备时有效,但在使用 iPhon
我有一个使用数据库的 Ionic 应用程序。我想用一个文件的内容填充这个数据库。 这部分我开始工作了。我想创建一个 DB.ready() 事件,很像 $ionicPlatform.ready() 或
我有一个名为“loadTimeTrackersGrid()”的函数,它加载一个弹性网格。设置如下所示: $(document).ready(function () { var edi
我有一个定义数量为 replicas 的部署.如果我的 Pod 准备好/未准备好处理新连接,我使用就绪探针进行通信 - 我的 Pod 在 ready 之间切换/not ready他们一生中的状态。 我
有什么区别: $(document).ready(initialize); 和 $(document).on('ready', initialize); 对我来说,它们似乎以相同的方式工作。 最佳答案
我看到很多项目都在使用 $(document).on('ready', function(e){ //jquery stuff }) 而不是: $( document ).ready(functio
我是一名优秀的程序员,十分优秀!