在meteor.js中使用相同的模板和不同的数据

时间:2013-10-29 09:07:44

标签: javascript templates meteor

我有一个模板在我的流星应用程序中显示地图。此模板用于两件事:

  • 在地图上显示我的应用的搜索结果
  • 用户选择一个
  • 后,显示特定场地的个别位置

目前我的代码如下(我没有关于此模板的其他功能或帮助):

Template.map.rendered = function() {
if(!map)
    var map = new Course_map('#map');

Deps.autorun(function(){

    if (Session.get("current_place")){
        var places = Places.find(Session.get("current_place")).fetch();
    }
    else{
        var places_cursor =  get_searched_places();
        if (places_cursor){
            var places = places_cursor[0].fetch();
            var places_id = _.pluck(places,'_id');

            //Remove the markers that are no longer in the places_id array
            map.remove_markers(places_id);
        }
    }

    if(places){
        // Add all the marker in places
        for(var i = 0, places_length = places.length; i < places_length; i ++){
            map.add_marker(places[i]);
        }
        // Recenter the map to focus on the marker area
        map.calc_bounds();
    }
});
};

我的地图模板:

<template name="map">
<div id="map" class='google-map'>
</div>

根据用户是否选择了具有Session.get(“current_place”)的地方,我会在我的places变量中加载不同的信息。

我真的不喜欢那种设计,我想我可以找到一种更好的方法来做我想做的事情。你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

看起来您遇到的问题之一是如何将Meteor与外部地图API集成。虽然迫切需要这样做,但Meteor实际上提供了一种更简单的方法来处理cursor#observeChanges()函数,其中addedchangedremoved回调可以只需直接连接到地图API。

我实际上已经使用Meteor和OpenLayers实现了一个映射应用程序。向下滚动到最后:

  

https://github.com/mizzao/CrowdMapper/blob/master/client/views/map.coffee