我navigator.geolocation.watchPosition在JavaScript中使用,我想要一种方法来处理用户在watchPosition找到位置之前提交依赖于位置的表单的可能性.
理想情况下,用户会定期看到"等待位置"消息,直到获得该位置,然后表单将提交.
但是,由于缺少wait函数,我不确定如何在JavaScript中实现它.
当前代码:
var current_latlng = null;
function gpsSuccess(pos){
//console.log('gpsSuccess');
if (pos.coords) {
lat = pos.coords.latitude;
lng = pos.coords.longitude;
}
else {
lat = pos.latitude;
lng = pos.longitude;
}
current_latlng = new google.maps.LatLng(lat, lng);
}
watchId = navigator.geolocation.watchPosition(gpsSuccess,
gpsFail, {timeout:5000, maximumAge: 300000});
$('#route-form').submit(function(event) {
// User submits form, we need their location...
while(current_location==null) {
toastMessage('Waiting for your location...');
wait(500); // What should I use instead?
}
// Continue with location found...
});
Run Code Online (Sandbox Code Playgroud) 我在Bitbucket上分了一个回购,这是一个错误,我想删除分叉.
而不是经历将其合并回来的麻烦(因为我没有做任何改动会产生误导),我只想删除它.
我不能在Bitbucket中看到前端选项,但必须有一个......对吗?
我正在使用此示例动态构建一些Django过滤器查询:
kwargs = { 'deleted_datetime__isnull': True }
args = ( Q( title__icontains = 'Foo' ) | Q( title__icontains = 'Bar' ) )
entries = Entry.objects.filter( *args, **kwargs )
Run Code Online (Sandbox Code Playgroud)
我只是不确定如何构建条目args.说我有这个数组:
strings = ['Foo', 'Bar']
Run Code Online (Sandbox Code Playgroud)
我如何从那里到:
args = ( Q( title__icontains = 'Foo' ) | Q( title__icontains = 'Bar' )
Run Code Online (Sandbox Code Playgroud)
我能得到的最接近的是:
for s in strings:
q_construct = Q( title__icontains = %s) % s
args.append(s)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何设置|条件.
我正在尝试使用这个非常基本的代码从本地JSON文件将一些数据加载到Backbone Collection中:
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students();
AllStudents.fetch({ url: "/init.json"});
console.log('AllStudents', AllStudents);
Run Code Online (Sandbox Code Playgroud)
在控制台语句中,AllStudents为空.但init.json肯定是被装载.它看起来像这样:
[
{ text: "Amy", grade: 5 },
{ text: "Angeline", grade: 26 },
{ text: "Anna", grade: 55 }
]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
更新:我也尝试reset在.fetch()调用之上添加一个监听器,但是它没有触发:
AllStudents.bind("reset", function() {
alert('hello world');
});
AllStudents.fetch({ url: "/init.json"});
Run Code Online (Sandbox Code Playgroud)
没有警报出现.
更新2:尝试此脚本(完整转载):
$(function(){
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students(); …Run Code Online (Sandbox Code Playgroud) 我试图找出如何通过绑定新数据更新一些D3.js元素.我不确定这是否可能,但感觉应该是这样.
首先,我创建了四个SVG圈,并将cx偏移量设置为数据的函数:
<body><div id="container"></div></body>
var svg = d3.select("div.container").append("svg")
.attr("class", "chart")
.attr("width", 1000)
.attr("height", 500);
// Create initial data and display
var data = [0, 10, 20, 30];
var circle = svg.selectAll("circle")
.data(data)
.enter()
.append('circle')
.attr("cx", function(d) {
return d*10;
})
.attr("cy", 100)
.attr("r", 10)
.style("fill", "steelblue");
Run Code Online (Sandbox Code Playgroud)
接下来,我附加新数据和转换.我希望看到圆圈慢慢移动到新位置(这就是我想要实现的目标),但他们没有:
var data1 = [40, 50, 60, 70];
circle.data(data1).transition().duration(2500);
Run Code Online (Sandbox Code Playgroud)
我是否犯了一个基本错误?也许我选错了.或者仅仅通过操纵数据来更新元素是不可能的?
更新:如果我这样做,console.log(circle)那么我会看到一组SVG圈元素,这正是我所期望的.
是否可以组合Underscore的过滤器和地图?我目前有两个独立的函数调用,但我想知道我是否可以通过将它们组合成一个单独的调用来提高它们的效率.
基本上我有一系列国家/地区名称 - 我想使用正则表达式过滤它们,然后将过滤后的结果映射到DataItem对象数组.这是我目前的代码:
var filteredData = _.filter(allCountries, function(n, i){
var re = RegExp("^" + searchString, "i");
if (re.exec(n['country'].toLowerCase()) !== null) {
return true;
}
});
var mappedData = _.map(filteredData, function(n, i){
return new DataItem(i, n['name'], n['budget']);
});
Run Code Online (Sandbox Code Playgroud)
还将非常感激地提出任何其他提高效率的建议.
我正在构建一个离线Web应用程序,并希望使用缓存清单.目前我的缓存清单看起来像这样:
CACHE MANIFEST
# Change the version number below each time we update a resource.
# Rev 1
index.html
photo.html
js/photo.js
css/photo.css
http://code.jquery.com/jquery-1.6.1.min.js
http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js
http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css
http://maps.google.com/maps/api/js?sensor=false&region=GB
Run Code Online (Sandbox Code Playgroud)
有没有理由不在缓存清单中包含外部的,CDN托管的jQuery,jQuery Mobile和Google Maps文件?
我想不到一个,但我想我会问那些比我自己更聪明:)
我在HTML页面上引用JavaScript如下:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=GB"></script>
<script type="text/javascript" src="js/shared.js"></script>
<script type="text/javascript">
$('document').ready(function() {
// In-page code: call some functions in shared.js
});
</script>
Run Code Online (Sandbox Code Playgroud)
shared.js中定义的函数不包含在内部$('document').ready.所以:
假设定义的函数shared.js可用于"页内代码"是否安全?
如果我将页内代码拉出到一个名为local.js(保持包装$('document').ready)的单独文件中,是否可以安全地假设定义的函数shared.js可用?
最后,事实是我没有在$('document').ready一个问题中包装shared.js 吗?我发现如果我将它包装起来,它的功能将不再适用于页内代码.
问题3的原因是我遇到了这个问题:Uncaught TypeError:Property ...不是函数 - 页面加载后
并想知道这是否与我如何组织我的代码有关.
更新:谢谢你的答案.现在很清楚,$('document').ready在shared.js 中使用会从全局范围中删除这些函数.但是,我只想澄清第3点中的原始问题.
如果我执行以下操作,我可以假设:
$('document').ready,从shared.js调用一个函数会不会有问题?
换句话说,shared.js即使我没有将该文件中的所有内容包装在里面,是否可以安全地假设页面将在调用内部函数时加载$('document').ready?
我正在使用Google网络字体,如下所示:
@font-face {
font-family: "Vollkorn";
font-style: normal;
font-weight: normal;
src: local('Vollkorn Regular'), local('Vollkorn-Regular'), url('http://themes.googleusercontent.com/static/fonts/vollkorn/v2/BCFBp4rt5gxxFrX6F12DKnYhjbSpvc47ee6xR_80Hnw.woff') format('woff');
}
body {
font-family: "Vollkorn", Georgia, Times, serif;
}
Run Code Online (Sandbox Code Playgroud)
在Chrome中工作,没有"闪烁的无格式文本"(如此Typekit博客文章中所述).相反,在Web字体下载完成之前,文本根本不会加载.
通过快速连接,它很棒,因为字体异步加载非常快.但是,通过缓慢的连接,页面看起来像是空的几秒钟,直到Web字体加载 - 这是可用性差.
有没有一种聪明的方法可以在佐治亚州显示文本,然后在资源加载后添加Vollkorn字体?
我想我所说的是,我实际上非常喜欢"无格式文本的闪光",而不是空白页面,并希望强制执行此行为.