根据上下文限制每个内部

时间:2015-08-30 20:38:17

标签: meteor

无法尝试将某种上下文传递到我的小测试中.-

问题是我有线程,有回复。只显示线程效果很好,根据它们来自的线程显示回复线程。

问题是如何在外部发送上下文。非常感谢你的帮助!

meteorchan.html

<head>      
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>
  <h1>Meteor Chan - Let's do this</h1>
  <form class="new-thread">
    <input type="text" name="text" placeholder="New Thread" />
    <button>Submit</button> 
  </form>
  <br/>
  {{#each threads}}
    {{> thread}}
    {{#each replies}}    
      {{> reply}}
    {{/each}}  
  {{/each}} 
</body>

<template name="thread">
  <div class="thread">
    <span>Anonymous</span> No. {{iden}}
    <br>
    {{text}}
  </div>
  <a class="reply" href="#">Reply</a> : <button class="purge">Purge</button>
  <form class="new-reply {{this._id}}" style="display:none;">
    <input type="text" name="text" placeholder="Add Reply" />
    <input type="text" name="thread" value="{{this._id}}" style="display:none;" />
    <button>Add New Reply</button> 
  </form>
  <br>
  <br>  
</template>

<template name="reply">
  <div class="reply">
      {{text}}
      <br>
      {{parentThread}}
  </div>
</template>

meteorchan.js

Threads = new Mongo.Collection("threads"); 
Replies = new Mongo.Collection("replies"); 

if (Meteor.isClient) {  
  Meteor.subscribe("threads");  
  Template.body.helpers({
    threads: function () {
      return Threads.find({}, {sort: {createdAt: -1}});
    },
    replies: function () {
      return Replies.find({}, {sort: {createdAt: 1}});
    }
  });

  Template.body.events({
    "submit .new-thread": function (event) {
      event.preventDefault();
      var text = event.target.text.value;
      var currentId = 0;
      if(Threads.findOne({}, {sort: {createdAt: -1}})){
        currentId = Threads.findOne({}, {sort: {createdAt: -1}}).iden;
      }
      Threads.insert({
        text: text,
        createdAt: new Date(),
        iden: currentId + 1
      });
      event.target.text.value = "";
    },
    "submit .new-reply": function (event) {
      event.preventDefault();
      var text = event.target.text.value; 
      var thread = event.target.thread.value;     
      Replies.insert({
        text: text,
        createdAt: new Date(),
        parentThread: thread
      });
      event.target.text.value = "";
    }
  });  
  Template.thread.events({
    "click .purge": function (){
      Threads.remove(this._id);
    },
    "submit .new-reply": function(event){
      event.preventDefault();
    },
    "click .reply": function(){ 
      $('.'+this._id+'.new-reply').css('display','inherit');
    }    
  });    
}

if (Meteor.isServer) {
  Meteor.publish("threads", function () {
    return Threads.find();
  });

    Meteor.publish("replies", function () {
    return Replies.find();
  });
}

1 个答案:

答案 0 :(得分:0)

您可以使用this参考模板助手中的上下文。 因此,在replies帮助器this中引用当前使用每个线程迭代的线程。
您可以执行以下操作以仅获取相应线程的回复:

replies: function () {
  return Replies.find({
    // this refers to the current thread
    parentThread: this._id
  }, {sort: {createdAt: 1}});
}