课程表

Ember.js 基础

Ember.js 对象模型

Ember.js handlebars模板

Ember.js 路由

Ember.js 组件

Ember.js 控制器

Ember.js 模型

Ember.js 测试

工具箱
速查手册

handlebars显示对象的键

当前位置:免费教程 » JS/JS库/框架 » Ember.js

在实际的开发过程中你很有可能需要显示出对象数组的键或者值,如果你需要同时显示出对象的键和值你可以使用{{#each-in}}标签。
注意:each-in标签是Ember 2.0才有的功能,之前的版本是无法使用这个标签的,如果是2.0一下的版本会报错:Uncaught Error: Assertion Failed: A helper named 'each-in' coulad not be found
准备工作:使用Ember CLI生成一个component,与此同时会生成一个对应的模板文件。
ember generate component store-categories
执行上述命令得到下面的3个文件:

  1. app/components/store-categories.js
  2. app/templates/components/store-categories.hbs
  3. tests/integration/components/store-categories-test.js

然后在app/router.js增加一个路由设置,在map方法里添加this.route('store-categories');;此时可以直接访问http://localhost:4200/store-categories;

在组件中增加测试数据

  1. // app/components/store-categories.js
  2. import Ember from 'ember';
  3.  
  4. export default Ember.Component.extend({
  5. // https://guides.emberjs.com/v2.4.0/components/the-component-lifecycle/
  6. willRender: function() {
  7. // 设置一个对象到属性“categories”上,并且设置到categories属性上的对象结构是:key为字符串,value为数组
  8. this.set('categories', {
  9. 'Bourbons': ['Bulleit', 'Four Roses', 'Woodford Reserve'],
  10. 'Ryes': ['WhistlePig', 'High West']
  11. });
  12. }
  13. })

willRender方法在组件渲染的时候执行,更多有关组件的介绍会在后面章节——组件中介绍,想了解更多有关组件的介绍会在后面的文章中一一介绍,目前你暂且把组件当做是一个提取出来的公共HTML代码。

有了测试数据之后我们怎么去使用each-in标签遍历出数组的键呢?

  1. <!-- // app/templates/components/store-categories.hbs -->
  2. <ul>
  3. {{#each-in categories as |category products|}}
  4. <li>{{category}}
  5. <ol>
  6. {{#each products as |product|}}
  7. <li>{{product}}</li>
  8. {{/each}}
  9. </ol>
  10. </li>
  11. {{/each-in}}
  12. </ul>

上述模板代码中第一个位置参数category就是迭代器的键,第二个位置参数product就是键所对应的值。

为了显示效果,在application.hbs中调用这个组件,组件的调用非常简单,直接使用{{组件名}}方式调用。

  1. <!-- //app/templates/application.hbs -->
  2. {{store-categories}}

渲染后结果如下图:

result

重渲染

{{each-in}}表达式不会根据属性值变化而自动更新。上述示例中,如果你给属性categories增加一个元素值,模板上显示的数据不会自动更新。为了演示这个特性在组件中增加一个触发属性变化的按钮,首先需要在组件类app/components/store-categories.js中增加一个action方法(有关action会在后面的章节介绍,暂时把他看做是一个普通的js函数),然后在app/templates/components/store-categories.hbs中增加一个触发的按钮。

  1. import Ember from 'ember';
  2.  
  3. export default Ember.Component.extend({
  4. // willRender方法在组件渲染的时候执行,更多有关组件的介绍会在后面章节——组件,中介绍
  5. willRender: function() {
  6. // 设置一个对象到属性“categories”上,并且设置到categories属性上的对象结构是:key为字符串,value为数组
  7. this.set('categories', {
  8. 'Bourbons': ['Bulleit', 'Four Roses', 'Woodford Reserve'],
  9. 'Ryes': ['WhistlePig', 'High West']
  10. });
  11. },
  12. actions: {
  13. addCategory: function(category) {
  14. console.log('清空数据');
  15. let categories = this.get('categories');
  16. // console.log(categories);
  17. categories['Bourbons'] = [];
  18. // 手动执行重渲染方法更新dom元素,但是并没有达到预期效果
  19. // 还不知道是什么原因
  20. this.rerender();
  21. }
  22. }
  23. });
  1. <!-- // templates/components/store-categories.hbs -->
  2.  
  3. <ul>
  4. {{#each-in categories as |category products|}}
  5. <li>{{category}}
  6. <ol>
  7. {{#each products as |product|}}
  8. <li>{{product}}</li>
  9. {{/each}}
  10. </ol>
  11. </li>
  12. {{/each-in}}
  13. </ul>
  14.  
  15. <button onclick={{action 'addCategory'}}>点击清空数据</button>

但是很遗憾,即使是手动调用了rerender方法也没办法触发重渲染,界面显示的数据并没有发生变化。后续找到原因后再补上!!

空数组处理

空数组处理与表达式{{each}}一样,同样是判断属性不是nullundefined[]就显示出数据,否则执行else部分。

  1. {{#each-in people as |name person|}}
  2. Hello, {{name}}! You are {{person.age}} years old.
  3. {{else}}
  4. Sorry, nobody is here.
  5. {{/each-in}}

可以参考上一篇的{{each}}标签测试,这里不再赘述。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号