简体   繁体   English

在Flow Router上使用URL查询参数进行搜索

[英]Search with the URL Query Parameters on Flow Router

I use Search Source , and Flow Router from Arunoda. 我使用Arunoda的Search SourceFlow Router They both work great, but I'm just struggling to get them work together. 他们俩都很棒,但我只是在努力使他们在一起。

I have a helper that returns some documents rendered from the server-run SearchSource method like this: 我有一个帮助程序,它返回从服务器运行的SearchSource方法呈现的一些文档,如下所示:

Template.search.helpers({
  things: function() {
    var currentUserId = Meteor.userId();
    var langParam = FlowRouter.current().queryParams.lang;
    console.log(langParam);
    return BookSearch.getData({
      sort: {date_added: -1}
    });
  }
});

As you see, I'm just trying to search for things that are registered in the language entered at the URL (eg 'en') as a query parameter. 如您所见,我只是尝试搜索以URL输入的语言(例如“ en”)作为查询参数注册的内容。 Let's say in "english" as in this example: 让我们在这个例子中用“英语”说:

http://localhost:3000/search?lang=en

I can perfectly read the "en" and log on the console by the below code, but it does not work to search with. 我可以完美地阅读“ en”,并通过以下代码登录控制台,但无法使用它进行搜索。 I mean because of this code: 我的意思是因为这段代码:

var langParam = FlowRouter.current().queryParams.lang;
console.log(langParam);

I get "en" printed on the browser console. 我在浏览器控制台上打印了“ zh”。 But I don't get the things that are registered in "en" language. 但是我没有用“ en”语言注册的东西。

So how can I achieve a proper search using the query parameters? 那么如何使用查询参数实现正确的搜索?

What I need to know is how to enter in the helper to render only data that fetches to the condition I want (in this case, english language - {lang: langParam}. For that one uses the Package.getData() API, but I could not locate exactly how. 我需要知道的是如何输入帮助程序,以仅呈现可获取所需条件的数据(在这种情况下,为英语-{lang:langParam}。为此,它使用Package.getData() API,但是我找不到确切的位置。

First of all, searchsource sets up necessary data delivery for you so you don't have to, indeed should not set up publications or subscriptions for your search flow. 首先,searchsource为您设置了必要的数据传递,因此您不必,实际上也不必为搜索流设置发布或订阅。 There's tons of literature around for how pub/sub works in Meteor so I'll skip ahead to your searchsource problem. 关于流星中发布/订阅的工作方式,有大量文献,所以我将跳过您的搜索源问题。

I see that you want to scope your search to a certain language. 我看到您想将搜索范围限制为某种语言。 Here's a basic set up that would get you going. 这是一个基本设置,可以助您一臂之力。 You should also fine tune things like throttling, metadata handling, limiting, paging, input and query param sanitization, result transformations etc. 您还应该微调节流,元数据处理,限制,分页,输入和查询参数清理,结果转换等。

Template 模板

<template name="booksearch">
  <form name="booksearch"><input type="search"/></form>
  <ul>
    {{#each hits}}
      <li>{{title}}</li>
    {{#each}}
  </ul>
</template>

Client: set up your helper 客户:设置您的助手

var options = {
  // cache the search results for 5 minutes
  keepHistory: 1000 * 60 * 5,
  // allow fast local searches on the cache
  localSearch: true
};
// feed the search to the title field only
var fields = ['title'];
// Set up your search
BookSearch = new SearchSource('books', fields, options);

/*
  get the search results reactively. mind you, this is not an invocation.
  you'll invoke the search within your event handler down below
*/
Template.booksearch.helpers({
  hits : function() {
    return BookSearch.getData();
  }
})

Template.booksearch.events({
  'submit form': function(e,t) {
    // listen for the submit event
    e.preventDefault();
    var options = {
      // this is your lang query param from the url
      lang: FlowRouter.getQueryParam('lang')
    };
    // value of the search input from your template
    var searchText = t.$('input').val();
    // invoke the search using the input and the language
    BookSearch.search(searchText,options);
  }
})

Server: set up your search 服务器:设置您的搜索

SearchSource.defineSource('books', function(searchText, options) {
  // make sure you do have a lang option or use a default one
  var lang = options.lang || 'english'
  if(searchText) {
    var regExp = buildRegExp(searchText);
    // use the input and lang to build your mongodb selector
    var selector = {title: regExp, language: lang};
    return Books.find(selector).fetch();
  } else {
    // don't return anything if nothing is searched for
    return [];
  }
});

function buildRegExp(searchText) {
  // copied over from the naive github example
  var parts = searchText.trim().split(/[ \-\:]+/);
  return new RegExp("(" + parts.join('|') + ")", "ig");
}

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

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