Я пытаюсь создать приложение с несколькими чатами. В настоящий момент одна из комнат работает, отправляя и получая сообщения. Однако когда я маршрутизирую и меняю шаблон и пытаюсь отправить сообщение в другую комнату, сообщение не отправляется, но ссылка наверху меняется? i.e localhost: 3000/java? text = hi - Он говорит, что сообщение будет кэшировано.Метеорный чат - несколько комнат
Как я могу получать сообщения для отправки в других комнатах, хранить в коллекции и на дисплее? Вот мой код: Client.js
// This code only runs on the client
Meteor.subscribe("messages", {
onReady: function() {
scrollToBottom();
autoScrollingIsActive = true;
}
});
/* helper code */
Template.body.helpers({
recentMessages: function() {
return Messages.find({}, {sort: {createdAt: 1}});
},
/* unread message helper */
thereAreUnreadMessages: function() {
return thereAreUnreadMessages.get();
}
});
/*chat window scrolling*/
Template.message.onRendered(function() {
if (autoScrollingIsActive) {
scrollToBottom(250);
} else {
if (Meteor.user() && this.data.username !== Meteor.user().username) {
thereAreUnreadMessages.set(true);
}
}
});
Template.body.events({
"submit .new-message": function (event) {
var text = event.target.text.value;
Meteor.call("sendMessage", text);
scrollToBottom(250);
event.target.text.value = "";
event.preventDefault();
},
/* scroll event */
"scroll .message-window": function() {
var howClose = 80; // # pixels leeway to be considered "at Bottom"
var messageWindow = $(".message-window");
var scrollHeight = messageWindow.prop("scrollHeight");
var scrollBottom = messageWindow.prop("scrollTop") + messageWindow.height();
var atBottom = scrollBottom > (scrollHeight - howClose);
autoScrollingIsActive = atBottom ? true : false;
if (atBottom) { // <--new
thereAreUnreadMessages.set(false);
}
},
"click .more-messages": function() {
scrollToBottom(500);
thereAreUnreadMessages.set(false);
}
});
Template.footer.usercount = function() {
return Meteor.users.find().count();
};
Вот мой server.js
// This code only runs on the server
Meteor.publish("messages", function() {
return Messages.find();
});
// This code only runs on the server
Meteor.publish("messages_java", function() {
return MessagesJava.find();
});
А вот код шаблона для "Java комнаты":
<template name="java">
<!--<h6>This is the Java template</h6>-->
<!--<p>*Put chatroom here*</p>-->
<div class="container">
<header>
<h1>ITHub Java Room</h1>
{{> loginButtons}}
</header>
<!-- chat messages here -->
<!-- -->
<ul class="message-window">
{{#each recentMessagesJava}}
{{> message}}
{{/each}}
</ul>
<!-- more-messages button -->
{{#momentum plugin="fade"}}
{{#if thereAreUnreadMessages}}
<button class="more-messages">New Messages ▾</button>
{{/if}}
{{/momentum}}
<footer>
{{#if currentUser}}
<form class="new-message">
<input type="text" name="text" placeholder="Add a message" autocomplete="off" />
</form>
{{/if}}
<br />
</footer>
<hr />
<li class="message-java">
<div class="username">{{username}}</div>
<div class="message-text">{{messageText}}</div>
</li>
</div>
Прогресс, вторая комната отправляет и получает те же сообщения, что и в первой комнате, последовали за советом и что-то возвращают, значит ли это, что мне нужно создать новую коллекцию сообщений для каждой комнаты? – Kaneo71
Нет, у вас может быть одна коллекция, но вам нужно ее каким-то образом отсортировать, например, по имени пользователя? Если вы разговариваете с 'xyz', вы должны как-то вернуть только те элементы, которые связаны с этим человеком. Вероятно, вы должны вставить в коллекцию: сообщение, дату и имя пользователя, а затем отсортировать элементы «имя пользователя» = «люди, с которыми вы общаетесь». – Sindis