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. C语言中do...while(0)的妙用-避免goto

    使用goto的优雅并避免结构的混乱 将要跳转到的语句用do{-}while(0) 包起来就可以. reference #defien N 10 bool Execute() { // 分配资源 int ...

  2. laravel 社会化(联合)登录扩展包(QQ、微信、微博等)

    laravel的官方包只支付国外网站的联合登录:http://laravelacademy.org/post/6288.html 国内用户的话,可以用这个:https://github.com/ove ...

  3. 【RF库Collections测试】Set To Dictionary

    Name:Set To DictionarySource:Collections <test library>Arguments:[ dictionary | *key_value_pai ...

  4. Activity、Window和View三者间的关系有一定的见解

    一.简述如何将Activity展现在手机上 Tips: Activity本身是没办法处理显示什么控件(view)的,是通过PhoneWindow进行显示的 换句话说:activity就是在造Phone ...

  5. nano100B的看门狗讲解

    看门狗定时器的用途是在软件出问题时执行系统复位功能,这可以防止系统无限期地挂起.除此之外,看门狗定时器还支持将CPU 从掉电模式唤醒的功能.看门狗定时器包含一个18 位的自由运行计数器,定时溢出间隔可 ...

  6. 开源免费天气预报接口API以及全国所有地区代码[值得收藏]

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  7. IOS视频播放器的制作

    利用自带MPMoviePlayerController来实现视频播放,首先要在项目中导入MediaPlayer.Framework框架包. 在视图控制器中 #import "MediaPla ...

  8. C++模板中的嵌套

    在下面的程序中,我们创建了一个模板类用于实现Queue容器的部分功能,并且在模板类中潜逃使用了一个Node类.queuetp.h // queuetp.h -- queue template with ...

  9. JS 保存表单默认值 为空时自动填充默认值

    var inputArray = document.getElementsByTagName("input"); var strArray = []; ; i < input ...

  10. 几种减小javascript对性能影响的方法

    1.将所有的script标签放在页面的底部,body的结束标签</body>之前. 2.将脚本打包,script标签越少,请求数就越少,加载速度加快,相应的响应时间变短. 3.使用非阻塞的 ...