0

我正在尝试回复评论选项,目前我有一个带有文本区域的输入字段,提交时会在屏幕上打印名称和评论。现在,我创建了第二个输入和文本区域,我希望稍后在该输入和文本区域中也打印到屏幕上,单击屏幕上的名称和评论旁边的回复按钮会弹出文本区域。但目前我想要工作的只是第二个输入和文本区域,用于在屏幕上打印任何内容。我基本上已经复制了以前的代码,只是更改了变量和函数名称,在它们之后添加了一个 1 现在它不起作用,它在我添加{{> message1}}到正文中的位置中断。页面上什至没有显示。这是现场网站http://tuts.meteor.com的链接

这是我正在使用的代码

HTML

<head>
  <title>tutorial</title>
</head>

<body>
 {{> messages}}
 {{> entry}}
 {{> entry1}}
</body>

<template name="messages">
    {{#each messages}}
        <!--echo of message template-->
        {{> message}}
    {{/each}}
</template>

<template name="message">
    <p><strong>{{name}}:</strong>{{message}}</p>
</template>

<template name="entry">
    <p><input type="text" id="name" placeholder="your name"></p>
    <p><textarea type="text" id="messageBox" placeholder="your message"></textarea></p>
    <p><input type="button" id="submit" value="submit"></p>
</template>

<template name="messages1">
    {{#each messages1}}
        <!--echo of message template-->
        {{> message1}}
        <a href="#">reply</a>
    {{/each}}

</template>

<template name="message1">
    <p><strong>{{name}}:</strong>{{message1}}</p>
</template>

<template name="entry1">
    <p><input type="text" id="name1" placeholder="your name"></p>
    <p><textarea type="text" id="messageBox1" placeholder="your message"></textarea></p>
    <input type="button" id="submit1" value="submit">
    </p>
</template>

JAVASCRIPT

Messages = new Meteor.Collection('messages');
Messages1 = new Meteor.Collection('messages1');

if (Meteor.is_client){

   ////////// Helpers for in-place editing //////////

  // Returns an event_map key for attaching "ok/cancel" events to
  // a text input (given by selector)
  var okcancel_events = function (selector) {
    return 'keyup '+selector+', keydown '+selector+', focusout '+selector;
  };

  // Creates an event handler for interpreting "escape", "return", and "blur"
  // on a text field and calling "ok" or "cancel" callbacks.
  var make_okcancel_handler = function (options) {
    var ok = options.ok || function () {};
    var cancel = options.cancel || function () {};

    return function (evt) {
      if (evt.type === "keydown" && evt.which === 27) {
        // escape = cancel
        cancel.call(this, evt);
      } else if (evt.type === "keyup" && evt.which === 13) {
        // blur/return/enter = ok/submit if non-empty
        var value = String(evt.target.value || "");
        if (value)
          ok.call(this, value, evt);
        else
          cancel.call(this, evt);
      }
    };
  };//added as test

    Template.entry.events = {};


  /*  Template.entry.events[okcancel_events('#messageBox')] = make_okcancel_handler({
      ok:function(text, event){
        var nameEntry = document.getElementById('name');
        if(nameEntry.value != ""){
          var ts = Date.now() / 1000;
          Messages.insert({name: nameEntry.value, message: text, time: ts});
          event.target.value = "";
        }//if statment ends
      }
    });
  */

  Template.entry.events['click #submit'] = function() {
      var nameEntry = document.getElementById('name');
      if(nameEntry.value != ""){
          var ts = Date.now() / 1000;
          Messages.insert({name: nameEntry.value, message: $('#messageBox').val(), time: ts});
     }
  }

  Template.entry.events['click #submit1'] = function() {
      var nameEntry1 = document.getElementById('name1');
      if(nameEntry1.value != ""){
          var ts = Date.now() / 1000;
          Messages1.insert({name: nameEntry1.value, message1: $('#messageBox1').val(), time: ts});
      }
  }

  Template.messages1.messages1 = function () {
    return Messages1.find({}, { sort: {time: -1} });
  };

  Template.messages.messages = function () {
    return Messages.find({}, { sort: {time: -1} });
  };
}
4

1 回答 1

1

您应该包含{{messages1}}而不是{{message1}},并且应该将事件作为函数调用而entry1不是entry

Template.entry1.events({'click #submit1' : function() {
    var nameEntry1 = document.getElementById('name1');
    if(nameEntry1.value != ""){
      var ts = Date.now() / 1000;
      Messages1.insert({name: nameEntry1.value, message1: $('#messageBox1').val(), time: ts});
    }
  }
});

如果将“messages1”重命名为“comments”,将“message1”重命名为“comment”,将“entry1”重命名为“enterComment”等等,也会更清楚。

<body>
 {{> messages}}
 {{> entry}}
 {{> enterComment}}
 {{> comments}}
</body>

然后JS变成:模板。enterComment .events({...})

http://tuts2.meteor.com/

于 2013-02-20T07:18:24.520 回答