简体   繁体   中英

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. 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.

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.

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}}.

Workaround is, you can change the

{{this}}

to

{{./this}}

I guess {{this}} depends on `toString' output of given object. Add a helper for stringfiy and try out {{stringify this}}. It might give you more insights.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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