【问题标题】:Use #each with argument in Meteor在 Meteor 中使用带有参数的 #each
【发布时间】:2015-11-09 13:18:29
【问题描述】:

我是 Meteor 框架的新手,我正在尝试使用流星 + mongo + 空格键显示一些数据。问题是我可能需要对空格键使用一个参数#each,但它不允许这样做。

我的代码:

$ 文件.js

  Template.home.helpers({
      places: function() {
          return Places.find();
      }
  });

  Template.content.helpers({
    images: function() {
        return Images.find({});
     }
  }); 

$ 文件.html

<template name="home">    
    {{#each places}}
        {{>content}}
    {{/each}}
</template>

<template name="content">
    <li>{{name}} - {{date}}</li>
    {{#each images}}
        <img src="{{this.url}}">    
    {{/each}}
</template>

我想要做的——但它没有用——是在 Template.content.helper 函数中使用一个参数,如下所示:

  Template.content.helpers({
    images: function(ARG) {
        return Images.find({place: ARG});
     }
  }); 

在 html 文件中会是

<template name="content">
    <li>{{name}} - {{date}}</li>
    {{#each images {{name}} }}
        <img src="{{this.url}}">    
    {{/each}}
</template>

你们知道如何正确显示信息吗?我不想显示每个地方的所有信息(图像)。我只想显示那个地方的图片通讯员...

提前致谢, 马库斯

【问题讨论】:

  • 你能在接收参数的助手中做一个console.log(Array.prototype.slice.call(arguments))吗?

标签: mongodb meteor meteor-blaze spacebars


【解决方案1】:

好的,我可以用下面的代码做我想做的事:

文件.html

<template name="content">
    <li>{{name}} - {{date}}</li>   
    {{#each images name}}
        <img src="{{this.url}}">    
    {{/each}}
</template>

(这意味着我必须删除双括号)

然后在file.js中

Template.content.helpers({
    images: function(location) {
        console.log(location);
        return Images.find({"metadata.location": location});
    }
  });

我希望它也对其他人有所帮助。

【讨论】:

    【解决方案2】:

    由于助手的实现可以像这样访问当前数据上下文,因此您无需将{{name}} 作为参数传递。相反,请使用 this.name 在帮助程序中访问 {{name}}

    翡翠

    <template name="content">
      <li>{{name}} - {{date}}</li>
      {{#each images}}
        <img src="{{this.url}}">    
      {{/each}}
    </template>
    

    JS

    Template.content.helpers({
      images: function() {
        place = this.name;
        return Images.find({place: place});
      }
    }); 
    

    【讨论】: