2017-09-19 6 views
0

文字列が現在のユーザーのユーザー名と一致するかどうかによって、Handlebars.jsを使用して要素を表示しています。Handlebars.jsのループ内の2つの属性を比較するには、1つの属性がループスコープ外にあるかどうかを確認します。

私は実際にコレクションblogsをループしています。これは、属性authorが出場するところです。どちらにかかわらず、author.usernameはユーザー名文字列を表し、これは私にとって利用可能です。私の問題は、user.attributes.usernameをあらかじめ定義された文字列、つまり「martin」に変更すると、条件がチェックアウトされ、必要な方法で要素が表示されることです。しかし、私も属性がuser.attributes.usernameであり、これは#ifEqualsの状態の外でうまくレンダリングします。私は実際にauthor.usernameをこの属性と比較したいと思います。あらかじめ定義された文字列ではありません。

は、どのように私は2つの属性、user.attributes.username代わりの"martin"author.usernameと、すなわちauthor.usernameを比較することができますか?

author.usernameuser.attributes.usernameはどちらも同じ値です。これは今の私のために働くが、私は最終的に何をしたいですしません:これは動作しますが、私の目的には十分ではありません

{{#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}} 

{{#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ヘルパー:

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

編集:さらに検査すると、問題があるようです。user.attributes.usernameウォンblogループ内に描画しません。 userblogの属性であると考えています。ループの範囲外にあるかのようにuserにアクセスする方法はありますか? authorblogに属しているのに対し、

+0

あなたは 'ember.js'を使用していますか? – Lux

+0

いいえ、偶然にタグを追加しました。ありがとう。それを削除しました。 – santafebound

答えて

1

どうやらあなたは../

{{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を追加することによってHandlebars.jsにアウターループ属性にアクセスすることができますが、blogループの外から来属性です。

関連する問題