我有一个 Meteor Handlebars 模板助手,它代表了我的大多数模板助手,如下所示。
# Address Form Controls Template
Template.AddressFormControls.helpers
address1: () ->
if typeof Session.get('edit-building') is 'string'
building = Buildings.findOne(Session.get('edit-building'))
return building?.address?.address1
address2: () ->
if typeof Session.get('edit-building') is 'string'
building = Buildings.findOne(Session.get('edit-building'))
return building.address.address2
city: () ->
if typeof Session.get('edit-building') is 'string'
building = Buildings.findOne(Session.get('edit-building'))
return building.address.city
state: () ->
if typeof Session.get('edit-building') is 'string'
building = Buildings.findOne(Session.get('edit-building'))
return building.address.state
zip_code: () ->
if typeof Session.get('edit-building') is 'string'
building = Buildings.findOne(Session.get('edit-building'))
return building.address.zip_code
main: () ->
if typeof Session.get('edit-building') is 'string'
building = Buildings.findOne(Session.get('edit-building'))
return building?.phone?.main
fax: () ->
if typeof Session.get('edit-building') is 'string'
building = Buildings.findOne(Session.get('edit-building'))
return building?.phone?.fax
您会注意到每个帮助程序都包含一个if
语句来查看 Session 变量是否为字符串。如果它是一个字符串,那么它应该是一个可用于执行所需对象搜索的 ID。
对于一个人使用的各种收藏品,在整个项目中重复这一点似乎是一种浪费。
我怎样才能把它弄干?
帮助我欧比旺,你是我唯一的希望!