Meteor - 将玉助手传递给助手函数
Meteor - Passing a jade helper into a helper function
我正在尝试用数据集填充列表并使用辅助函数设置所选选项,该辅助函数将当前数据与另一个对象的数据进行比较(这 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
所有其他部分功能正常 100%,但所选部分不是
我也试过改变输入 selected='' 部分的方式,例如:
- 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 之一,因此我需要从 locations 传入 siteName 以将其与当前建筑物的 locate 属性相匹配
虽然它在我想使用的上下文中不起作用,但它确实为我指明了一个我没有想到的方向。我正在研究将父模板(建筑物)日期上下文作为参数移动到位置模板中,并从位置模板中使用它。这很容易在正常的 HTML 空格键中修复:
{{>locations parentDataContext/variable}}
玉石中的类似东西很容易解决这个问题
简答
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]
我进一步研究了数据上下文,最终将 select 填充到不同模板中的选项设置为该模板的助手,访问模板的父级数据上下文并使用它来确定位置该建筑物已保存在其中,因此我可以将该选项设置为 selected
翡翠-
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,你的回答极大地帮助我朝着正确的方向前进
我正在尝试用数据集填充列表并使用辅助函数设置所选选项,该辅助函数将当前数据与另一个对象的数据进行比较(这 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
所有其他部分功能正常 100%,但所选部分不是
我也试过改变输入 selected='' 部分的方式,例如:
- 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 之一,因此我需要从 locations 传入 siteName 以将其与当前建筑物的 locate 属性相匹配
虽然它在我想使用的上下文中不起作用,但它确实为我指明了一个我没有想到的方向。我正在研究将父模板(建筑物)日期上下文作为参数移动到位置模板中,并从位置模板中使用它。这很容易在正常的 HTML 空格键中修复:
{{>locations parentDataContext/variable}}
玉石中的类似东西很容易解决这个问题
简答
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]
我进一步研究了数据上下文,最终将 select 填充到不同模板中的选项设置为该模板的助手,访问模板的父级数据上下文并使用它来确定位置该建筑物已保存在其中,因此我可以将该选项设置为 selected
翡翠-
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,你的回答极大地帮助我朝着正确的方向前进