简体   繁体   English

学习把手:如果我不使用`this`,为什么数据不传递到我的模板上

[英]Learning Handlebars: why isn't data being passed to my template if I don't use `this`

I'm learning Handlebars and using this piece of code below after looking at a couple of tutorials. 在看了几本教程之后,我正在学习Handlebars,并在下面使用这段代码。 Below, my code won't work if I don't use the keyword this ; 在下面,如果使用关键字this ,我的代码将无法工作; I tried {{#each shoesData}} and nothing happens to my HTML. 我尝试了{{#each shoesData}} ,但HTML没有任何反应。 If I don't want to use this , it will work only if I change the shoesData to JS Example #2 (following an example from another tutorial that uses the name of an object and not this keyword) and also, in my template wrote {{#each shoes}} . 如果我不想使用this ,则仅当我将shoeData更改为JS Example#2(下面是另一个使用对象名称而不是 this关键字的教程中的示例)并且在我的模板中编写时,它才有效{{#each shoes}} What's the difference? 有什么不同?

  <h3>The List of Shoes:</h3>
  <ul class="shoesNav"></ul>​

  <script id="shoe-template" type="text/x-handlebars-template">
    {{#each this}}
      <li class="shoes"><a href="/{{name}}">{{name}} -- Price: {{price}}</a></li>
    {{/each}}
  </script>

JavaScript #1 JavaScript#1

  var shoesNav = document.getElementsByClassName("shoesNav");
  var theTemplateScript = document.getElementById("shoe-template").innerHTML;
  var theTemplate = Handlebars.compile(theTemplateScript);

  var shoesData =[
      {
        name: "Nike",
        price: 199.00
      },
      {
        name: "Loafers",
        price: 59.00
      },
      {
        name: "Wing Tip",
        price: 259.00
      }
    ];

  var theCompiledTemplate = theTemplate(shoesData);
  shoesNav[0].innerHTML = theCompiledTemplate;

JavaScript #2 JavaScript#2

  var shoesData =
  {
    shoes: [
      {
        name: "Nike",
        price: 199.00
      },
      {
        name: "Loafers",
        price: 59.00
      },
      {
        name: "Wing Tip",
        price: 259.00
      }
    ]
  };

The item you are passing to your template is the array that is referenced by the variable 'shoesData'. 您传递给模板的项目是变量“ shoesData”引用的数组。 So when the template sees it the 'this' object looks like this: 因此,当模板看到它时,“ this”对象看起来像这样:

    [
      {
        name: "Nike",
        price: 199.00
      },
      {
        name: "Loafers",
        price: 59.00
      },
      {
        name: "Wing Tip",
        price: 259.00
      }
    ]

In Javascript #2, the object passed to the template is the value referenced by shoesData, which in this case is: 在Javascript#2中,传递给模板的对象是shoeData引用的值,在这种情况下为:

  {
    shoes: [
      {
        name: "Nike",
        price: 199.00
      },
      {
        name: "Loafers",
        price: 59.00
      },
      {
        name: "Wing Tip",
        price: 259.00
      }
    ]
  };

Do you see the difference? 你看得到差别吗? In the second example, the template sees an object with a property of shoes . 在第二个示例中,模板看到一个具有shoes属性的对象。 In your example (Javascript #1) it simply sees an array. 在您的示例(JavaScript#1)中,它只是看到一个数组。

The this parameter in both cases represents what is passed to the template so in your javascript it works with this because it represents an array that can be iterated over by the {{#each ... }} . this两种情况下的this参数都表示传递给模板的内容,因此在您的javascript中可以使用this因为它表示可以由{{#each ... }}进行迭代的数组。

To fix this issue to work the way you expect, change the shoesData to look like this: 要解决此问题,以使其达到预期的效果,请将shoeData更改为如下所示:

shoesData = {  
  shoes:
    [
      {
        name: "Nike",
        price: 199.00
      },
      {
        name: "Loafers",
        price: 59.00
      },
      {
        name: "Wing Tip",
        price: 259.00
      }
    ]
  }

Now you can use {{#each shoes}} in your template. 现在,您可以在模板中使用{{#each shoes}}

Another way to think of this, if you are still stuck, is what do you see if you do console.log(shoesData) in both examples? 如果仍然遇到问题,可以用另一种方法思考这两个示例中的console.log(shoesData)问题。 This will tell you what the handlebars will see as the this object, and if you do console.log(shoesData.shoes) in the second example, this is what will be iterated over when you do {{#each shoes}} . 这将告诉您将在this对象上看到的把手,如果在第二个示例中执行console.log(shoesData.shoes) ,则将在执行{{#each shoes}}时进行迭代。

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

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