Created
August 18, 2015 23:45
-
-
Save cmather/bbdc893bc2eb9e21451f to your computer and use it in GitHub Desktop.
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
Rooms = new Mongo.Collection('rooms'); | |
Messages = new Mongo.Collection('messages'); | |
if (Meteor.isClient) { | |
Meteor.subscribe('rooms', function () { | |
var room = Rooms.findOne(); | |
Meteor.subscribe('messages', room._id); | |
}); | |
} | |
if (Meteor.isServer) { | |
Meteor.publish('rooms', function () { | |
return Rooms.find(); | |
}); | |
Meteor.publish('messages', function (roomId) { | |
var cursor = Rooms.find({_id: roomId}); | |
var self = this; | |
var publishMessages = function (roomId) { | |
Messages.find({roomId: roomId})._publishCursor(self); | |
}; | |
cursor.observeChanges({ | |
added: function (roomId, room) { | |
if (room.isAccepted) | |
publishMessages(roomId); | |
}, | |
changed: function (roomId, room) { | |
if (room.isAccepted) { | |
publishMessages(roomId); | |
} | |
} | |
}); | |
this.ready(); | |
}); | |
if (Rooms.find().count() == 0) { | |
var roomId = Rooms.insert({name: 'My Room', isAccepted: false}); | |
Messages.remove({}); | |
Messages.insert({roomId: roomId, message: 'Hello'}); | |
Messages.insert({roomId: roomId, message: 'World'}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment