Mon*_*Boo 3 javascript django google-maps coding-style django-templates
我正在实施Google Maps API,我希望第一个标记的InfoWindow在首次呈现模板时打开,但仅在某个条件为真时才打开.
我有这样的事情:
{% if project %}
//the following is automatically open the infowindow of the FIRST marker in the array when rendering the template
var infowindow = new google.maps.InfoWindow({
maxWidth:500
});
infowindow.setContent(markers[0].html);
infowindow.open(map, markers[0]);
{% endif %}
Run Code Online (Sandbox Code Playgroud)
这并不能扔在Firefox或Internet Explorer 7的错误; 它做我想要的 - 但它只是看错了.我的文本编辑器尖叫着警告/错误.
这是不好的编码练习吗?如果是这样,对替代方案的任何建议?
这是完整的代码,在脚本标记内,编辑出不相关的位:
function initialize() {
...
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var markers = []
setMarkers(map, projects, locations, markers);
...
}
function setMarkers(map, projects, locations, markers) {
for (var i = 0; i < projects.length; i++) {
var project = projects[i];
var address = new google.maps.LatLng(locations[i][0],locations[i][1]);
var marker = new google.maps.Marker({
map: map,
position:address,
title:project[0],
html: description
});
markers[i] = marker;
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(this.html);
infowindow.open(map,this);
});
}
{% if project %}
//the following is automatically open the infowindow of the FIRST marker in the array when rendering the template
var infowindow = new google.maps.InfoWindow({
maxWidth:500
});
infowindow.setContent(markers[0].html);
infowindow.open(map, markers[0]);
{% endif %}
})
}
google.maps.event.addDomListener(window, 'load', initialize);
Run Code Online (Sandbox Code Playgroud)
在一堆Javascript中使用Django模板标签没有任何问题.Django的模板语言在很大程度上与语言无关:它不关心模板化文本的含义.
我有Javascript的河流,有大量的标签,条件,变量替换等等.
这种事情的另一个选择是插入一个Javascript布尔变量,并将条件放在Javascript中:
<script>
var is_project = {% if project %}true{% else %}false{% endif %};
//...
if (is_project) {
// stuff for project
}
</script>
Run Code Online (Sandbox Code Playgroud)
我想这可以让Javascript变得更干净.你必须根据自己的代码决定你喜欢哪种风格.
| 归档时间: |
|
| 查看次数: |
2877 次 |
| 最近记录: |