admin管理员组

文章数量:1353311

I need a way to pare values in handlebars and found this helper on the web:

Handlebars.registerHelper('pare', function(lvalue, rvalue, options) {

    if (arguments.length < 3)
        throw new Error("Handlerbars Helper 'pare' needs 2 parameters");

    var operator = options.hash.operator || "==";

    var operators = {
        '==':       function(l,r) { return l == r; },
        '===':      function(l,r) { return l === r; },
        '!=':       function(l,r) { return l != r; },
        '<':        function(l,r) { return l < r; },
        '>':        function(l,r) { return l > r; },
        '<=':       function(l,r) { return l <= r; },
        '>=':       function(l,r) { return l >= r; },
        'typeof':   function(l,r) { return typeof l == r; }
    }

    if (!operators[operator])
        throw new Error("Handlerbars Helper 'pare' doesn't know the operator "+operator);

    var result = operators[operator](lvalue,rvalue);

    if( result ) {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }

This is how I use it:

{{#each numbers as |nr|}}
         {{#pare nr stars operator="<=" }}

             <span{{action "rate" }}  class="rating{{nr}} glyphicon glyphicon-star"></span>

         {{/pare}}


       {{/each}}
});

I get this error:

TypeError: options.inverse is not a function

I need a way to pare values in handlebars and found this helper on the web:

Handlebars.registerHelper('pare', function(lvalue, rvalue, options) {

    if (arguments.length < 3)
        throw new Error("Handlerbars Helper 'pare' needs 2 parameters");

    var operator = options.hash.operator || "==";

    var operators = {
        '==':       function(l,r) { return l == r; },
        '===':      function(l,r) { return l === r; },
        '!=':       function(l,r) { return l != r; },
        '<':        function(l,r) { return l < r; },
        '>':        function(l,r) { return l > r; },
        '<=':       function(l,r) { return l <= r; },
        '>=':       function(l,r) { return l >= r; },
        'typeof':   function(l,r) { return typeof l == r; }
    }

    if (!operators[operator])
        throw new Error("Handlerbars Helper 'pare' doesn't know the operator "+operator);

    var result = operators[operator](lvalue,rvalue);

    if( result ) {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }

This is how I use it:

{{#each numbers as |nr|}}
         {{#pare nr stars operator="<=" }}

             <span{{action "rate" }}  class="rating{{nr}} glyphicon glyphicon-star"></span>

         {{/pare}}


       {{/each}}
});

I get this error:

TypeError: options.inverse is not a function

Share Improve this question asked Oct 8, 2015 at 6:25 LukeLuke 1,8142 gold badges22 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You need to define the else section for pare block helper.

Handlebars provides the block for the else fragment as options.inverse. You do not need to check for the existence of the else fragment: Handlebars will detect it automatically.

Source: https://handlebarsjs./guide/block-helpers.html#conditionals

Another option is to go with principle of least surprise in the helper:

if (typeof options.inverse === "function")
  return options.inverse(this);
else
  return null;

Which isn't bad practice for any Handlebars helper; don't try to render an inverse if the template writer didn't provide one.

本文标签: javascriptTypeError inverse is not a function (in handlebars helper)Stack Overflow