您还可以将所有收藏放在/lib/collection.js 路由中(以获取更好的做法)。
因此,我们确保meteor首先加载集合,并且它们将在客户端/服务器上都可用。
您应该删除 Autopublish/insecure 包,以避免流星在加载时发送所有集合并控制谁可以插入/删除/更新集合。
meteor remove autopublish
meteor remove insecure.
所以一个简单的集合看起来像这样。
//lib/collection.js
Example = new Mongo.Collection("Example") //we create collection global
if(Meteor.isClient) {
Meteor.subscribe('Example') //we subscribe both after meteor loads client and server folders
}
现在/server/collections.js
Meteor.publish('Example', function(){
return Example.find(); //here you can control whatever you want to send to the client, you can change the return to just return Example.find({}, {fields: {stuff: 1}});
});
// 这里我们控制集合的安全性。
Example.allow({
insert: function(userId, doc) {
if(Meteor.userId()){
return true; //if the user is connected he can insert
} else{
return false// not connected no insert
}
},
update: function(userId, doc, fields, modifier) { //other validation },
remove: function(userId, doc) { //other validation },
});
只是尝试更深入地解释一下关于流星的Collection,希望它对您有所帮助