Last active
September 20, 2019 10:57
-
-
Save aaronthorp/06b67c171fde6d1ef317 to your computer and use it in GitHub Desktop.
Meteor observe subscription example
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
Meteor.publish('usersImages',function(groupName){ | |
var collectionName = "user_images"; | |
var subscription = this; | |
group = Groups.findOne({name:groupName}); | |
if(!group) { | |
subscription.stop(); | |
} else { | |
var query = {}; | |
query['roles._' + group._id] = {$exists:true}; | |
query['profile.imageFile'] = {$exists:true}; | |
var userHandle = Meteor.users.find(query).observe({ | |
added: function(newDoc) { | |
var docId = newDoc.profile.imageFile; | |
var doc = UserImages.findOne({_id: docId}); | |
subscription.added(collectionName, docId, doc); | |
}, | |
removed: function(oldDoc) { | |
var docId = oldDoc.profile.imageFile; | |
subscription.removed(collectionName, docId); | |
}, | |
changed: function(newDoc, oldDoc) { | |
var docId = newDoc.profile.imageFile; | |
if (docId !== oldDoc.profile.imageFile) { | |
subscription.removed(collectionName, oldDoc.profile.imageFile); | |
var doc = UserImages.findOne({_id: docId}); | |
subscription.added(collectionName, docId, doc); | |
} | |
} | |
}); | |
subscription.ready(); | |
} | |
// turn off observe when client unsubscribes | |
subscription.onStop(function () { | |
userHandle.stop(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment