简体   繁体   English

显示在Meteor中的模板事件中做出的搜索结果

[英]display search results made in Template events in Meteor

I have a meteor project in which, with 3 radio buttons, I can choose different variables of my search; 我有一个流星项目,其中有3个单选按钮,可以选择搜索的不同变量; then, with a click on the button submit of the form, the query to do in mongo is created. 然后,单击表单提交按钮,创建在mongo中执行的查询。

This happens in events: 这发生在以下事件中:

//global var
var resultCursor;
...

Template.device.events({
    click .btn-search": function (event) {
    event.preventDefault();

    [...]

    resultsCURSOR = myCollection.find(query, queryOptions);
})

Now, I have to display the results in the page with an helper. 现在,我必须在页面上显示一个帮助程序。 I tried saving the cursor in a global var, and pass it to the helper (I cannot save in a Session var, as described here - it doesn't work) 我试图保存光标在一个全局变量,并将其传递到助手(我不能在保存Session变种,如描述在这里 -它不工作)

Template.device.helpers({
    searchResults: function() {
    return resultCursor;
    }
})

And in html 和在HTML

{{#each flightSearchResults}}
    <div class="flight-list">
        <p>Pilot: {{this.pilot}}</p>
        <p>Time: {{this.date}}</p>
    </div>
{{/each}}

But this does not work. 但这是行不通的。 I found a kind of solution here but I have to move the entire query to helpers and before doing this... is this the only solution? 我在这里找到了一种解决方案但我必须将整个查询移至辅助程序,然后再执行此操作……这是唯一的解决方案吗? And is that a good practice solution? 那是一个好的实践解决方案吗?

Put the query (not the cursor) in a Session variable, like this: 将查询(而不是游标)放在Session变量中,如下所示:

Template.device.events({
  click .btn-search": function (event) {
  event.preventDefault();

  [...]

  Session.set('query', query);
})

Change your helper (note that the name was wrong): 更改您的助手(请注意,名称错误):

Template.device.helpers({
  flightSearchResults: function() {
    return myCollection.find(Session.get('query'), queryOptions);
  }
});

You don't need to change your html, and you can now remove all occurrences of resultCursor. 您无需更改html,现在可以删除所有出现的resultCursor。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM