【发布时间】:2015-10-17 03:38:42
【问题描述】:
我正在使用 Easy-Search 软件包并想搜索帖子(或这些帖子的 cmets)。
问题:输入搜索时没有显示任何内容。console.log 和服务器上都没有显示错误消息。
更新: 我在发布和订阅上都做了 console.log。因此,订阅在浏览器 devtools 上返回 console.log,但发布在服务器终端上没有返回任何内容。
模板 html
<template name="searchPosts">
{{> esInput id="main" index=indexes placeholder="Search.." }}
{{#esEach index="posts"}}
{{> postItem}}
{{/esEach}}
{{#esEach index="comments"}}
{{> postItem}}
{{/esEach}}
</template>
模板 js - 客户端
Template.searchPosts.onCreated(function () {
var self = this, instance;
instance = EasySearch.getComponentInstance(
{ id: 'main', index: 'posts'},
{ id: 'main', index: 'comments'}
);
instance.on('searchingDone', function (searchingIsDone) {
searchingIsDone && console.log('I am done!');
});
instance.on('currentValue', function (val) {
console.log('The user searches for ' + val);
});
this.subscribe("allDocs");
});
Template.searchPosts.helpers({
indexes : function () {
return ['posts', 'comments'];
},
posts: function () {
return Posts.find();
}
});
【问题讨论】:
-
你用的是铁路由器吗?你忘记订阅路线了吗??
-
请把订阅放在
Route.route。它已经解决了我的问题几次它应该类似于Router.route('/allDocs', { name: 'allDocs', subscriptions: function () { this.subscribe('allDocs'); }}) -
Router.route('/allDocs', { name: 'allDocs', subscriptions: function () { this.subscribe('allDocs'); }}) -
那我需要更换助手吗?有 1 个发布/订阅帖子,但我的搜索包括
Posts和Comments收藏?posts: function () { return Posts.find(); }. -
否。您不必更改助手。 Subsription 将根据不在模板上的路由激活。例如。您可以在 2 条不同的路线上使用相同的模板,并且根据路线订阅,您将获得模板上的数据。助手查询结果将始终是发布查询结果的子集(在您的情况下与没有查询参数相同)。
标签: javascript meteor publish-subscribe