了解Ember aray计算

时间:2013-12-24 11:28:07

标签: ember.js

这只是为了提高我的理解力。我正在学习余烬计算属性。我通过jonathan goldman复制并修改了幻灯片中的一个jsbin 这是我的代码 *
JSBIN

  

我理解根据计算属性和聚合数据的文档   http://emberjs.com/guides/object-model/computed-properties-and-aggregate-data/

1.The isDone property of any of the objects in the todos array changes.

2.An item is added to the todos array.

3.An item is removed from the todos array.

4.The todos property of the controller is changed to a different array.

与上面粘贴的bin一样如果我添加或删除任何Location,整个列表和分组列表都会按预期更新。

但是,当我将常规列表中的文本框中的任何名称编辑为现有名称时,不会更新“分组”列表。 说一般列表的初始状态是伦敦,纽约,悉尼 如果我试图将纽约改为伦敦。该组显示Londo(n missin)并且分组不会发生。

请找到以下代码。如果你想看到工作,请参阅上面链接的jsbin

 App.groupBy = function(dependentKey,property){
  var options = {
    initialValue: Em.A([]),
    initialize: function(initialValue,cm,im){
      return im.previousKeys={};
    },
    addedItem: function(accumulatedValue,item,cm,im){
      var key=item.get(property);
      var group = accumulatedValue.findBy('key',key);
      if(!group){
        group = Em.Object.create({
              key: key,
          numLocation:0 
        }); 
        accumulatedValue.pushObject(group);
      }
      group.incrementProperty('numLocation');
      im.previousKeys[guidFor(item)] = key;
      console.log(accumulatedValue);
      return accumulatedValue;
    },
    removedItem: function(accumulatedValue,item,cm,im){
      var key = item.get(property);
      var group = accumulatedValue.findBy('key',key);
      if(!group){ return; }
      var current = group.decrementProperty('numLocation');
      if(current ===0){
        accumulatedValue.removeObject(group);
      }

      return accumulatedValue;
    }
  };
   return Ember.arrayComputed(dependentKey, options);
};

和我的IndexController

App.IndexController = Em.Controller.extend({ 
  selected: App.groupBy('model.@each.location','location'),
  actions:{
    addOne: function(){
      this.get('model').pushObject(
        App.Location.create({
          name: this.get('newName'),
          location: this.get('newLocation')
        })
      );
    },
    del:function(obj){
      this.get('model').removeObject(obj);
    }
  }
}); 

0 个答案:

没有答案