cphill
cphill

Reputation: 5924

Handlebars Accessing Non-Related Object in Nested Each Statement

I have a scenario where I am using a registered helper ifeq conditional that is comparing the object being used within my each statement and a separate object that is public to the view file. i.e. blog_comment and user object.

However, I can't seem to figure out a way to access the user object for the fact that it doesn't have a relationship to the each statement of blog_comment. Is there a way to access non-related objects within a handlebars expression?

Two objects that are publicly accessible by the view:

blog_comments (Being looped through)

user (represents the information of the user currently logged in)

Here is my ifeq conditional:

hbs.registerHelper('ifeq', function(value1, value2, options){
    return((value1 === value2) ? options.fn(this) : options.inverse(this));
});

Here is my view file: (comparing blog_comments.userId to user.userId)

    {{#blog_comments}}

<i>{{createdAtDateSlug}}</i>
            {{#ifeq userId user.userId}}<a href="#" class="blog-comment-delete" data-blog-id="{{blogId}}" data-comment-id="{{blogCommentId}}">Delete</a></p>
            {{/ifeq}}
            <p class="blog-comment">{{comment}}</p>

    {{/blog_comments}}

Upvotes: 0

Views: 231

Answers (1)

cphill
cphill

Reputation: 5924

I figured out the solution. Since I am trying to access a separate object, I need to get back to the root of the scope and then access the object and its property. i.e. @root.user.userId in {{ifeq userId @root.user.userId}}. This change resolved my issue

Upvotes: 1

Related Questions