Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为包定义的模板设置数据上下文_Javascript_Meteor - Fatal编程技术网

Javascript 为包定义的模板设置数据上下文

Javascript 为包定义的模板设置数据上下文,javascript,meteor,Javascript,Meteor,以下是相关的代码模板代码: <!-- display a list of users --> <template name="available_user_list"> <h2 class="cha_heading">Choose someone to chat with:</h2> <div class="row"> {{#each users}} {{> availabl

以下是相关的代码模板代码:

<!-- display a list of users -->
<template name="available_user_list">
    <h2 class="cha_heading">Choose someone to chat with:</h2>
    <div class="row">
        {{#each users}}
            {{> available_user}}
        {{/each}}
    </div>
</template>

<!-- display an individual user -->
<template name="available_user">
    <div class="col-md-2">
        <div class="user_avatar">
            {{#if isMyUser _id}} 
            <div class="bg-success">
                {{> avatar user=this shape="circle"}}
                <div class="user_name">{{getUsername _id}} (YOU)</div>
            </div>
            {{else}}
            <a href="/chat/{{_id}}">
                {{> avatar user=this shape="circle"}}
                <div class="user_name">{{getUsername _id}}</div>
            </a>
            {{/if}}
        </div>
    </div>
</template>

<template name="chat_page">
    <h2>Type in the box below to send a message!</h2>
    <div class="row">
        <div class="col-md-12">
            <div class="well well-lg">
            {{#each messages}}
                {{> chat_message}}
            {{/each}}
            </div>  
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <form class="js-send-chat">
                <input class="input" type="text" name="chat" placeholder="type a message here...">
                <button class="btn btn-default">send</button>
            </form>
        </div>
    </div>
</template>

<!-- simple template that displays a message -->
<template name="chat_message">
    <div class="chat-message">
    {{> avatar user=user size="small" shape="circle"}} <div class="chat-center">{{user}}: {{text}}</div>
    </div>
</template>
我正在制作一个网站,用户可以登录并相互聊天。我已经下载了实用程序:avatar包来显示用户的化身。化身图像基于用户用户名的第一个首字母。当我在模板
available\u user
中使用代码
{{>avatar user=this shape=“circle”}
呈现化身模板时,它会很好地显示化身中的首字母,因为它与用户集合的上下文有关

我还想在用户发送消息时显示化身,但模板
chat_message
在chats集合中数组的数据上下文中。因此,它只显示默认的化身,而不显示首字母


没有完全指定如何为
user
userId
设置模板参数。有人能帮我解决这个问题吗?

我知道了。我在
userId
字段下的
messages
数组中将用户id包含到每个对象中,并在模板中将其设置为
{{>avatar userId=userId size=“small”shape=“circle”}

Template.available_user_list.helpers({
  users:function(){
    return Meteor.users.find();
  }
})
Template.available_user.helpers({
  getUsername:function(userId){
    user = Meteor.users.findOne({_id:userId});
    return user.username;
  }, 
  isMyUser:function(userId){
    if (userId == Meteor.userId()){
      return true;
    }
    else {
      return false;
    }
  }
})


Template.chat_page.helpers({
  messages:function(){
    var chat = Chats.findOne({_id:Session.get("chatId")});
    return chat.messages;
  }, 
  other_user:function(){
    return ""
  }, 

})

Template.chat_message.helpers({
  user: Meteor.user()
})