Meteor-一次注册多个助手

Meteor-一次注册多个助手,meteor,spacebars,Meteor,Spacebars,在我的Meteor应用程序v1.1.0.2中,我有10个全局帮助程序: Template.registerHelpergetUser,函数ID{ return Meteor.users.findOne{ _id:id }; }; Template.registerHelpergetPublication,函数ID{ return.findOne{ _id:id }; }; Template.registerHelpergetCategory,函数ID{ return Categories.fin

在我的Meteor应用程序v1.1.0.2中,我有10个全局帮助程序:

Template.registerHelpergetUser,函数ID{ return Meteor.users.findOne{ _id:id }; }; Template.registerHelpergetPublication,函数ID{ return.findOne{ _id:id }; }; Template.registerHelpergetCategory,函数ID{ return Categories.findOnenew Mongo.objectidd; }; Template.registerHelpergetAge,函数ID{ 返回Ages.findOnenew Mongo.objectivedid; }; Template.registerHelpergetPhoto,函数ID{ 返回新的FS.FileImagesPublications.findOne{ _id:id }; }; Template.RegisterHelpOptions类别,函数ID{ return Categories.find.mapfunctionelement{ 返回{ 标签:element.name, 值:元素。\u id.\u str }; }; }; Template.RegisterHelpOptionsAges,函数ID{ 返回Ages.find.mapfunctionelement{ 返回{ 标签:element.name, 值:元素。\u id.\u str }; }; }; Template.registerHelperallowedCategories,functionid{ return Categories.find.mapfunctionelement{ 返回元素。\u id.\u str; }; }; Template.registerHelperallowedAges,函数ID{ 返回Ages.find.mapfunctionelement{ 返回元素。\u id.\u str; }; }; Template.registerHelperPrentifydate,functiondate{ 返回时间。格式为'lhh:mm:ss'; }; 我试图使用一个对象在单个函数调用中压缩所有这些:

Template.registerHelper{ getUser:functionid{ return Meteor.users.findOne{ _id:id }; }, getPublication:functionid{ return.findOne{ _id:id }; }, getCategory:functionid{ return Categories.findOnenew Mongo.objectidd; }, getAge:functionid{ 返回Ages.findOnenew Mongo.objectivedid; }, getPhoto:functionid{ 返回新的FS.FileImagesPublications.findOne{ _id:id }; }, 选项类别:函数ID{ return Categories.find.mapfunctionelement{ 返回{ 标签:element.name, 值:元素。\u id.\u str }; }; }, 选项页面:functionid{ 返回Ages.find.mapfunctionelement{ 返回{ 标签:element.name, 值:元素。\u id.\u str }; }; }, allowedCategories:functionid{ return Categories.find.mapfunctionelement{ 返回元素。\u id.\u str; }; }, allowedAges:functionid{ 返回Ages.find.mapfunctionelement{ 返回元素。\u id.\u str; }; }, prettifyDate:functiondate{ 返回时间。格式为'lhh:mm:ss'; } };
我尝试了上述方法,但没有成功,因此我想知道是否可以做类似的事情。

当然可以,只需将您的助手放入一个键/值字典中,如下所示:

var helpers = {
  getAge: function(id) {
    return Ages.findOne(new Mongo.ObjectID(id));
  }
};
然后使用循环遍历每一对,并在其中注册帮助器

_.each(helpers, function(value, key){
  Template.registerHelper(key, value);
});

该死,你说得对。我不知道为什么我不这么想,这很明显。我觉得自己像个白痴。。。谢谢