2013-01-25 62 views
0

的問題一個集合的數據集是我需要一個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")}); 

該流星合併一個集合的兩個數據集的主要問題的兩個不同的數據集。

在模板中,我有兩個不同的演示文稿的一個集合。收藏品很大(6000個文檔),我無法將其全部發送給客戶。

如何在不將所有文檔發送到客戶端的情況下獲得一個集合的兩組不同文檔?

回答

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 
+0

非常感謝你! –