【发布时间】:2013-09-15 15:25:39
【问题描述】:
在我的模板中,我正在迭代模型中的一个数组。 为简单起见,我将使用以下示例:
{{#each user in appModel.users}}
现在,如果用户在某个组中,我正在尝试显示 user 的元素。
我有App.Group 作为模型,它具有检查用户是否在组中的方法。这个方法有一个参数,用户。
所以,
{{group.has_user user}}
其中group 是App.Group 的特定实例,如果在组的ID 列表中找到用户ID,则返回true 或false。
我正在尝试找到一种方法将此方法用作模板条件。像这样:
{{#if group.has_user user}}
<block>
{{/if}}
把它们放在一起:
{{#each user in appModel.users}}
{{#if group.has_user user}}
<block displaying user data>
{{/if}}
{{/each}}
这样就达到了显示群组用户的预期效果。问题是有条件的{{#if}} helper 不能使用该方法的参数。
我尝试使用自定义注册助手,但找不到传递模型属性的方法。如果我像这样在模板中使用帮助器:
{{#ifUserInGroup user.id group.id}}
我会得到字符串 'user.id' 和 'group.id' 作为传递的参数。助手是这样写的:
Ember.Handlebars.registerHelper('ifUserInGroup', function(val1, val2, options){
//This is where I would get the models, but val1 and val2 are strings, not id's...
});
如果有人知道在条件中使用该方法的方法,或者如何将实际值传递给助手,请帮助我。
谢谢。
【问题讨论】:
标签: templates ember.js helpers conditional