2014-07-08 48 views
1

我在名爲「map」的模板中的每個頁面上都有一個傳單地圖的應用程序。 在該圖中,我在「Template.map.rendered」函數中添加了一個contextmenu。Meteor.js - 如何在登錄時重新渲染模板

凡變得棘手,是我想要添加在文本菜單斷開鏈接和一個配置文件鏈接,當用戶登錄,但不是當用戶不在。 即使沒有連接,地圖也在那裏。

我的問題,現在的問題是,當我登錄或應用程序的註銷我的地圖是不是重新呈現。 我嘗試了幾個我在谷歌上找到的解決方案,但似乎沒有任何工作,我在這裏有點失落。

這是我的第一個流星應用。

代碼:

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 

以及登錄是標準的 「帳戶基」 與 「帳戶的用戶界面的自舉-3」

編輯:啊,我用玉來代替火焰如果改變的東西

回答

2

由於Meteor.loggingIn()只會在短時間內爲true,並且只能在該窗口中呈現模板才能顯示菜單項,所以很可能您的代碼存在爭用條件。此外,正如您發現的那樣,用戶註銷後不會再運行。

我不知道你的地圖插件能夠做什麼,但假設它具有添加/刪除功能,你可以嘗試在你的渲染函數中使用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(...); 
    } 
    }); 
}; 

的想法是,它會創建一個將運行每當用戶登錄或退出反應計算。在任何情況下,您都可以根據需要更新地圖菜單。

+0

對於這個問題的答案以及感謝,如果不是溶液,然後在它至少回答很多問題,我有,像在哪裏使用自動運行。 Leaflet contextmenu插件沒有添加和刪除項目功能,所以我會這樣做。我一開始想這樣做,但不知道如何在流星中做到這一點。 – jfmmm

+0

完美無瑕,再次感謝! – jfmmm

0

設置會話變量CA lled「logged」,默認情況下爲false,並在登錄時將其設置爲true。然後在您想要在會話更改時重新呈現的任何模板中添加「Session.get(」logged「), 無論何時使用」Session.get「在模板內流星會創建依賴關係,並在檢測到依賴關係發生變化時重新渲染模板。

,如果你不想使用會話變量爲此,您可以在其中用於創建依賴於「的DEP」流星文檔閱讀。