Meteor Helper检查平等 [英] Meteor Helper Check equality

查看:113
本文介绍了Meteor Helper检查平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查meteor helper中字符串的值?

Is there a way to check the value of a string in meteor helper?

让我说我有这个帮手

Template.example.helpers({
 typeOfVideo:function(videoType){

        var videoLink = GS.Gems.Collections.Gems.findOne({_id:this._id}).videoLink;

        if(videoLink.match(/youtube\.com/)){
            return "youtube";

        }else if(videoLink.match(/vimeo\.com/)){
            return "vimeo";

        }else{
            return "undefined"

        }
    }
})

现在我想检查视频是否等于Youtube,Vimeo或undefined,我可以通过返回true / false来做,但是因为还有更多我想知道什么类型的视频即时处理

Now i want to check if the video is equal to Youtube, Vimeo or undefined, i could do by returning true/false, but since there are more i want to know what type of video im dealing with

{{#if typeOfVideo=youtube}} <!-- also {{#if typeOfVideo 'youtube'}}
            youtube
{{/if}}

{{#if typeOfVideo=vimeo}}
         vimeo
{{/if}}

模板呈现所有值(youtube,vimeo),我缺少某些东西?

And the templates render all the value (youtube,vimeo), im missing somehting?

推荐答案

作为Spacebars (流星模板系统)是一个无逻辑的模板系统(就像它所基于的Handlebars)你不能直接检查 if 语句中是否相等。

As Spacebars (Meteor templating system) is a "logic-less" templating system (like Handlebars on which it is based) you cannot directly check for an equality in a if statement.

但是你可以创建一个全球助手有两个参数来检查字符串相等或匹配字符串,如下所示:

But you can create a global helper with two parameters to check for a string equality or matching string like this:

Template.registerHelper('isEqual', function(string, target) {
  if( string.match( new RegExp(target, 'g') ) ) {
    return true;
  }
  return false;
});

并在您的模板中使用它,如下所示:

And use it in your template like this:

{{#if isEqual videoLink 'youtube'}}
  <p>Youtube</p>
{{else}}
  <p>Vimeo</p>
{{/if}}

如果没有其他在Spacebars中,所以如果你有很多情况要检查它可能会很麻烦。
但是你可以考虑在可能的情况下直接从全局帮助器中注入模板/内容。

There is no else if in Spacebars, so if you have many cases to check it may be a hassle. But you could think of injecting the template/content directly from the global helper when possible.

这篇关于Meteor Helper检查平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆