我正在使用谷歌地图API,每当我从codeLatLng函数返回变量到初始化函数时,它声称未定义.如果我从codeLatLng打印变量,它显示正常.
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(40.730885,-73.997383);
var addr = codeLatLng();
document.write(addr);
}
function codeLatLng() {
var latlng = new google.maps.LatLng(40.730885,-73.997383);
if (geocoder) {
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
return results[1].formatted_address;
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
打印出undefined
如果我做:
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
var …Run Code Online (Sandbox Code Playgroud) 视频有很多活动
我正在努力获取将来安排活动的所有视频.
我已经有了这个:
named_scope :scheduled_in_future, :joins => :event, :conditions => ["event.scheduled_start > ? AND event.status = ?", Time.now.to_i, 'PENDING']
Run Code Online (Sandbox Code Playgroud)
这是有效的,但如果同一个视频将来有多个事件,它将给我重复的视频记录.当然,我可以通过数组并清除重复项,但必须有一种SQL方法来完成它.
我试过添加一个
:select => "DISTINCT(video.id)"
Run Code Online (Sandbox Code Playgroud)
但它只返回ID字段而不是整个记录.
在我的Rails代码中,我需要确认只有在剩余某个记录超过1时才允许操作.出于这个原因,我需要锁定更新,然后执行读取.我的rails代码如下所示:
PaymentProfile.transaction do
profiles = PaymentProfile.lock("LOCK IN SHARE MODE").where(user_id: xxx)
if profiles.count > 1
#allow
else
#do not allow
end
end
Run Code Online (Sandbox Code Playgroud)
从理论上讲,这很有效并且可以正确锁定行.但是,如果另一个请求遍历相同的代码路径,则打开事务会删除我在另一个进程中取出的锁,从而破坏了锁的目的.
来自MySQL文档:
Beginning a transaction also causes table locks acquired with LOCK TABLES to be released, as though you had executed UNLOCK TABLES. Beginning a transaction does not release a global read lock acquired with FLUSH TABLES WITH READ LOCK.
Run Code Online (Sandbox Code Playgroud) 在我的rails应用程序上,我实现了AuthLogic和CanCan.然而,当试图弄清楚用户是否可以通过CanCan来管理文章(通过article.user_id检查他是否是所有者)时,我遇到了问题.这应该是直截了当我不知道我做错了什么.
用户has_many文章
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
can :manage, Article do |article|
article.user_id == user.id
end
end
end
Run Code Online (Sandbox Code Playgroud)
转到/ articles/index时出现以下错误
undefined method `user' for #<Array:0x1035c5158>
Extracted source (around line #15):
12: <td><%= article.created_at %></td>
13: <td><%= article.user.login %></td>
14: <td><%= link_to 'View', article %></td>
15: <% if can? :manage, article %>
16: <td><%= link_to 'Edit', edit_article_path(article) %></td>
17: <% end %>
18: <% if can? :manage, article %>
app/models/ability.rb:9:in `try'
app/models/ability.rb:9:in `initialize'
cancan …Run Code Online (Sandbox Code Playgroud) $('#div1_button').click(function() {
$('#div0').fadeOut(function(){
$('#div1').fadeIn();
});
});
Run Code Online (Sandbox Code Playgroud)
当用户单击 div1_button 时,先前选择的 div0 淡出,div1 淡入。如果用户疯狂单击并在 div1 完成淡入之前单击 div2,则 div2 开始淡入,最终 div1 淡出,但它们堆叠在每个顶部其他直到 div1 完成淡入然后淡出。如何停止 .click() 事件,直到单击的 div 完成淡入。
因为它是固定在浏览器窗口的固定位置不为我用的情况下工作,您可以在状态得到其中的文字是关闭屏幕的右侧,你不能得到它.无论如何,我试图使用绝对定位然后调整javascript中的"顶部".它在firefox和Chrome中运行得非常好,但在Safari中,滚动时内容会抖动.
<div class="fixed sticky" data-offset-top="50"><p>fixed</p></div>
$(document).ready(function() {
var documentHeight = $(document).height();
$(document).scroll(function() {
var scrollTop = $(window).scrollTop();
$(".sticky").offset(function() {
$this = $(this);
var offsetTop = $this.data("offset-top");
if (scrollTop < 0) {
scrollTop = 0;
}
var newTop = offsetTop + scrollTop;
if (newTop < offsetTop) {
newTop = offsetTop;
}
// Prevents document from infinitely expanding.
var maxTop = documentHeight - $this.height();
if (newTop > maxTop) {
newTop = maxTop
}
// Prevents a bit of jitter since the …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails3与jQuery并尝试做简单的ajax调用.我有一个链接,显示应用程序的当前状态(在线/离线).单击时,它将更新状态.
link_to app.status, { :controller => :apps, :action => :live_status, :id => app }, {:remote => true, :class => "#{app.status} status"}
Run Code Online (Sandbox Code Playgroud)
live_status.js.erb:
$(this).fadeOut();
$(this).parent().html("<%= escape_javascript(status_link(@wowza_app)) %>");
$(this).fadeIn();
Run Code Online (Sandbox Code Playgroud)
status_link将返回更新的link_to
但是$(这个)并不是我想象的那样.如何使用新状态更新点击状态?
javascript ×3
jquery ×3
activerecord ×1
ajax ×1
asynchronous ×1
cancan ×1
google-maps ×1
innodb ×1
locking ×1
mysql ×1
ruby ×1
safari ×1
scroll ×1
sql ×1
transactions ×1