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

2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)的更多相关文章

  1. 2.3 The Object Model -- Computed Properties

    一.What are computed properties? 1. 简而言之,计算属性让你声明函数为属性.你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个f ...

  2. 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?

    有时候新用户在使用计算属性.绑定和监视者时感到困惑.下面是一些指导方针: 1. 使用computed properties来合成其他属性,以构建新的属性.computed properties不应该包 ...

  3. (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 ...

  4. object model 概述

    Object Model 综述 标准 C++ 的对象模型为对象的动态特性提供了运行时的支持. 但是它静态的本性决定了在某些领域它表现出僵化.不可扩展的特点. GUI编程就是一个既需要运行时编译的效率, ...

  5. Stored Properties 与 Computed Properties

    Stored Properties 与 Computed Properties About Swift Stored Properties In its simplest form, a stored ...

  6. POM(project Object Model) Maven包管理依赖 pom.xml文件

    什么是POM POM全称为“Project Object Model”,意思是工程对象模型.Maven工程使用pom.xml来指定工程配置信息,和其他文本信息.该配置文件以xml为格式,使用xml语法 ...

  7. 2.5 The Object Model -- Observers

    Ember支持监视任何属性,包括计算的属性.你可以使用Ember.observer为一个对象设置一个监视者: Person = Ember.Object.extend({ //these will b ...

  8. [转]The Regular Expression Object Model

    本文转自:https://docs.microsoft.com/en-us/dotnet/standard/base-types/the-regular-expression-object-model ...

  9. Component Object Model (COM) 是什么?

    本文主要介绍 COM 的基础知识,倾向于理论性的理解,面向初学者,浅尝辄止. 1. COM 是什么: COM 的英文全称是,Component Object Model,中文译为,组件对象模型.它官方 ...

随机推荐

  1. iOS-WKWebView使用

    使用代码:可直接粘贴到自己项目中使用 .h #import "BaseViewController.h" @interface LinkNewsController : BaseV ...

  2. Java精选笔记_JSTL(JSP标准标签库)

    JSTL(JSP标准标签库) JSTL入门 JavaServer Pages Standard Tag Library:JSP标准标签库 在JSP中可以通过Java代码来获取信息,但是过多的Java代 ...

  3. apache 图片防盗链

    RewriteEngine on RewriteCond %{HTTP_REFERER} !ot.com [NC] RewriteCond %{HTTP_REFERER} !baidu.com [NC ...

  4. m2014-architecture-webserver->百万记录级mysql数据库及Discuz!论坛优化

    作者:shunz,出处:http://shunz.net/2008/06/mysql_discuz_.html 最近,帮一个朋友优化一个拥有20万主题,100万帖子,3万多会员,平均在线人数2000人 ...

  5. date类型数据插入

    --字段类型是dateinsert into tab(column) values(to_date('2017_06_30 11:38:22','yyyy-mm-dd hh24:mi:ss'));-- ...

  6. ios学习--TableView详细解释

    -.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTa ...

  7. C、C++编程入口,常见的编程题

    1.设计一个从5个数中取最小数和最大数的程序. 2.#include<stdio.h> 3.int min(int a[],int i); 4.int max(int a[],int i) ...

  8. Excel 2010 如何在Excel的单元格中加入下拉选项

    http://jingyan.baidu.com/article/03b2f78c4ba8a05ea237ae95.html 第一步:打开excel文档,选中需加入下拉选项的单元格.   第二步:点击 ...

  9. 生命游戏/Game of Life的Java实现

    首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...

  10. Web和Native使用外部字体ttf方法

    论坛教程:http://bbs.egret.com/forum.php?mod=viewthread&tid=24879&extra=&highlight=%E5%AD%97% ...