Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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
Javascript Ember.js:a组模型(使用ArrayProxy)_Javascript_Ember.js - Fatal编程技术网

Javascript Ember.js:a组模型(使用ArrayProxy)

Javascript Ember.js:a组模型(使用ArrayProxy),javascript,ember.js,Javascript,Ember.js,我有一个数组,要对其项进行分组,然后以这种分组方式显示。这一切都令人非常困惑: App.GroupedThings = Ember.ArrayProxy.extend({ init: function(modelToStartWith) { this.set('content', Ember.A()); this.itemsByGroup = {}; modelToStartWith.addArrayObserver(this, {

我有一个数组,要对其项进行分组,然后以这种分组方式显示。这一切都令人非常困惑:

App.GroupedThings = Ember.ArrayProxy.extend({
  init: function(modelToStartWith) {
    this.set('content', Ember.A());        
    this.itemsByGroup = {};  

    modelToStartWith.addArrayObserver(this, {      
      willChange: function(array, offset, removeCount, addCount) {},
      didChange: function(array, offset, removeCount, addCount) {
        if (addCount > 0)
          // Sort the newly added items into groups
          this.add(array.slice(offset, offset + addCount))
      }
    });
  },

  add : function(things) {
    var this$ = this;

    // Group all passed things by day    
    things.forEach(function(thing) {
      var groupKey = thing.get('date').clone().hours(0).minutes(0).seconds(0);

      // Create data structure for new groups
      if (!this$.itemsByGroup[groupKey]) {
        var newArray = Ember.A();
        this$.itemsByGroup[groupKey] = newArray;
        this$.get('content').pushObject({'date': groupKey, 'items': newArray});
      }

      // Add to correct group
      this$.itemsByGroup[groupKey].pushObject(thing);
    });
  }
});


App.ThingsRoute = Ember.Route.extend({
  model: function() {
    return new App.GroupedThings(this.store.find('thing'));
  },
});
这仅在我使用以下模板时有效:

{{#each model.content }}
它们不会呈现任何内容(使用ArrayController):

为什么??ArrayController不应该代理“模型”(即GroupedThings)吗?后者应该代理“内容”

这成为一个问题的原因是,我想对这些组进行排序,一旦我更改了整个内容数组(甚至使用了ArrayProxy.replaceContent()),即使只向单个组添加了一个项,也会重新生成整个视图


有没有一种完全不同的方法呢?

在做这些事情时,我倾向于稍微不同地使用ArrayProxies。 我可能会让Ember完成所有繁重的工作,为了排序,我会让它围绕内容集合创建ArrayProxies,这样您就可以自动对它们进行排序:

(注意,我还没有运行此代码,但它应该会将您推向正确的方向)

然后创建一个GroupedThings实例,如下所示:

var aGroupedThings = App.GroupedThings.create({content: someArrayOfItemsThatYouWantGroupedThatHaveBothSortKeyAndGroupKeyAttributes, sortKey: 'someSortKey', groupKey: 'someGroupKey'});
如果您想获取groupedContent,只需获取实例并获取('groupedContent')。轻松点!:)

…它将保持分组和排序(计算属性的威力)。。。如果需要“添加”便利方法,可以将其添加到上面的Em.Object.Extend def中,但也可以轻松地使用Em.ArrayProxy中的本机方法,这些方法更好:

aGroupedThings.addObjects([some, set, of, objects]);

H2H

var aGroupedThings = App.GroupedThings.create({content: someArrayOfItemsThatYouWantGroupedThatHaveBothSortKeyAndGroupKeyAttributes, sortKey: 'someSortKey', groupKey: 'someGroupKey'});
aGroupedThings.addObjects([some, set, of, objects]);
aGroupedThings.addObject(aSingleObject);