2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)
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
2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)的更多相关文章
- 2.3 The Object Model -- Computed Properties
一.What are computed properties? 1. 简而言之,计算属性让你声明函数为属性.你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个f ...
- 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?
有时候新用户在使用计算属性.绑定和监视者时感到困惑.下面是一些指导方针: 1. 使用computed properties来合成其他属性,以构建新的属性.computed properties不应该包 ...
- (3)选择元素——(2)文档对象模型(The Document Object Model)
One of the most powerful aspects of jQuery is its ability to make selecting elements in the DOM easy ...
- object model 概述
Object Model 综述 标准 C++ 的对象模型为对象的动态特性提供了运行时的支持. 但是它静态的本性决定了在某些领域它表现出僵化.不可扩展的特点. GUI编程就是一个既需要运行时编译的效率, ...
- Stored Properties 与 Computed Properties
Stored Properties 与 Computed Properties About Swift Stored Properties In its simplest form, a stored ...
- POM(project Object Model) Maven包管理依赖 pom.xml文件
什么是POM POM全称为“Project Object Model”,意思是工程对象模型.Maven工程使用pom.xml来指定工程配置信息,和其他文本信息.该配置文件以xml为格式,使用xml语法 ...
- 2.5 The Object Model -- Observers
Ember支持监视任何属性,包括计算的属性.你可以使用Ember.observer为一个对象设置一个监视者: Person = Ember.Object.extend({ //these will b ...
- [转]The Regular Expression Object Model
本文转自:https://docs.microsoft.com/en-us/dotnet/standard/base-types/the-regular-expression-object-model ...
- Component Object Model (COM) 是什么?
本文主要介绍 COM 的基础知识,倾向于理论性的理解,面向初学者,浅尝辄止. 1. COM 是什么: COM 的英文全称是,Component Object Model,中文译为,组件对象模型.它官方 ...
随机推荐
- eclipse中debug模式不能启动运行,run运行模式却能启动运行!
这个问题我郁闷了好久!问题原因:因为断点太多了,断点冲突了. 解决办法:只要进如deug界面,选择BreakPoints选项,然后清除所有断点,再重新debug启动.问题解决! 希望能帮到遇到此问题的 ...
- EHcache经典配置
记录重要的东西和常用的东西. <ehcache> <!-- 指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个文件目录下 --> <diskStore ...
- js常用总结
常用总结,方便大家学习共享. 1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html-& ...
- /etc/redhat-release
该文件用于记录 RedHat 的发行版本信息 [root@localhost ~]$ cat /etc/redhat-release CentOS release 6.5 (Final)
- osgEarth使用没有DX的Triton库Triton-MT-DLL-NODX.lib
将Triton-MT-DLL修改为Triton-MT-DLL-NODX
- unity动态加载(翻译) .
AssetBundles are files which you can export from Unity to contain assets of your choice. These files ...
- 说说NAND FLASH以及相关ECC校验方法
Flash名称的由来,Flash的擦除操作是以block块为单位的,与此相对应的是其他很多存储设备,是以bit位为最小读取/写入的单位,Flash是一次性地擦除整个块:在发送一个擦除命令后,一次性地将 ...
- mySQL数据库二:命令行的使用
在做整理的时候,上一篇刚开始只是简单的做了个数据类型的开头,在这里简单说一下mySQL的使用以及它的命令行 1.准备工作 有一个好的开发工具可以几何倍数的增加我们的工作效率,所以,工具是必不可少的,首 ...
- jquery类似方法的比较(三)
属性选择器 1.$("div[class|='test']")可以选择 class为test的div 和 class以“test-”开头的div. 2.$("div[cl ...
- [分享]收集的Linux学习资源
下面是我收集的一些Linux资源,与大家分享.大家共同学习,一起进步. 国内的专业Linux网站(GB) 1. ChinaUnix:http://www.chinaunix.net/ 2. Linux ...