Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ember handlebars traverses the label


May 09, 2021 Ember.js Reference documents



In the same way as in the previous article, a routing file and a corresponding template file were created using the ember generate route handlebars-each command. T his article will introduce you to traversal tags, which are visible in almost any commonly used development language and are a very widely used feature. I 'll introduce you to handlebars label of handlebars, which are used almost the same as EL expressions. I think you'll understand when you look at it... Nonsense less, below directly on the demo code! !

  1. // app/routes/handlebars.js
  2. import Ember from 'ember';
  3. /**
  4. * 定义一个用于测试的对象数组
  5. */
  6. export default Ember.Route.extend({
  7. // 重写model回调函数,初始化测试数据
  8. model: function() {
  9. return [
  10. Ember.Object.create({ name: 'chen', age: 25}),
  11. Ember.Object.create({ name: 'i2cao.xyz', age: 0.2}),
  12. Ember.Object.create({ name: 'ibeginner.sinaapp.com', age: 1}),
  13. Ember.Object.create({ name: 'ubuntuvim.xyz', age: 3})
  14. ];
  15. }
  16. });

As shown above, an route objects for testing is built in the route class, each with two properties name age Here's a template for displaying the data:

  1. {{! 遍历在route里设置的对象数组 }}
  2. <ul>
  3. {{#each model as |item|}}
  4. <li>Hello everyone, My name is {{item.name}} and {{item.age}} year old.</li>
  5. {{/each}}
  6. </ul>

Is there a feeling that seems to have been similar!! T he forEach label forEach the EL expression. No surprises you should be able to see the following results.

Ember handlebars traverses the label

Reminder: Remember that the URL running at this time is the new rout that was just created. When operating arrays, be careful to use the official recommended methods (for pushObject instead push see the previous article.

1, access the array undersrug

In some cases we may need to get the undersequent of the array, for example, sometimes we may sign down as the serial number of the data. Take a look at the demo below:

  1. {{! 遍历在route里设置的对象数组 }}