简体   繁体   English

流星模板功能的上下文

[英]Context of Meteor Template Functions

I have html like: 我有喜欢的html:

<template name="chargers">
  {{#each list}}
    {{> charger}}
  {{/each}}
</template>

<template name="charger">
  <p>
    {{full_name}}
  </p>
</template>

There is a Meteor Collection called Chargers and the chargers template iterates over it just fine. 有一个称为Chargers的流星集合,并且chargers模板可以很好地对其进行迭代。 There is, however, no full_name attribute on that collection. 但是,该集合上没有full_name属性。 That is to be computed by concatenating the name property and the address.country property. 这是通过将name属性和address.country属性连接起来来计算的。

It would seem to me that some coffee script like: 在我看来,一些咖啡脚本如下:

Template.charger.full_name = ->
  console.log "full name of #{self} is #{name}"

  # return name

  if address.country.length() > 0
    return "#{name}, #{address.country}"
  else
    return name

would work, but self as echoed in the first line of the function is Window. 会起作用,但是函数第一行中所回显的self是Window。 I thought the context would the the Charger object being rendered. 我以为上下文将渲染Charger对象。 Oddly, If I uncomment the #return name , the whole thing works (but no country). 奇怪的是,如果我取消注释#return name ,那么整个过程都可以工作(但没有国家/地区)。 I've verified that the data is present in the collection from the console window. 我已验证数据是否存在于控制台窗口中的集合中。

Using transform on the collection seems like a big hammer just to add a pseudo-property. 仅在添加伪属性的情况下对集合使用transform似乎是一个巨大的锤子。

I tried defining this as a helper, but the same thing happened. 我尝试将其定义为帮助程序,但是发生了同样的事情。 What's the correct approach. 什么是正确的方法。

Sorry... The correct solution involved goofing around with the Coffeescript a bit: 抱歉,正确的解决方案涉及到对Coffeescript进行一些修改:

if @address.country.length > 0
  return "#{@name}, #{@address.country}"
else
  return @name

So, the member of the collection passed to the template helper is this.memberName , and thus, should be an @ variable in Coffeescript. 因此,传递给模板帮助器的集合的成员是this.memberName ,因此应为Coffeescript中的@变量。 TIL. 瓷砖。

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

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