2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)
2024-09-02 02:20:45
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中删除一项。
- controller的todos属性被改变为另外一个数组。
- 在上面的例子中,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
最新文章
- 关于 Dictionary<;string,string>;,和List<;T>;在View的使用
- ASP.NET - 回滚事务
- app启动速度
- Boost练习程序(multi_index_container)
- winform里怎样在一个按钮上实现“单击”和“双击”事件?
- iOS开发-迭代器模式
- 转:C/C++内存管理详解 堆 栈
- Java_解决java.security.cert.CertificateException: Certificates does not conform to algorithm constraints
- Mysql 基本操作连接数据库读取信息内容
- nginx的upstream目前支持5种方式的分配(转)
- phpcms 列表项 内容项
- git上传项目全部流程
- jQuery 事件绑定 和 JavaScript 原生事件绑定
- Unable to ignore resources
- Dynamics 365的审核日志分区删除超时报错怎么办?
- iOS 初探代码混淆(OC)
- webpack简单的打包体验
- Java基础98 gson插件的使用
- python中常用的模块一
- linux od命令详解
热门文章
- swift学习笔记之—自定义函数的规则说明
- ios 获取设备相关的信息
- HTTP 请求过程
- 你知道嵌入式C语言中各变量存储的位置吗?
- Google&#39;s C++ coding style
- java高级---->;Thread之Condition的使用
- 2015.10.11(js判断鼠标进入容器的方向)
- Egret类class和module写法区别
- linux显示文件列表命令ls,使用ls --help列出所有命令参数
- Android在ArrayAdapter<;>;里如何得到List<;>;的Items