是否有可能在Meteor中一次又一次地定义多个模板助手?

时间:2014-06-04 06:36:29

标签: javascript meteor spacebars meteor-helper meteor-collections

名称为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"
}

假设我想要帮助者显示每个不同服务的serviceDescription和价格。

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个不同的助手。

有更简单的方法吗?此外,通过我的网站结构化方式,我不能使用#each作为Meteor手册用于其Posts集合。

2 个答案:

答案 0 :(得分:2)

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

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

您可以像{{getField "hdrPhotos" "serviceDescription"}}一样调用该帮助程序。

答案 1 :(得分:0)

我还找到了另一种方法:

(这是用于注册全局帮助程序,但相同的代码体可用于模板助手)

Handlebars.registerHelper("hdrPhotos", function() {
  return Services.findOne({"sku": "hdrPhotos"});
});

Handlebars.registerHelper("twilightPhotos", function() {
  return Services.findOne({"sku": "twilightPhotos"});
});

Handlebars.registerHelper("videoClips", function() {
  return Services.findOne({"sku": "videoClips"});
});

这个想法是这些助手中的每一个都基于sku返回服务的整个文档。

然后,可以使用(。)

简单地调用各个属性

{{hdrPhotos.price}}返回100

{{hdrPhotos.serviceTitle}}返回"无限HDR摄影"

等等。