Javascript Meteor.js-如何在登录时重新提交模板

Javascript Meteor.js-如何在登录时重新提交模板,javascript,node.js,login,meteor,leaflet,Javascript,Node.js,Login,Meteor,Leaflet,我有一个应用程序,每个页面上都有一个名为“地图”的模板。 在该映射中,我在“Template.map.rendered”函数中添加了一个contextmenu 棘手的是,当用户登录时,我想在该上下文菜单中添加一个断开连接链接和一个配置文件链接,但当用户未登录时,我不想添加。 即使没有连接,地图也在那里 我现在的问题是,当我登录或注销应用程序时,我的地图不会被重新命名。 我尝试了一些在谷歌上找到的解决方案,但似乎没有任何效果,我在这里有点迷茫 这是我的第一个流星应用程序 代码: Template.

我有一个应用程序,每个页面上都有一个名为“地图”的模板。 在该映射中,我在“Template.map.rendered”函数中添加了一个contextmenu

棘手的是,当用户登录时,我想在该上下文菜单中添加一个断开连接链接和一个配置文件链接,但当用户未登录时,我不想添加。 即使没有连接,地图也在那里

我现在的问题是,当我登录或注销应用程序时,我的地图不会被重新命名。 我尝试了一些在谷歌上找到的解决方案,但似乎没有任何效果,我在这里有点迷茫

这是我的第一个流星应用程序

代码:

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”的标准“account base”


编辑:啊,我使用的是Jade而不是Blaze,如果这改变了什么,请设置一个名为“logged”的会话变量,默认情况下该变量为false,并在登录时将其设置为true。然后将“Session.get”(“logged”)添加到您希望在该会话更改时重新提交的任何模板中, 无论何时在模板内使用“Session.get”,Meteor都会创建依赖项,并在检测到依赖项已更改时重新启动模板


如果您不想为此使用会话变量,您可以在“Deps”上的Meteor文档中读取,该文档用于创建依赖项。

您的代码可能存在争用条件,因为
Meteor.loggin()
将仅在短时间内为真,并且只有在该窗口中呈现模板才能显示菜单项。此外,正如您所发现的,当用户注销时,模板不会再次运行

我不知道你的地图插件能做什么,但假设它有添加/删除功能,你可以尝试使用渲染函数的内部,而不是上面的
if(Meteor.logging())
代码。试试下面的方法:

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(...);
    }
  });
};

这个想法是,它将创建一个反应式计算,在用户登录或注销时运行。在每种情况下,您都可以根据需要更新地图的菜单。

谢谢您的回答,如果这不是解决方案,至少它回答了我的许多问题,比如在哪里使用自动运行。传单contextmenu插件确实有一个add和remove item函数,所以我会这样做。一开始我想这样做,但在meteor中不知道怎么做。