gpt4 book ai didi

javascript - Ember PromiseArray sortBy

转载 作者:行者123 更新时间:2023-12-03 21:19:54 25 4
gpt4 key购买 nike

好的,我有一个 Controller ,其模型设置如下:

App.MyModel = DS.Model.extend({
myOtherProperty: attr(),
mySortKey: attr(),
mySubCollection: function () {
return this.store.filter('MyOtherModel', function (myOtherModel) {
//Some basic filtering logic (it's actually more complex than this)
return myOtherModel.get('someProperty') == this.get('myOtherProperty');
});
}.property('myOtherProperty')
})

实际的过滤器函数并不重要——重要的是“mySubCollection”计算属性返回一个 DS.PromiseArray 对象。上述模型的 Controller 是“Ember.ObjectController”类型,因为它只显示一个对象。

显示此属性的模板使用 {{each}} 帮助器来执行此操作:

我的模板:

{{#each mySubCollection}}
{{view App.MyView}}
{{/each}}

现在 - 我需要按“mySortKey”属性降序对这个 promise 数组进行排序。我怎样才能做到这一点?文档说 DS.PromiseArray 从 Ember.Array 扩展而来,但是在 'mySubCollection' 的计算属性末尾添加 'sortBy('mySortKey:desc')' 会导致它中断并且根本不起作用 - 这是有道理的,因为我会根据 promise 而不是数组调用“sortBy”。

我研究了很多不同的想法,不断涌现的一个想法是改用 ArrayController。我可以做到,但它并不理想,因为它在一个非常简单的设置之上添加了一堆复杂性,而这个设置已经很好用了(除了排序)。

有什么想法吗?

最佳答案

我不太明白为什么 sortByPromiseArray 不起作用。但这是我用作解决方法的方法:

sortedCollection: function() {
return Em.ArrayProxy.createWithMixins(Em.SortableMixin, {
content: this.get('mySubCollection'),
sortProperties: [this.get('mySortKey')],
sortAscending: false
});
}.property('mySubCollection', 'mySortKey')

关于javascript - Ember PromiseArray sortBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22078197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com