如何将模板变量传递给模板辅助函数以保留上下文? [英] How to pass a template variable to a template helper function to preserve context?

查看:74
本文介绍了如何将模板变量传递给模板辅助函数以保留上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很乐意学习一个更好的方式来做到这一点,但我目前正在尝试做的是将 {{assigneeId}} 传递给名为 agentIs 。问题是我找不到传递值的正确方法。

 < 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}}按预期存在 - >
assigneeId:{{assigneeId}}
< label for =agent>代理< / label>
< select id =agentname =agent>
{{#each agents}}
<! - 传递的值:{{assigneeId}} - >
< option value ={{_ id}}{{#if agentIs{{assigneeId}}}}} selected {{/ if}}>
{{profile.name}}
< / option>
<! - 传递的值:undefined - >
< option value ={{_ id}}{{#if agentIs assigneeId}} selected {{/ if}}>
{{profile.name}}
< / option>
<! - 传递的值:JSON.parse错误 - >
< option value ={{_ id}}{{#if agentIs {{assigneeId}}}} selected {{/ if}}>
{{profile.name}}
< / option>
{{/每个}}
< / 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;
};


解决方案

正确的语法是:

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

这些{{#each agents}}块帮助程序在模板上下文树中引入了新的级别(新的上下文对应于当前的代理),这就是为什么您需要从一个层面返回,以正确引用受让人所在的前一个上下文。


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;
};

解决方案

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.

这篇关于如何将模板变量传递给模板辅助函数以保留上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆