我的应用程序在名为" map"的模板中的每个页面上都有一个传单地图。 在该地图中,我在" Template.map.rendered"中添加了一个上下文菜单。功能
如果它变得棘手,我想在用户登录时在该上下文菜单中添加断开链接和配置文件链接,而不是在用户不登录时添加。 即使你没有连接,地图就在那里。
我现在的问题是,当我登录或退出应用程序时,我的地图不会被重新呈现。 我尝试了一些我在google上找到的解决方案,但似乎没有任何效果,我在这里有点迷失。
这是我的第一个流星应用程序。
代码:
Template.map.rendered = function(){
L.Icon.Default.imagePath = 'packages/leaflet/images';
var map = L.map('map', {
doubleClickZoom: false,
contextmenu: true,
contextmenuWidth: 160,
contextmenuItems: [{
text: 'Show coordinates',
callback: function(event){
console.log(event);
},
icon: 'images/icons/mini-map-pin.png'
}]
}).setView([Session.get('mapLatitude'), Session.get('mapLongitude')], Session.get('mapZoom'));
map.on('dragend zoomend', function(event){
//map position and zoom are saved in session on every action so they
//stay the same when the template is rerendered
Session.set("mapLatitude", map.getCenter().lat);
Session.set("mapLongitude", map.getCenter().lng);
Session.set("mapZoom", map.getZoom());
});
if( Meteor.loggingIn() ){
map.contextmenu.addItem('-');
map.contextmenu.addItem({
text: 'My profile',
callback: function(event){
console.log(event);
},
icon: 'images/icons/profile.png'
});
map.contextmenu.addItem({
text: 'Disconnect',
callback: function(event){
console.log(event);
},
icon: 'images/icons/logout.png'
});
}
L.tileLayer.provider('OpenStreetMap.BlackAndWhite').addTo(map);
}
地图模板就是这个
template(name="map")
div#map
并且登录是标准"帐户基础"使用" accounts-ui-bootstrap-3"
编辑:啊,如果改变了什么,我会使用Jade代替Blaze
答案 0 :(得分:2)
您的代码可能会出现竞争条件,因为Meteor.loggingIn()
只会在短时间内生效,并且必须仅在该窗口中呈现模板才能使菜单项出现。此外,正如您所发现的那样,用户注销后不会再次运行。
我不知道您的地图插件能够做什么,但假设它具有添加/删除功能,您可以尝试在渲染函数中使用autorun而不是if( Meteor.loggingIn() )
上面的代码。尝试这样的东西:
Template.map.rendered = function() {
// create the map for all users
var map = ...;
// replace the if( Meteor.loggingIn() ) section with this code
this.autorun(function() {
if (Meteor.userId()) {
// add code here to add menu items
map.contextmenu.addItem(...);
} else {
// add code here to remove menu items
map.contextmenu.removeItem(...);
}
});
};
这个想法是它会创建一个反应计算,只要用户登录或注销就会运行。在每种情况下,您都可以根据需要更新地图的菜单。
答案 1 :(得分:0)
设置一个名为"记录的会话变量"默认情况下为false,并在登录时将其设置为true。然后在此会话更改时,在要重新呈现的任何模板中添加" Session.get("记录"), 每当你使用" Session.get"在模板内部,Meteor会在检测到依赖项发生更改时创建依赖项并重新呈现模板。
如果你不想为此使用会话变量,你可以阅读关于" Deps"的Meteor文档。用于创建依赖关系。