流星 - 将玉助手传递给助手功能

时间:2015-10-07 13:44:25

标签: meteor coffeescript pug

我正在尝试使用数据集填充列表,并使用辅助函数设置所选选项,该函数将当前数据与另一个对象的数据进行比较(2个对象已链接)

我使用静态变量创建了相同类型的列表填充: 玉 -

         select(name='status')
            option(value='Newly Acquired' selected='{{isCurrentState "Newly Acquired"}}') Newly Acquired
            option(value='Currently In Use' selected='{{isCurrentState "Currently In Use"}}') Currently In Use
            option(value='Not In Use' selected='{{isCurrentState "Not In Use"}}') Not In Use
            option(value='In Storage' selected='{{isCurrentState "In Storage"}}') In Storage

的CoffeeScript -

  "isCurrentState" : (state) ->
     return @status == state

这使用帮助程序isCurrentState将给定参数与我的其他代码链接到的同一对象相匹配,因此我知道该部分可以正常工作

我试图开始工作的代码是: 玉 -

            select.loca(name='location')
               each locations
                   option(value='#{siteName}' selected='{{isCurrentLocation {{siteName}} }}') #{siteName}

的CoffeeScript -

  "isCurrentLocation": (location) ->
     return @locate == location

所有其他部分均正常运行,但所选部分不是

我也尝试过以下方式改变我进入所选=''部分的方式:

  • selected ='{{isCurrentLocation“#{siteName}”}}'
  • selected ='{{isCurrentLocation“#{siteName}}}'
  • selected ='{{isCurrentLocation {{siteName}}}}'
  • selected ='#{isCurrentLocation“{{siteName}}”}'
  • selected ='#{isCurrentLocation {{siteName}}}'
  • selected ='#{isCurrentLocation#{siteName}}'

我正在尝试做甚么可能吗? 有没有更好的方法来实现这一目标?

非常感谢任何帮助

更新: 感谢@david-weldon快速回复,我已经尝试了一下,并意识到我在我的问题中想要完成的事情并不完全清楚。 我有一个模板“update_building”,它创建了一个带有许多属性的参数(一个建筑对象),其中一个是“locate”。

Locations是另一个具有许多属性的对象,其中一个是“siteName”。其中一个siteName == locate因此我需要从位置传入siteName以使其与当前建筑物的locate属性相匹配

虽然它在我想要使用的上下文中不起作用,但它肯定指向了我没有想到的方向。我期待将父模板(建筑物)日期上下文作为参数移动到位置模板中,并在位置模板中使用它。这可以在普通的HTML空格键中轻松修复:

{{>locations parentDataContext/variable}}

玉石中的东西很容易解决这个问题

2 个答案:

答案 0 :(得分:0)

简短回答

selected='{{isCurrentLocation siteName}}'

答案很长

您并不需要传递当前位置,因为帮助程序应该知道它自己的上下文。这是一个简单(经过测试)的例子:

<强>玉

template(name='myTemplate')
  select.location(name='location')
    each locations
      option(value=this selected=isCurrentLocation) #{this}

<强>咖啡

LOCATIONS = [
  'Newly Acquired'
  'Currently In Use'
  'Not In Use'
  'In Storage'
]

Template.myTemplate.helpers
  locations: LOCATIONS

  isCurrentLocation: ->
    @toString() is Template.instance().location.get()

Template.myTemplate.onCreated ->
  @location = new ReactiveVar LOCATIONS[1]

答案 1 :(得分:0)

我更多地查看了datacontexts并最终制作了将select填充到另一个模板并为该模板提供帮助的选项,访问模板的父级数据上下文并使用它来确定建筑物在其中保存了哪个位置这样我就可以将该选项设置为选中

玉 -

template(name="location_building_option")
   option(value='#{siteName}' selected='{{isSelected}}') #{siteName}

Coffeescript -

Template.location_building_option.helpers
   'isSelected': ->
      parent = Template.parentData(1)
      buildSite = parent.locate
      return @siteName == buildSite

谢谢你@david-weldon,你的回答极大地帮助我朝着正确的方向前进

相关问题