【问题标题】:Why is helper called multiple times in this scenario?为什么在这种情况下会多次调用助手?
【发布时间】:2015-08-28 00:17:24
【问题描述】:

我有一个模板:

<template name="addform">
{{#if editmode}}
<form class="form-inline phonebook-edit" role="form">
  <div class="form-group">
    <label for="name">Name:</label>
    <input type="text" class="form-control" id="name" value="{{datatoedit.name}}" name="personname">
  </div>
  <div class="form-group">
    <label for="pwd">Phone:</label>
    <input type="text" class="form-control" value="{{datatoedit.phone}}" name="phoneno" id="pwd">
    <input type="hidden" value="{{datatoedit._id}}" name="id">
  </div>
  <button type="submit" class="btn btn-success">Edit</button>
</form>
{{else}}
<form class="form-inline phonebook-add" role="form">
  <div class="form-group">
    <label for="name">Name:</label>
    <input type="text" class="form-control" id="name" name="personname">
  </div>
  <div class="form-group">
    <label for="pwd">Phone:</label>
    <input type="text" class="form-control" name="phoneno" id="pwd">
  </div>
  <button type="submit" class="btn btn-success">Add</button>
</form>
{{/if}}
</template>

还有一个助手:

Template.addform.helpers({

  'editmode':function(){
    return Session.get('editmode');  
  },
'datatoedit': function(){
        if(Session.get('idtoedit')) {
            var phonebookdata = Phonebook.findOne({"_id":Session.get('idtoedit')});
            console.log(phonebookdata);
            return phonebookdata;
        } else {
            return "world";
        }
        //return Session.get('idtoedit');
    }    

  });

Editmode 是一个标志,可以切换添加/编辑表单,效果很好。

datatoedit 是我们使用放置在会话中的 _id 从 mongodb 获取的数据。

在模板中,我不得不在 3 个不同的地方调用这个数据来编辑,以便在每个文本框中输入值。

我的问题是我如何只让流星调用 mongodb 一次并获取我需要将它们放入文本框中的所有数据。

我放置了一个console.log,它打印相同的数据。这让我想知道为什么流星每次都会去 mongo 打印姓名、电话和 _id 值

我是流星新手。请向我建议一种正确的方法来构建编辑表单。我希望调用一次 mongodb 并发送那些我应该将它们放入编辑文本框中的数据。

http://phonebook.meteor.com/ 打开控制台选项卡并点击不同行中的编辑,您将看到上述问题。

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    这是一个可以解决问题的模式:

    <template name="addform">
    {{#if editmode}}
    <form class="form-inline phonebook-edit" role="form">
      <div class="form-group">
        <label for="name">Name:</label>
        {{#with datatoedit}}
          <input type="text" class="form-control" id="name" value="{{this.name}}" name="personname">
        </div>
        <div class="form-group">
          <label for="pwd">Phone:</label>
          <input type="text" class="form-control" value="{{this.phone}}" name="phoneno" id="pwd">
          <input type="hidden" value="{{this._id}}" name="id">
        {{/with}}
      </div>
      <button type="submit" class="btn btn-success">Edit</button>
    </form>
    {{else}}
    <form class="form-inline phonebook-add" role="form">
      <div class="form-group">
        <label for="name">Name:</label>
        <input type="text" class="form-control" id="name" name="personname">
      </div>
      <div class="form-group">
        <label for="pwd">Phone:</label>
        <input type="text" class="form-control" name="phoneno" id="pwd">
      </div>
      <button type="submit" class="btn btn-success">Add</button>
    </form>
    {{/if}}
    </template>
    

    基本上{{#with datatoedit}} 将html 块的数据上下文设置为this 一次,然后您将访问this 对象的3 个键。

    【讨论】:

    • 。是的,这应该工作。这是一个最小的流星垫示例meteorpad.com/pad/duHXPWC4kPGB74gz5/WithExample。如果您编写“fieldName”而不是“this.fieldName”,它也可以工作。但也许使用“this”更清楚,它提醒读者自定义上下文。
    猜你喜欢
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多