从搜索中检索显示结果

时间:2014-12-16 10:17:25

标签: javascript node.js meteor iron-router

我对meteor.js相对较新,我试图让搜索表单起作用。到目前为止,我甚至都没有试图让params工作,但它会在以后发生。

我基本上试图让一堆电梯显示出来。

LIB / router.js

Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading',
  notFoundTemplate: 'notFound',
  waitOn: function() {
    return Meteor.subscribe('lifts');
  }
});

Router.route('/', { name: 'liftsList' });

Router.route('/lifts/search/:from-:to-:when', {
  name: 'liftsSearch',
  waitOn: function() {
    return Meteor.subscribe('liftsSearch');
  }
});

服务器/ publications.js

Meteor.publish('liftsSearch', function() {
  var query = { fromLoc: { $near : { 
    $geometry: { 
      type : "Point" ,
      coordinates: [ 6.11667, 45.9 ]
    } },
    $maxDistance : 50
  }};

  return Lifts.find(query);
});

如果我尝试使用Lifts.find(query).fetch()显示结果,则返回实际结果。

的客户机/ lifts_search.html

<template name="liftsSearch">
  <div class="container">
    <h3>Lifts search results {{hi}}</h3>
    <div class="lifts">
      {{#each lifts}}
        hi
        {{> liftItem}}
      {{/each}}
    </div>
  </div>
</template>

在这里,我根本没有电梯显示,甚至没有显示小电话&#34; hi&#34;字符串。

由于

1 个答案:

答案 0 :(得分:1)

除非您没有提供代码,否则{{#each lifts}}无法呈现,因为您没有在任何地方定义lifts。仅仅因为你正在填充Lifts集合,模板就不会自动知道lifts引用它(主要是因为那将是完全随意的 - 它会引用什么确切的查询?)。 / p>

因此,您需要在路由器data function中定义lifts

Router.route('/lifts/search/:from-:to-:when', {
  name: 'liftsSearch',
  waitOn: function() {
    return Meteor.subscribe('liftsSearch');
  },
  data: function() {
    return {
      lifts: Lifts.find() // add a query if you want
    }
  }
});

template helper

Template.liftsSearch.helpers({
  lifts: function() {
    return Lifts.find(); // add a query if you want
  }
});
相关问题