I’m just trying to build a simple chat application that has various rooms. Each message in my Messages collection has a room property, and obviously only messages matching the current room should be displayed.
First up, the relevant router code (FlowRouter w/ BlazeLayout):
So, whenever I click on a new room, the router kicks in and sets the proper Session variable. When this happens, I know the .autorun() function fires because I can see the console.log() statement being printed. The issue is, I know Meteor is reactive, and I was expecting the messages the chatArea to be reloaded according the currently set room. Instead, nothing is reloaded, so I can click around to a buch of different rooms (routes) and the same messages are displayed.
Not sure what the exact problem is, but seems like you’re relying on Session vars when you don’t really have to. Flow-Router offers reactivity out of the box, so you could do something like:
FlowRouter.route('/room/:room', {
name: 'room',
action: function (params) {
BlazeLayout.render('appLayout', { chatAreaTemplate: 'chatArea' });
}
});
Template.chatArea.onCreated(function () {
var self = this;
self.autorun(function () {
self.subscribe('messages', FlowRouter.getParam('room'));
});
});
Template.chatArea.helpers({
// You only really need to filter this by the room _id if you have multiple messages subscriptions
messages: Messages.find({}),
'fromCurrentUser': function (user) {
if (user._id === Meteor.userId()) {
return true;
} else {
return false;
}
}
});
No, this would just display all messages in every room, which is not what i want. If I then filter my .find() call like so:
Template.chatArea.helpers({
// You only really need to filter this by the room _id if you have multiple messages subscriptions
messages: Messages.find({room: FlowRouter.getParam('room')}),
'fromCurrentUser': function (user) {
if (user._id === Meteor.userId()) {
return true;
} else {
return false;
}
}
});