简体   繁体   English

Meteor,如何从另一个助手访问助手?

[英]Meteor, how to access to a helper from another helper?

I have a helper like 我有一个好帮手

Template.user_profile.helpers({
  user:function() {
     return Meteor.users.find({'profile.front_name':Session.get('slug')}).fetch()[0];
  }
});

I want to add a helper to the collection which could access the user helper and compare its _id with the current user _id , to tell whether the user is visiting its own profile. 我想在集合中添加一个帮助程序,它可以访问user帮助程序并将其_id与当前用户_id进行比较,以判断用户是否正在访问自己的配置文件。 I'm using something pretty ugly: 我正在使用一些非常难看的东西:

Template.user_profile._tmpl_data.helpers.user()

The final code: 最终代码:

Template.user_profile.helpers({
  user:function() {
     return Meteor.users.find({'profile.front_name':Session.get('userId')}).fetch()[0];
  },
  isCurrentUser: function() {
    return Template.user_profile._tmpl_data.helpers.user()._id === Meteor.userId();
  }
});

Is there any better way to access another helper? 有没有更好的方法来访问另一个帮手?

I've just accidentally discovered this in the console: 我刚刚在控制台中意外地发现了这个:

Template.registerHelper
function (name, func) {                                                                             
  Blaze._globalHelpers[name] = func;                                                                                   
} 

So, Blaze._globalHelpers is what we are looking for! 所以, Blaze._globalHelpers是我们正在寻找的!

You can call a template helper (not global helper - which is in outluch's answer) with: 您可以使用以下命令调用模板帮助程序(不是全局帮助程序 - 这是outluch的答案)

Template.tplName.__helpers.get('helper').call()

MDG suggests using a regular function and then passing it to helpers, events and so on. MDG建议使用常规函数,然后将其传递给帮助程序,事件等。 See here . 看到这里

Update 16.06.16 更新16.06.16
Actually I strongly advise to simply use manuel:viewmodel - it alleviates so many Blaze headaches... 实际上我强烈建议简单地使用manuel:viewmodel - 它减轻了很多Blaze的头痛......

As I was searching for a way to call a helper from another helper, I found that Meteor 1.0 defines "Template.registeredHelpers" that are available for all other helpers to use. 当我在寻找一种从另一个助手调用帮助器的方法时,我发现Meteor 1.0定义了“Template.registeredHelpers”,可供所有其他助手使用。 https://docs.meteor.com/#/full/template_registerhelper https://docs.meteor.com/#/full/template_registerhelper

Template.registerHelper("checkedIf",function(value){
  return value?"checked":"";
});

You might not even need to call a helper like that. 您可能甚至不需要像这样调用帮助器。 There is a currentUser helper already built in. 已经内置了currentUser助手。

http://docs.meteor.com/#template_currentuser http://docs.meteor.com/#template_currentuser

{{currentUser}}

maybe this would work for you: 也许这对你有用:

  //js
Template.foo.helpers({ bar: function() {
 return this.userId == Meteor.userId(); },
 domain: function() {
 var a = document.createElement('a'); a.href = this.url;
 return a.hostname;
 } });

 ownsDocument = function(userId, doc) { return doc && doc.userId === userId;}

 Posts = new Meteor.Collection('posts');
 Posts.allow({
 update: ownsDocument, remove: ownsDocument
 });

  //html
{{#if bar}}<a href="{{pathFor 'postEdit'}}">Edit</a>{{/if}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 能够从Meteor中的助手访问特定值 - Ability to access specific values from helper in Meteor meteor:如何从流星助手打印数组 - meteor : How to print an array from meteors helper 如何将数据从流星控制器传递到流星助手? - How can I pass data from a meteor controller to a meteor helper? 在模板的上下文中从另一个帮助器调用一个帮助器(Meteor 0.9.4) - Calling one helper from another helper within the context of a template (Meteor 0.9.4) Meteor.js:如何在辅助函数中从afQuickField访问架构元数据(字段类型) - Meteor.js : How to access schema metadata(field type) from afQuickField in a helper function 使用流星时,如何从另一个模板的辅助函数中调用模板? - How Do I Call a Template from Within Another Template's Helper Function When Using Meteor? 如何在不使用帮助程序的情况下访问Meteor模板中的全局变量? - How to access global variables in Meteor template without using a helper? 如何访问流星助手功能中的URL参数? - How do I access URL parameters in Meteor helper functions? 流星-如何在同一模板助手中调用另一个函数 - Meteor - How call another function in same template helper Meteor.js:如何将一个助手的数据上下文传递给另一个助手? - Meteor.js: how to pass the data context of one helper to another helper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM