【发布时间】:2013-12-06 22:30:48
【问题描述】:
我有一个数组,我想将其项目分组,然后以这种分组方式显示。这一切都非常令人困惑:
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):
{{#each model }}
{{#each content }}
{{#each}}
为什么? ArrayController 不应该代理“model”(即 GroupedThings),而应该代理“content”吗?
这成为问题的原因是我想要对这些组进行排序,并且一旦我更改整个内容数组(即使使用 ArrayProxy.replaceContent()),整个视图都会重新构建,即使只有一个项目被添加到单个组中。
有完全不同的方法吗?
【问题讨论】:
标签: javascript ember.js