本文介绍了逻辑或/和Handlebars.JS助手,多个参数,第一个总是被检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 中评估为 true  code>转换并使您的帮助器返回 true ,尽管调用站点只传递了 false 值。 / p> 

为了使帮助程序按预期工作,我们需要在 参数对象。 (FYI: slice 的目的是将类似数组的参数 Object转换为数组,以便我们可以调用Array.prototype方法,如 .some 。)为此,我们将或 helper更新为be:

  return Array.prototype.slice.call(arguments,0,-1).some(Boolean); 

现在我们可以转向比较我们的第一个参数表达式和其余的问题。我们可以类似地更新我们的 .slice 调用以排除第一个参数: .slice.call(arguments,1,-1)。然后,我们只需要将切片中的每个项目与第一个参数进行比较。我们的助手变成:

$ p $ return Array.prototype.slice.call(arguments,1,-1).some(arg => ; arg === arguments [0]);

我们的帮手现在按照我们的意图工作;但我会敦促你重新命名它,因为它不是一个或操作,而是一个in。


The following was proposed as a logical AND/OR multi-arg Handlebars.JS helper:

Handlebars.registerHelper({
    and: function () {
        return Array.prototype.slice.call(arguments).every(Boolean);
    },
    or: function () {
        return Array.prototype.slice.call(arguments).some(Boolean);
    }
});

Handlebars.js Else If

This doesn't work for me because I need to call it as

{{#if (or questionType 'STARTTIME' 'ENDTIME') }}

{{#if (or questionType 'STARTTIME' 'ENDTIME' 'ARGUMENT3' 'ARGUMENT4') }}

In other words,

  1. I support multiple args for my AND/OR,
  2. The first arg is always what I'm checking, e.g.

    return (questionType == arg1 || questionType == arg2 || questionType == arg3 ...)

In other words, I can't write a dumb 2-param or(..) / and(..) like this,

Handlebars.registerHelper('or', function(a, b, c) {
    if(a == b || a == c)
        return true;
    else
        return false;
});

It should be multi-argument, with the first one always being checked. Any thoughts?

解决方案

First: Your original or helper is not going to work. Handlebars passes an additional meta object as the final argument when invoking a helper. For example, using your or helper in a template as (or false false) results in the helper function being executed with the following arguments object:

{
    0: false,
    1: false,
    2: {
        "name": "or",
        "hash": {...},
        "data": {...}
    },
    length: 3
}

The existence of that Object at 3 will evaluate to true in the Boolean conversion and cause your helper to return true despite that the call site passes only false values.

To make the helper work as intended, we need to exclude the last arg when slicing our arguments object. (FYI: The purpose of the slice is to convert the array-like arguments Object to an Array, so that we may call Array.prototype methods on it, like .some.) To do this, we update our or helper to be:

return Array.prototype.slice.call(arguments, 0, -1).some(Boolean);

Now we can turn to the problem of comparing our first argument expression to the rest. We can similarly update our .slice call to exclude the first argument as well: .slice.call(arguments, 1, -1). Then we need only to compare each item in the slice to the first argument. Our helper becomes:

return Array.prototype.slice.call(arguments, 1, -1).some(arg => arg === arguments[0]);

Our helper now works as we intend; but I would urge you to rename it as it not an "or" operation, but an "in".

这篇关于逻辑或/和Handlebars.JS助手,多个参数,第一个总是被检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:04