简体   繁体   English

有时在每个助手中都不会打印{{this}}

[英]Handlebars sometimes not print {{this}} in each helper

I iterate an object and try to print value of object and sometimes it works and sometimes it does not. 我迭代一个对象并尝试打印该对象的值,有时它起作用,有时却不起作用。 This mainly happen in Chrome and Firefox and IE9 is working as it should be. 这主要发生在Chrome和Firefox中,并且IE9可以正常工作。 Somehow the generated function from handlebars get different when it works and when it does not. 从车把生成的功能在工作时和不工作时会有所不同。 Data is the same for both time. 两者的数据相同。 When i use log helper to print {{this}}, i can see correct value print in console. 当我使用日志帮助程序打印{{this}}时,可以在控制台中看到正确的值打印。

If you are wondering what is underscoretodash, it is a simple helper and even when i remove that it does not make any different. 如果您想知道下划线是什么,它是一个简单的帮助器,即使我删除它也没有什么不同。

My application using Backbone, Marionette, Handlebars and requireJs. 我的应用程序使用Backbone,Marionette,Handlebars和requireJs。

If anyone came across such a issue or know how to fix this, it would be very helpful. 如果有人遇到这样的问题或知道如何解决此问题,那将非常有帮助。

Data structure as follows 数据结构如下

var errors = {
            "custom_agent": "You can't be an agent.",
            "min_length": "Occupation must be at least 2 characters."
        };

Handlebar code as follows 车把代码如下

{{#each error_messages}}
  <span class="error-{{format @key "underscoretodash"}}">{{this}} {{log this}}</span>
{{/each}}

Handlebar generated functions as follows. 车把产生的功能如下。

When works. 当工作。

*** "Invalid Branch." handlebars.js:469
*** prog: "function program8(depth0,data) {

  var buffer = "", helper, options;
  buffer += "\n                    <span class=\"error-"
    + escapeExpression((helper = helpers.format || (depth0 && depth0.format),options={hash:{},data:data},helper ? helper.call(depth0, (data == null || data === false ? data : data.key), "underscoretodash", options) : helperMissing.call(depth0, "format", (data == null || data === false ? data : data.key), "underscoretodash", options)))
    + "\">"
    + escapeExpression((typeof depth0 === functionType ? depth0.apply(depth0) : depth0))
    + "</span>\n                ";
  return buffer;
  }" 

When does not works 何时不起作用

*** "Invalid Branch." handlebars.js:468
*** prog: "function program8(depth0,data) {

  var buffer = "", stack1, helper, options;
  buffer += "\n                    <span class=\"error-"
    + escapeExpression((helper = helpers.format || (depth0 && depth0.format),options={hash:{},data:data},helper ? helper.call(depth0, (data == null || data === false ? data : data.key), "underscoretodash", options) : helperMissing.call(depth0, "format", (data == null || data === false ? data : data.key), "underscoretodash", options)))
    + "\">";
  if (helper = helpers.undefined) { stack1 = helper.call(depth0, {hash:{},data:data}); }
  else { helper = (depth0 && depth0.undefined); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
  buffer += escapeExpression(stack1)
    + "</span>\n                ";
  return buffer;
  }"

It seems a bug of HB to determine the type of {{this}}. 确定{{this}}的类型似乎是HB的错误。

Workaround is, you can change the 解决方法是,您可以更改

{{this}}

to

{{./this}}

I guess {{this}} depends on `toString' output of given object. 我猜{{this}}取决于给定对象的“ toString”输出。 Add a helper for stringfiy and try out {{stringify this}}. 添加一个用于stringfiy的助手,然后尝试{{stringify this}}。 It might give you more insights. 它可能会给您更多的见解。

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

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