简体   繁体   English

没有参数无法解析自定义手把助手

[英]Custom Handlebars helper is not resolved without parameters

I am creating a handlebars helper, which takes the following form: 我正在创建一个车把助手,其格式如下:

define(['Handlebars'], function (Handlebars) {
    Handlebars.registerHelper("myHelper", function (options) {
        console.log('myHelper');
        if (*condition*) {
            console.log('myHelper False');
            return options.inverse(this);
        } else {
            console.log('myHelper True');
            return options.fn(this);
        }
    });
});

As you can see, I'm using require.js. 如您所见,我正在使用require.js。 I'm also using this as part of a Backbone.js application. 我还将它用作Backbone.js应用程序的一部分。 In the template, the helper is called like so: 在模板中,帮助程序的调用方式如下:

{{#myHelper}}
<!-- Some HTML -->
{{else}}
<!-- Some HTML -->
{{/myHelper}}

However, the helper always returns false because it is not recognized. 但是,帮助程序总是返回false,因为它无法被识别。 I know this because the console.log is never called. 我知道这是因为从不调用console.log。 I have other custom helpers in the application that work, but they all take in arguments. 我在应用程序中还有其他可以工作的自定义助手,但是它们都带有参数。 If I add a dummy argument, the helper works fine: 如果我添加了一个哑元参数,则助手可以正常工作:

define(['Handlebars'], function (Handlebars) {
    Handlebars.registerHelper("myHelper", function (dummy, options) {
        console.log('myHelper');
        if (*condition*) {
            console.log('myHelper False');
            return options.inverse(this);
        } else {
            console.log('myHelper True');
            return options.fn(this);
        }
    });
});

Template: 模板:

{{#myHelper "string"}}
<!-- Some HTML -->
{{else}}
<!-- Some HTML -->
{{/myHelper}}

I'm using handlebars v1.0.0. 我正在使用把手v1.0.0。 Is this something that is addressed in 2.0.0? 这是2.0.0中解决的问题吗? This isn't blocker, but I clearly would prefer not to use a dummy argument if possible. 这不是阻止程序,但我显然希望尽可能不要使用虚拟参数。

Here is fiddle with helper you need. 这是您需要的帮手摆弄的东西。 handlebars-1.0.rc.1 used. 使用handlebars-1.0.rc.1。 Also tried with handlebars-1.3.0 - works fine. 还尝试过车把1.3.0-工作正常。

HTML HTML

<script id="topLevel" type="text/x-handlebars-template">
    {{#myHelper}}
        it's truthy
    {{else}}
        it's falsy
    {{/myHelper}}
</script>

JS JS

Handlebars.registerHelper('myHelper', function (options) {
    if (true) {
        console.log("It's true");
        return options.fn(this);
    }
    console.log("It's false");
    return options.inverse(this);
});

var _template =  Handlebars.compile($('#topLevel').html());
$('body').append(_template());

So your issue could occur: 因此,您的问题可能会发生:

Outdated library or you are trying to use helper before it has been registered. 库已过时,或者您在注册助手之前正在尝试使用它。 Require.js loads libraries/files asynchronously, call handlebars as dependency. Require.js异步加载库/文件,将句柄称为依赖项。 Example: 例:

define(function(require){
    var yourObj = function() {
      require(['handlebars'], function (Handlebars) {
      // use Handlebars here
      });
    };
    return yourObj;
}); 

Hope it's help. 希望对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM