传递处理程序后车把上下文丢失

Sam*_*gal 2 javascript handlebars.js

我确信这是非常基本的,但我无法在 ../model 中使用{{#if_eq}}。我什至尝试使用 ../../model 并且它指向 model._revs_info 的子代。

  {{#each model._revs_info}} 
        {{debug ../model}}
        {{#if_eq status compare="available"}} 
            {{debug ../model}}
            <a href="#list/{{model.id}}/{{rev}}">{{rev}}</a>
         {{/if_eq}}                                      
  {{/each}}         
Run Code Online (Sandbox Code Playgroud)

{{#if_eq}}已从https://github.com/danharper/Handlebars-Helpers/blob/master/helpers.js复制

/**
 * If Equals
 * if_eq this compare=that
 */
Handlebars.registerHelper('if_eq', function(context, options) {
    if (context == options.hash.compare)
        return options.fn(this);
    return options.inverse(this);
});
Run Code Online (Sandbox Code Playgroud)

{{debug}}已从http://thinkvitamin.com/code/handlebars-js-part-3-tips-and-tricks/复制

Handlebars.registerHelper("debug", function(optionalValue) {
  console.log("Current Context");
  console.log("====================");
  console.log(this);

  if (optionalValue) {
    console.log("Value");
    console.log("====================");
    console.log(optionalValue);
  }
});
Run Code Online (Sandbox Code Playgroud)

nik*_*shr 5

根据Handlebars 关于路径的文档

../ 路径段引用父模板范围,而不是上下文中的上一级。这是因为块助手可以调用具有任何上下文的块,因此“上一级”的概念除了作为对父模板范围的引用之外并没有特别意义。

每个块助手定义一个范围,所以在 if_eq 中你的层次结构看起来像

  1. 基本模板,
  2. 每个范围,
  3. if_eq 范围。

指向祖父母,../../model您将获得正确的上下文。

还有一个基于你的代码的小提琴http://jsfiddle.net/aFGD6/另一个调试活跃的,http://jsfiddle.net/aFGD6/1/