gpt4 book ai didi

javascript - 是否可以在 Meteor 中一起定义多个模板助手而不是一个一个地定义?

转载 作者:行者123 更新时间:2023-11-29 22:00:29 25 4
gpt4 key购买 nike

名称为 Services 的集合有多个文档:

{
serviceTitle: "Unlimited HDR Photography",
serviceDescription: "Includes unlimited daytime HDR photos and a single trip out to the property. A zip file with the bare JPG files will be emailed to you. No website or virtual tour / panoramas.",
serviceHTML: "",
sku: "hdrPhotos",
price: 100,
_id: "x5Hq7ybdS3YHmrgsa"
}

{
serviceTitle: "Twilight Photography",
serviceDescription: "Photos will be shot 15 minutes prior to sunset for dramatic lighting effects.",
serviceHTML: "",
sku: "twilightPhotos",
price: 100,
_id: "RLhjHBGicGQKgS4SQ"
}

{
serviceTitle: "Video Clips",
serviceDescription: "We will come in with professional video equipment and shoot numerous video clips of the property. The video files will then be uploaded to your Single Property Website provider.",
serviceHTML: "",
sku: "videoClips",
price: 175,
_id: "uBZSeNWa4zZNMa8z9"
}

假设我想要可以显示每个不同服务的服务描述和价格的助手。

Template.myTemplate.helpers({

hdrPhotosServiceDescription: function(){
return Services.findOne({"sku": "hdrPhotos"}, { serviceDescription:1, _id:0}).serviceDescription
},
hdrPhotosPrice: function(){
return Services.findOne({"sku": "hdrPhotos"}, { price:1, _id:0}).price
},

twilightPhotosServiceDescription: function(){
return Services.findOne({"sku": "twilightPhotos"}, { serviceDescription:1, _id:0}).serviceDescription
},
twilightPhotosPrice: function(){
return Services.findOne({"sku": "twilightPhotos"}, { price:1, _id:0}).price
},

videoClipsServiceDescription: function(){
return Services.findOne({"sku": "videoClips"}, { serviceDescription:1, _id:0}).serviceDescription
},
videoClipsPrice: function(){
return Services.findOne({"sku": "videoClips"}, { price:1, _id:0}).price
}

});

这个例子只展示了三个不同的服务,并且希望每个服务的两个属性都有助手。

{{hdrPhotosServiceDescription}}, {{hdrPhotosPrice}}, {{twilightPhotosServiceDescription}}, {{twilightPhotosPrice}}, {{videoClipsServiceDescription}}, {{videoClipsPrice}}

如果每个服务有 10 个不同的属性,我需要为每个属性提供帮助,而我有 30 个不同的服务,会怎样?这样做需要我编写代​​码并定义 300 个不同的助手。

有没有更简单的方法来做到这一点?此外,由于我网站的结构方式,我不能像 Meteor 手册用于其 Posts 集合的那样使用 #each

最佳答案

模板助手可以有参数。最简单的方法是:

Template.myTemplate.helpers({
getField: function(sku, fieldName) {
var projection = {_id: 0};
projection[fieldName] = 1;
return Services.findOne({"sku": sku}, projection)[fieldName];
}
}

您可以像 {{getField "hdrPhotos""serviceDescription"}} 那样调用该助手。

关于javascript - 是否可以在 Meteor 中一起定义多个模板助手而不是一个一个地定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24030604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com