2016-05-06 7 views
0

複数のチャットルームでアプリケーションを作成しようとしています。現時点では部屋の1つがメッセージを送受信して動作します。しかし、私がルーティングしてテンプレートを変更し、別の部屋でメッセージを送信しようとすると、メッセージは送信されませんが、上部のリンクは変更されますか?すなわち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 &#x25BE;</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> 

答えて

0

あなたのテンプレートはbodyと呼ばれていませんが、javaとなっていますので、イベントを添付するにはTemplate.java.helpersで追加する必要があります。

Template.java.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); 
} 
}); 

helpersと同じことです。

+0

第2の部屋は、第1の部屋と同じメッセージを送受信していて、あなたの助言に従って何かを戻しています。これは、各部屋の新しいメッセージのコレクションを作る必要があるのですか? – Kaneo71

+0

いいえ、1つのコレクションを持つことはできますが、ユーザー名のように何とかソートする必要がありますか? 'xyz'でチャットした場合、この人に何らかの形で接続されているアイテムだけを返すべきです。おそらくcollection:message、date、およびusernameに挿入して、 'username ===あなたがチャットした相手のアイテムを並べ替えるべきです。 – Sindis

関連する問題