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,中文译为,组件对象模型.它官方 ...
随机推荐
- 微软Azure、谷歌GAE、亚马逊AWS比較
谷歌Google App Engine 亚马逊AWS 微软Microsoft Azure 提供服 务类型 PaaS, SaaS Iaas, PaaS IaaS, PaaS, SaaS 服务间 ...
- VS2015编译OpenSSL1.0.2源码
更多详细信息http://blog.csdn.net/YAOJINGKAO/article/details/53041165?locationNum=10&fps=1 1.下载安装编译必须的A ...
- js里面声明变量时候的注意事项
变量名可以是中文,只能有下划线,$,数字和字母组成,开头只能以下划线(不建议使用)和字母开头.
- Java编程基本概念
1.标识符 ①用于给变量.类和方法命名(类名首字母大写,变量和方法名首字母小写并遵循驼峰原则)②标识符的命名规范: ■标识符必须以字母.下划线和美元符$开头. ■标识符其他部分可以是字母.下划线.美元 ...
- 96、facebook Fresco框架库源使用(转载)
各个属性详情:http://blog.csdn.net/y1scp/article/details/49245535 开源项目链接 facebook Fresco仓库:git clone https: ...
- Sql中将字符串按分割符拆分
创建函数 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create FUNCTION [dbo].[F_Split] ( @SplitString ...
- 【jQuery系列之插件】jquery插件之jquery-validation
equalTo方法: equalTo: function( value, element, param ) { // Bind to the blur event of the target in o ...
- CSS-自定义高度的元素背景图如何自适应以及after伪元素在ie下的处理
我都好久没更新了! 遇到一个效果,之前没有考虑清楚,设置了固定高度,到了后边,产品要加长,我就觉得设计得从新弄张长点的背景图!这不多余么? 其实分析原图还是可以再切分,再细化到不用改设计图,让我们前端 ...
- 学习坤哥的replaceTpl方法
学习坤哥的方法之后自己写的replaceTpl function replaceTpl(tpl, data){///////////////没有传入可让用户自己定义的方式进行替换,不够灵活 ...
- poj2096 Collecting Bugs[期望dp]
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 5394 Accepted: 2670 ...