【发布时间】:2012-11-23 10:24:49
【问题描述】:
我有以下模板:
<template name="test">
{{#isolate}}
<div id="map_canvas" style="width:50%; height:50%"></div>
{{/isolate}}
</template>
在我的 test.js 中(来自 https://developers.google.com/maps/documentation/javascript/tutorial#HelloWorld):
function initialize(){
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map($("#map_canvas")[0], mapOptions);
}
Template.test.rendered = function(){
initialize();
//*** The following is the workaround I am using:***
// if (! this.initialized){
// initialize();
// this.initialized = true;
// }
};
问题是:如果没有注释代码部分中显示的解决方法,模板总是被渲染两次(因为 initialize() 运行)。它既显示在控制台日志中(此处未显示记录代码),也可以从闪烁一次的地图中看到(这是不可接受的)。
原因,我猜,是因为发生了以下事件:
- 模板被渲染,
$('#map_canvas')生成为一个简单的 div 元素(未附加地图); - google map api返回aync-ly并修改
$('#map_canvas'); - Meteor 不知何故检测到更改,尽管有
{{#isolate}},但仍决定再次渲染整个模板(这显示在日志中); - 在
Template.test.rendered内再次调用initialize()。
我的问题:
- 为什么?
- 如果发生这种情况,为什么 Meteor 只渲染两次,而不是无限次?
- 解决方案?
3 个问题,非常感谢!
【问题讨论】:
-
您好,您是如何设法以正确的顺序包含您的 google maps api 外部文件的?我把它放在 但是当我调用它时它抱怨它不存在..