Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 当要聚合的数据的值未知时,是否有方法聚合数据?_Ember.js - Fatal编程技术网

Ember.js 当要聚合的数据的值未知时,是否有方法聚合数据?

Ember.js 当要聚合的数据的值未知时,是否有方法聚合数据?,ember.js,Ember.js,我很想知道是否有一种方法可以在不知道要计算的属性值的情况下创建计算属性。例如,如果我有以下模型: TR.location = DS.Model.extend({ name: DS.attr('string'), city: DS.attr('string'), state: DS.attr('string'), type: DS.attr('string') }); 使用以下固定装置: TR.location.FIXTURES = [ { id: 1,

我很想知道是否有一种方法可以在不知道要计算的属性值的情况下创建计算属性。例如,如果我有以下模型:

TR.location = DS.Model.extend({
    name: DS.attr('string'),
    city: DS.attr('string'),
    state: DS.attr('string'),
    type: DS.attr('string')
});
使用以下固定装置:

TR.location.FIXTURES = [
    { id: 1, name: 'The Four Seasons', city: 'New York', state: 'NY', type: 'Restaurant' },
    { id: 2, name: 'Le Cirque', city: 'New York', state: 'NY', type: 'Restaurant' },
    { id: 3, name: 'The Russian Tea Room', city: 'New York', state: 'NY', type: 'Restaurant' },
    { id: 4, name: 'The Waldorf Astoria', city: 'New York', state: 'NY', type: 'Hotel' },
    { id: 5, name: 'The Plaza', city: 'New York', state: 'NY', type: 'Hotel' }
];
我想根据“type”属性显示总计。因此,根据上述信息,我希望在我的模板中显示以下内容:

餐厅:3间 酒店:2
这里的问题是没有关于“type”属性的值可能是什么的领域知识,这就是为什么我不知道如何为上面的属性创建计算属性。有什么解决办法吗?提前谢谢。

filterProperty是您的朋友。看

直列钻孔

 var models = TR.location.find();

 var hotelCount = models.filterProperty('type','Hotel').get('length);
 var restaurantCount = models.filterProperty('type', 'Restaurant').get('length');
正如已经指出的,您可能不知道这些类型是什么,在这个特定的例子中,对于性能,我将进行一次观察,迭代列表,然后将它们相加。基本上,只要更新模型中任何项的类型,您的属性“someCustomCounts”也将更新

 someCustomCounts: function(){
   var agg= {};
   this.get('model').forEach(function(item){
     var type = item.get('type');
     if(!agg[type]) agg[type] = 0;
     agg[type]++;
   });
   return agg;
 }.observes('model.@each.type'),
在某些arraycontroller上,当模型设置为位置列表时,计算属性

 hotelCount: function(){
    return this.get('model').filterProperty('type','Hotel').get('length);
 }.property('model'),

 restaurantCount: function(){
    return this.get('model').filterProperty('type','Restaurant').get('length);
 }.property('model'),

根据要进行的聚合的数量,一个好的选择可能是使用Ember.Map,因为它的结构非常适合这些问题

关于您遇到的具体问题,我们可以这样做:

groupCount: function() {
  var listResult = [];
  // aggregating data
  var mapResult = this.get('content').mapProperty('type').reduce(function(previousValue, item, index, enumerable) {
    var currentItemCount = (previousValue.get(item) || 0);
    previousValue.set(item, currentItemCount + 1);
    return previousValue;
  }, Ember.Map.create());
  // prepare data for displaying in the template
  mapResult.forEach(function(key, value) {
    listResult.push({type: key, count: value});
  });
  return listResult;
}.property('content.@each.type')
computed属性的第一部分负责根据需要聚合信息,第二部分设置数据以在模板中显示。如果数组更改,也会重新计算这些值


下面是一个jsbin示例:

非常好的解决方案。我将尝试实现这一点。谢谢