简体   繁体   English

如何在Handlebars.js的循环中比较两个属性,其中一个属性在循环范围之外?

[英]How can I compare two attributes in a loop in Handlebars.js where one attribute is outside the loop scope?

I'm using Handlebars.js to display an element depending on whether a string matches the current user's username. 我正在使用Handlebars.js根据字符串是否与当前用户的用户名匹配来显示元素。

I'm actually looping through a collection blogs and this is where the attribute author comes into play. 我实际上是遍历一个收藏blogs而这正是属性author发挥作用的地方。 Regardless, author.username represents some username string and this is available to me. 无论如何, author.username代表一些用户名字符串,我可以使用它。 My problem is that if I change user.attributes.username to some pre-defined string, ie "martin" then the condition checks out and it displays the element the way I want. 我的问题是,如果我将user.attributes.username更改为一些预定义的字符串,即“ martin”,则条件将检出,并以所需的方式显示该元素。 But I also have an attribute user.attributes.username and this renders just fine outside the #ifEquals condition. 但是我还有一个属性user.attributes.username ,它在#ifEquals条件之外也可以user.attributes.username显示。 I really want to compare author.username with this attribute, and not some pre-defined string. 我真的想将author.username与此属性进行比较,而不是将一些预定义的字符串进行比较。

How can I compare two attributes, ie author.username with user.attributes.username instead of author.username with "martin" ? 如何比较两个属性,即author.usernameuser.attributes.username而不是author.username"martin"

Both author.username and user.attributes.username have the same value. author.usernameuser.attributes.username都具有相同的值。 This doesn't work for me right now but is what I ultimately want: 目前这对我不起作用,但我最终想要的是:

{{#each blog}}
        {{#ifEquals author.username user.attributes.username}}
            <a href="#" onClick="return false;"><span class="delete-container" style="cursor: pointer" data-blog-id="{{objectId}}"><span class="typcn typcn-arrow-sorted-down hvr-pulse-grow" style="color: gray; font-size: 30px;"></span></span></a>
        {{/ifEquals}}
{{/each}}

This works but isn't sufficient for my purposes: 这有效,但不足以达到我的目的:

{{#each blog}}
        {{#ifEquals author.username "martin"}}
            <a href="#" onClick="return false;"><span class="delete-container" style="cursor: pointer" data-blog-id="{{objectId}}"><span class="typcn typcn-arrow-sorted-down hvr-pulse-grow" style="color: gray; font-size: 30px;"></span></span></a>
        {{/ifEquals}}
{{/each}}

Handlebars.js Helper: Handlebars.js帮助器:

<script>
    Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
        return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
    });
</script>

Edit: Upon further inspection, it looks like the problem is that user.attributes.username won't render inside the blog loop. 编辑:经过进一步检查,看来问题在于user.attributes.username不会在blog循环中呈现。 It thinks user is an attribute of blog . 它认为userblog一个属性。 Is there any way to access user as if it were outside the scope of the loop? 有没有什么办法可以访问user ,就好像它不在循环范围之内?

Apparently you can access outer-loop attributes in Handlebars.js by appending ../ 显然,您可以通过添加../来访问Handlebars.js中的外循环属性。

{{currentUser.username}} // Renders fine outside the loop

 {{#each blog}}
    // Append ../ to access currentUser inside the loop
    {{#ifEquals author.username ../currentUser.username}}
        // Show element here
    {{/ifEquals}}
 {{/each}}

../user is an attribute that comes from outside the blog loop, whereas author belongs to blog . ../user是来自blog循环外部的属性,而author属于blog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM