【问题标题】:Use two datasets of one collection in Meteor在 Meteor 中使用一个集合的两个数据集
【发布时间】:2013-01-08 23:07:56
【问题描述】:

问题是我需要一个 mongodb 集合的两个不同数据集

//lib/col.js
Photos = new Meteor.Collection("photos");
lastPhotos = function() {
  return Photos.find({},{sort:{created_time:-1},limit:4});
}
locationPhotos = function(location_id_var) 
{
    //**doesn't return data from server, goes to local cache**
    return Photos.find({location_id: location_id_var});
}
//server
Meteor.publish("lastPhoto", function () 
{
   return lastPhoto();
});

Meteor.publish("locationPhoto", function (location_id) 
{
   return locationPhoto(location_id);
});

//client
Meteor.subscribe("lastPhoto");
Meteor.subscribe("locationPhoto",{location_id:Session.get("location_id")});

meteor 合并一个集合的两个数据集的主要问题。

在模板中,我对一个集合有两种不同的展示方式。收藏量很大(6000 份文件),我无法将其全部发送给客户。

如何在不将所有文档发送给客户端的情况下获取一个集合的两组不同文档?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    你在正确的轨道上。您需要将您的subscribe 包装在autorun 中,以便动态更改服务器的publish 方法。

    我希望这会有所帮助:

    Javascript:

    var Photos = new Meteor.Collection("Photos");
    
    if (Meteor.isClient) {
    
      Template.main.photos = function () {
        return Photos.find({location_id: Session.get("location") });
      };
    
      Meteor.autorun(function() {
        Meteor.subscribe("photos", Session.get("location"));
      });
    
      Template.main.events({
        'click button' : function (e) {
            Session.set("location", $(e.target).data("locale"));
        }
      });
    }
    
    if (Meteor.isServer) {
      Meteor.publish("photos", function(location_id) {
        return Photos.find({location_id: location_id}, {sort: { created_time: -1 }, limit: 4});
      });
    }
    

    HTML:

    <body>
      {{> main}}
    </body>
    
    <template name="main">
        {{#each photos}}
            {{location_id}} | {{created_time}} | {{name}} <br/>
         {{/each}}
    
        <hr/>
    
        <button type="button" data-locale="MSP">MSP</button>
        &nbsp;
        <button type="button" data-locale="SEA">SEA</button>
    
    </template
    

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      相关资源
      最近更新 更多