Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 流星,火焰:创建一个简单的循环与每个?_Meteor_Meteor Blaze_Meteor Helper - Fatal编程技术网

Meteor 流星,火焰:创建一个简单的循环与每个?

Meteor 流星,火焰:创建一个简单的循环与每个?,meteor,meteor-blaze,meteor-helper,Meteor,Meteor Blaze,Meteor Helper,一个简单的问题,但我找不到简单的解决办法。我必须为此写一个助手吗 我想在模板中创建特定数量的li项。具体数字通过一个名为max的参数给出 {{> rating max=10 value=rating }} <template name="rating"> <ul class="rating"> {{#each ?}} <li class="star">\u2605</li>

一个简单的问题,但我找不到简单的解决办法。我必须为此写一个助手吗

我想在模板中创建特定数量的li项。具体数字通过一个名为max的参数给出

{{> rating max=10 value=rating }}

<template name="rating">
    <ul class="rating">
        {{#each ?}}
            <li  class="star">\u2605</li>
        {{/each }}
    </ul>
</template>
{{>额定最大值=10值=额定值}
    {{{每个?}
  • \u2605
  • {{/每个}}
来自:

构造一个视图,该视图为视图中的每个项呈现contentFunc 顺序

因此,每个都必须与一个序列(数组、光标)一起使用,因此您必须创建一个助手来创建要使用的序列#each。 通常视图是按项目值定制的,但在您的情况下,使用
max
大小的数组就可以完成这项工作

或者,您可以创建一个自定义模板帮助器,在其中可以传递要重复的html以及帮助器中的重复次数和concat html。 类似(未测试的代码):

//注册帮助程序
htmlRepeater=函数(html,n){
var out='';

对于(var i=0;i,只需从底层助手返回所需数量的项:

html:

当然,如果在集合上进行迭代,则可以将
.find()
限制为
max

我还注意到您的
评级
模板没有在任何地方使用参数
。您的意思是:

<ul class = "{{value}}">
还是应该在其他地方替换该值?

我同意:

Template.registerHelper('repeat', function(max) {
    return _.range(max); // undescore.js but every range impl would work
});

{#每个(重复10次)}
{{this}}
{{/每个}}

这能回答你的问题吗?不太可能。这是在onCreated中创建数组的明显解决方案的一个复杂变体。我正在寻找类似ng repeat in angular的东西。但这听起来很糟糕。创建一个数组来迭代特定的次数。没有更好的解决方案吗?这似乎是。奇怪的是,这不是内置的.METER或focus db,有时这是一个限制,但其余时间它很好:)通过使用
Template.registerHelper('repeat',function(){let args=[].slice.call(arguments);args.splice(-1,1);return{.range.apply(this,args);})
您将能够使用
repeat max
重复开始-结束
重复开始-结束步骤
返回范围(1,最大+1)用于非零基数组
Template.rating.helpers({
  myThings: function(max){
    // return an array 0...max-1 (as convenient as any other set of values for this example)
    return Array.apply(null, Array(max)).map(function (x, i) { return i; });
  }
});
<ul class = "{{value}}">
Template.registerHelper('repeat', function(max) {
    return _.range(max); // undescore.js but every range impl would work
});
{#each (repeat 10)}}
    <span>{{this}}</span>
{{/each}}