giraffeslacks
giraffeslacks

Reputation: 75

How to pass a template variable to a template helper function to preserve context?

I may be using the wrong words to describe my issue so here is the (simplified) code I'm working with.

I'm happy to learn a better way to do this but what I'm currently trying to do is pass {{assigneeId}} to the template helper function called agentIs. The problem is that I can't find the correct way to pass the value.

<template name="ticket_list">
  {{#each tickets}}
    {{> ticket}}
  {{/each}}
</template>

<template name="ticket">
  <h3>{{title}}</h3>
  <p>{{assigneeId}}</p>
  {{> ticket_footer}}
</template>

<template name="ticket_footer">
  {{> agent_list}}
</template>

<template name="agent_list">
  <!-- {{assigneeId}} exists here as expected -->
  assigneeId: {{assigneeId}}
  <label for="agent">Agent</label>
  <select id="agent" name="agent">
    {{#each agents}}
      <!-- value passed: "{{assigneeId}}" -->
      <option value="{{_id}}" {{#if agentIs "{{assigneeId}}"}}selected{{/if}}>
        {{profile.name}}
      </option>
      <!-- value passed: undefined -->
      <option value="{{_id}}" {{#if agentIs assigneeId}}selected{{/if}}>
        {{profile.name}}
      </option>
      <!-- value passed: JSON.parse error -->
      <option value="{{_id}}" {{#if agentIs {{assigneeId}}}}selected{{/if}}>
        {{profile.name}}
      </option>
    {{/each}}
  </select>
</template>
Template.agent_list.agents = function() {
  return Meteor.users.find({"profile.is_agent": true}, {sort: {profile: 1}});
}

Template.agent_list.agentIs = function(assigneeId) {
  return this._id === assigneeId;
};

Upvotes: 2

Views: 3487

Answers (1)

saimeunt
saimeunt

Reputation: 22696

The correct syntax would be :

{{#if agentIs ../assigneeId}}selected{{/if}}

The {{#each agents}} block helper introduces a new level in the template contexts tree (the new context corresponds to the current agent), this is why you need to "go back" from one level to properly reference the previous context where assigneeId resides.

Upvotes: 3

Related Questions