【问题标题】:Meteor user search log流星用户搜索日志
【发布时间】:2014-08-07 22:56:08
【问题描述】:

对于一个流星应用程序,我想记录用户的搜索并让每个用户看到他们以前的搜索,包括日期和时间。

目前,我将此工作搜索与 bootstrap-3 一起使用。

<template name="search">
  <div class="input-group" style="margin:1em 1em 0 1em;">
    <input type="text" class="form-control" style="padding:1em; font-size: 120%; color:black; padding:0em 1em 0em 1em; height:2em; border: 1px solid #aaa; border-radius: 0;" placeholder="Start searching" id="search"/>
    <span class="input-group-addon">
      <span class="glyphicon glyphicon-search" style="cursor:pointer;"></span>
    </span> 
  </div>
</template>

现在我想将用户搜索存储在我假设的集合中,例如

Searches = new Meteor.Collection("Searches");

并且只允许每个用户查看他们之前的搜索。我在 html 中添加了 accounts-ui 和 accounts-password 以及 {{>loginButtons}}。

如何保存搜索(最好是日期和时间)并将它们显示给用户(在额外的页面上)?在这种情况下如何限制用户的视图?

【问题讨论】:

    标签: twitter-bootstrap search logging login meteor


    【解决方案1】:

    您可以为搜索历史创建出版物。

    服务器

    // will publish all searches for a user, could also sort by date and limit
    Meteor.publish('searchHistory', function () {
      return Searches.find({userId: this.userId});
    });
    

    您还应该设置 Searches 集合的权限。 http://docs.meteor.com/#allow

    客户

    // get my search history
    Meteor.subscribe('searchHistory');
    
    // insert a new search document when enter key is pressed
    Template.search.events({
      'keydown #search': function (e, t) {
        if (e.keyCode === 13) {
          Searches.insert({
            userId: Meteor.userId(), 
            value: t.$('#search').val(),
            date: Date.now()
          });
        }
      }
    });
    

    编辑:这是一个简单的搜索历史模板。

    // sort searches by date for display
    Template.history.searches = function () {
      return Searches.find({}, {sort: {date: -1}});
    };
    
    <template name="history">
      {{#each searches}}<div>{{value}}</div>{{/each}}
    </template>
    

    【讨论】:

      猜你喜欢
      • 2017-08-20
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 2015-12-16
      • 1970-01-01
      相关资源
      最近更新 更多