当我使用参数生成模板时,我丢失了数据上下文

时间:2015-09-15 21:58:18

标签: meteor handlebars.js

当我在#each helper中生成子模板并添加参数时,我会丢失数据上下文,这通常是可见的。

我通过

将数据字段传递给模板找到了解决方法
{{> productItem parameter="test" name=name details=details}}

,但是对于更复杂的收藏来说,这将是非常无聊的......是不是有更好的选择来解决这个问题?

<template name="main">
    {{#each products}}
        {{> productItem parameter="test"}}
    {{/each}}
</template>

<template name="productItem">
    <div class="product">
        <p>{{name}}</p>
        <p>{{details}}</p>
        <p>{{parameter}}</p>
    </div>
</template>

和javascript:

Template.main.helpers({
    products: function(){
        return Products.find({});
    }
});

1 个答案:

答案 0 :(得分:0)

您正在创建一个新的上下文(它不会神奇地合并所有内容),但它很容易包含原始上下文。

你去了: -

 {{> productItem product=this parameter="test"}}

然后

<template name="productItem">
    <div class="product">
        <p>{{product.name}}</p>
        <p>{{product.details}}</p>
        <p>{{parameter}}</p>
    </div>
</template>

<template name="productItem">
    <div class="product">
       {{#with product}}
        <p>{{name}}</p>
        <p>{{details}}</p>
       {{/with}}
        <p>{{parameter}}</p>
    </div>
</template>