【问题标题】:Is it possible to define multiple template helpers en masse instead of one-by-one in Meteor?是否可以在 Meteor 中定义多个模板助手而不是一个一个地定义?
【发布时间】:2014-07-24 16:48:08
【问题描述】:

名称为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 集合的方式。

【问题讨论】:

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


    【解决方案1】:

    我还找到了另一种方法:

    (这是为了注册一个全局助手,但同样的代码体可以用于模板助手)

    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 摄影”

    等等等等

    【讨论】:

      【解决方案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"}} 这样称呼那个助手。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-19
        • 1970-01-01
        • 2012-02-02
        • 2019-04-07
        • 2015-01-26
        • 2014-11-28
        • 1970-01-01
        • 2015-05-14
        相关资源
        最近更新 更多