1. 通常,你可能有一个计算的属性依赖于数组中的所有元素来确定它的值。例如,你可能想要计算controller中所有todo items的数量,以此来确定完成了多少任务。

export default Ember.Controller.extend({
todos: [
Ember.Object.create({ isDone: true }),
Ember.Object.create({ idDone: false }),
Ember.Object.create({ isDone: true })
], remaining: Ember.computed('todos.@each.isDone', function () {
var todos = this.get('todos');
return todos.filterBy('isDone', false).get('length');//1
});
});
  • 注意这里依赖的key(todos.@each.isDone)包含特殊的key @each
  • 当以下四个事件发生时,这指示ember.js更新此计算属性的绑定和触发观察者:
    • todos数组中的任何对象的isDone属性发生改变。
    • todos数组中添加一项。
    • todos中删除一项。
    • controllertodos属性被改变为另外一个数组。
  • 在上面的例子中,reamaining的count值是1:
import TodosController from 'app/controllers/todos';
todosController = TodosController.create();
todosController.get('remainging');

2. 如果我改变todo's isDone属性, remaining属性将会被自动更新:

var todos = todosController.get('todos');
var todo = todos.objectAt(1);
todo.set('isDone', true); todosController.get('remaining'); // todo = Ember.Object.Create({ isDone: false });
todos.pushObject(todo); todosController.get('remaining');//

3. 请注意@each不能嵌套。

正确:todos@each.owner.name

错误:todos@each.owner.@each.name

最新文章

  1. 关于 Dictionary<string,string>,和List<T>在View的使用
  2. ASP.NET - 回滚事务
  3. app启动速度
  4. Boost练习程序(multi_index_container)
  5. winform里怎样在一个按钮上实现“单击”和“双击”事件?
  6. iOS开发-迭代器模式
  7. 转:C/C++内存管理详解 堆 栈
  8. Java_解决java.security.cert.CertificateException: Certificates does not conform to algorithm constraints
  9. Mysql 基本操作连接数据库读取信息内容
  10. nginx的upstream目前支持5种方式的分配(转)
  11. phpcms 列表项 内容项
  12. git上传项目全部流程
  13. jQuery 事件绑定 和 JavaScript 原生事件绑定
  14. Unable to ignore resources
  15. Dynamics 365的审核日志分区删除超时报错怎么办?
  16. iOS 初探代码混淆(OC)
  17. webpack简单的打包体验
  18. Java基础98 gson插件的使用
  19. python中常用的模块一
  20. linux od命令详解

热门文章

  1. swift学习笔记之—自定义函数的规则说明
  2. ios 获取设备相关的信息
  3. HTTP 请求过程
  4. 你知道嵌入式C语言中各变量存储的位置吗?
  5. Google's C++ coding style
  6. java高级---->Thread之Condition的使用
  7. 2015.10.11(js判断鼠标进入容器的方向)
  8. Egret类class和module写法区别
  9. linux显示文件列表命令ls,使用ls --help列出所有命令参数
  10. Android在ArrayAdapter<>里如何得到List<>的Items