2016-02-25 53 views
0

您好:我正在使用教程todo。
我一直在试图流星测试将输入值等于文档字段

<input type="text" name="text" placeholder="Type Or Scan to add new Name" /> 

比较的输入值命名为一个名为“任务”文档中的“文本”的领域。
已尝试EasySearch和{{#if $eq a b}} ... {{ /if }}
可能因为我是Meteor新手,未正确设置<templates>
希望有一个简短的模板或助手来比较或检查值。

回答

0

你可以使用reactive variable来做到这一点。

下面是一些示例代码:

JS:

Template.yourTemplateName.onCreated(function() { 
    this.input = new ReactiveVar(""); // Declare the reactive variable. 
}); 

Template.yourTemplateName.helpers({ 
    tasks() { 
     return Tasks.find(); // Find all tasks as an example. 
    }, 
    isInputEqualToTaskText(taskId) { 
     var task = Tasks.findOne({ _id: taskId }); 
     if(task && task.text) { 
      return task.text == Template.instance().input.get(); 
     } 
    } 
}); 

Template.yourTemplateName.events({ 
    'change input': function(event, template) { 
     template.input.set(event.target.value); 
    } 
}); 

HTML:

<template name="yourTemplateName"> 
    {{#each task in tasks}} 
     {{#if isInputEqualToTaskText task._id}} 
      <p>I was equal: {{task.text}}</p> 
     {{/if}} 
    {{/each}} 
    <input type="text"> 
</template>