Meteor数据上下文,将数组传递给每个Spacebars循环 [英] Meteor data context, passing array into each Spacebars loop

查看:160
本文介绍了Meteor数据上下文,将数组传递给每个Spacebars循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例2 - 传递数据上下文



JS

  Template.overview.helpers({
users:[
{name:'David'},
{name:'Shaune'}
]
} );

HTML

 < template name =overview> 
{{> userList users}}
< / template>

< template name =userList>
{{#each this}}
{{name}}< br>
{{/每个}}
< / template>

结果

  David 
Shaune

我的目标是将一组模板名称传递到 Template.overview 其中的用户,如下所示:

 < template name =overview> 
{{#each templateArray}}
{{> userList this}}
{{/ each}}
< / template>

其中 templateArray 是一个字符串数组,模板名称,这个是每个模板的名称。



这可能吗?



编辑:



更深入地考虑我在帮助者

  Template.overview.helpers({
users:[
{name:'David'},
{name:'Shaune'}
],
otherUsers:[
{name:'Jane'},
{name :'Betty'}
],
moarUsers:[
{name:'Ben'},
{name:'Jerry'}
]
});

我想将每个数据上下文传递到同一个模板中(在这种情况下 userLists )。

解决方案

可以在帮助器中使用 this ,比如

  helper:function(){ 
返回此; //如果你通过templateArray传递字符串
return this.name; //如果你通过templateArray传递对象



$ b因为你看起来像这样但是只是很好地包装到模板中

 < template name =overview> 
{{#each templateArray}}
{{name}}< br>
{{/每个}}
< / template>

我不知道你为什么在代码中使用双循环,不能遍历它

编辑



如果您想循环使用字符串数组,请执行以下操作:

  {{#each templateArray}} 
{{this}}< br>
{{/ each}}


In this Example 2 -- "Passing a data context"

JS

Template.overview.helpers({  
  users: [
    { name: 'David' },
    { name: 'Shaune' }
  ]
});

HTML

<template name="overview">  
  {{> userList users}}
</template>

<template name="userList">  
  {{#each this}}
    {{name}}<br>
  {{/each}}
</template>  

Result

David  
Shaune 

My goal is to pass an array of template names into Template.overview where users is, like so:

<template name="overview">  
  {{#each templateArray }}
    {{> userList this }}
  {{/each}}
</template>

Where templateArray is an array of strings with template names, and this is the name of each template.

Is this possible?

EDIT:

For more depth, consider that I have the following in my helpers

Template.overview.helpers({  
  users: [
    { name: 'David' },
    { name: 'Shaune' }
  ],
  otherUsers: [
    { name: 'Jane' },
    { name: 'Betty' }
  ],
  moarUsers: [
    { name: 'Ben' },
    { name: 'Jerry' }
  ]
});

And I'd like to pass each of these data context into the same template (in this case userLists).

解决方案

You don't need to call anything, you can use this inside helper, like

helper: function(){
   return this; //if you pass string via templateArray
   return this.name; //if you pass object via templateArray
}

Because what you do looks like this but is just wrapped nicely into templates

<template name="overview">  
  {{#each templateArray }}
      {{name}}<br>
  {{/each}}
</template>

I don't know why you have in code double loop, in first loop you pass object and it can't iterate throught it

EDIT

If you wanna just loop throught array of strings, do:

{{#each templateArray}}
       {{this}}<br>
 {{/each}}

这篇关于Meteor数据上下文,将数组传递给每个Spacebars循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆