zoukankan      html  css  js  c++  java
  • 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中删除一项。
      • 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'); //0
    
    todo = Ember.Object.Create({ isDone: false });
    todos.pushObject(todo);
    
    todosController.get('remaining');//1

    3. 请注意@each不能嵌套。

    正确:todos@each.owner.name

    错误:todos@each.owner.@each.name

  • 相关阅读:
    docker使用以及dockerfile编写
    c++:空构造空析构的益处之一
    python os.path模块常用方法详解(转)
    enlarge your dataset
    解决镜像无法删除的问题multiple repositories
    Ubuntu 14.04 LTS 安装Docker(转)
    忘记root密码,怎么办
    [Unity3D]降低向Shader中传值的开销
    Shader预处理宏、内置状态变量、多版本编译等
    Unity Shader 常用函数列表
  • 原文地址:https://www.cnblogs.com/sunshineground/p/5147344.html
Copyright © 2011-2022 走看看