Skip to content

Instantly share code, notes, and snippets.

@copleykj
Created December 12, 2013 03:09
Show Gist options
  • Save copleykj/7922639 to your computer and use it in GitHub Desktop.
Save copleykj/7922639 to your computer and use it in GitHub Desktop.
Basic example of publishing one server side collection to 2 different client side collections.
//Available on Client and Server and is a managed collection
Vehicles = new Meteor.Collection('vehicles');
if(Meteor.isServer){
Meteor.publish('cars', function () {
var cursor = Vehicles.find({type:"car"});
var subscription = this;
var handle = cursor.observe({
added: function (document) {
subscription.added('cars', document._id, document);
},
changed: function (newDocument, oldDocument) {
subscription.changed('cars', newDocument._id, newDocument);
},
removed: function (oldDocument) {
subscription.removed('cars', oldDocument._id);
}
});
subscription.ready();
subscription.onStop(function(){
handle.stop()
});
});
Meteor.publish('trucks', function () {
var cursor = Vehicles.find({type:"truck"});
var subscription = this;
var handle = cursor.observe({
added: function (document) {
subscription.added('trucks', document._id, document);
},
changed: function (newDocument, oldDocument) {
subscription.changed('trucks', newDocument._id, newDocument);
},
removed: function (oldDocument) {
subscription.removed('trucks', oldDocument._id);
}
});
subscription.ready();
subscription.onStop(function(){
handle.stop()
});
});
}
if(Meteor.isClient){
//The 2 unmanaged collection on the client, you will not be able to write to these.
//Write to the vehicles collection directly instead
Cars = new Meteor.Collection('cars', {connection:null});
Trucks = new Meteor.Collection('trucks', {connection:null});
Meteor.subscribe('cars');
Meteor.subscribe('trucks');
}
Copy link

ghost commented Dec 12, 2013

Wow, it looks really good. It smells like a new meteor pattern ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment