【问题标题】:Meteor - Passing a jade helper into a helper functionMeteor - 将一个玉助手传递给一个助手函数
【发布时间】:2015-10-08 18:46:38
【问题描述】:

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

我用静态变量制作了相同类型的列表填充: 玉-

         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

咖啡脚本-

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

这使用一个助手 isCurrentState 将给定参数与我的其他代码链接到的同一对象匹配,因此我知道该部分有效

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

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

咖啡脚本-

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

所有其他部分都 100% 运行,但所选部分没有

我还尝试通过以下方式更改输入 selected='' 部分的方式:

  • selected='{{isCurrentLocation "#{siteName}" }}'
  • selected='{{isCurrentLocation "#{siteName} }}'
  • selected='{{isCurrentLocation {{siteName}} }}'
  • selected='#{isCurrentLocation "{{siteName}}" }'
  • selected='#{isCurrentLocation {{siteName}} }'
  • selected='#{isCurrentLocation #{siteName} }'

我正在尝试做的事情是否可能? 有没有更好的方法来实现这一点?

任何帮助将不胜感激

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

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

虽然它在我想使用它的上下文中不起作用,但它确实为我指明了一个我没有想到的方向。我正在考虑将父模板(建筑物)日期上下文作为参数移动到位置模板中,并在位置模板中使用它。这在普通的 HTML 空格键中很容易修复:

{{>locations parentDataContext/variable}}

jade 中的这种东西很容易解决这个问题

【问题讨论】:

    标签: meteor coffeescript pug


    【解决方案1】:

    简答

    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]
    

    【讨论】:

      【解决方案2】:

      我进一步研究了数据上下文,最终将选择填充到不同模板中的选项设置为不同的模板,并为该模板提供了一个帮助器,访问模板的父级数据上下文并使用它来确定建筑物保存在其中的位置这样我就可以将该选项设置为选中

      玉-

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

      咖啡脚本-

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-10
        • 2016-01-07
        • 2013-09-30
        • 1970-01-01
        • 2015-09-02
        • 2013-06-18
        • 2019-04-07
        • 2014-11-28
        相关资源
        最近更新 更多