Created
December 12, 2013 03:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, it looks really good. It smells like a new meteor pattern ;)