belongsTo属性上的Ember.computed.sort

时间:2018-03-23 16:48:14

标签: ember.js ember-model

我正在尝试使用Ember.computed.sort,但无法弄清楚如何按belongsTo属性的名称进行排序。我已经在GOogle上工作了几个小时,找不到合适的组合。请帮助!

// my model
export default DS.Model.extend({
        targetName: DS.attr(),
        targetType: DS.attr(),
        rack: DS.belongsTo('rack', {async: true}),
});

// this works:
export default Ember.Controller.extend({
    sortedTargets: Ember.computed.sort('model', 'sortDefinition'),
    sortDefinition: ['targetType']
});

// this doesn't work :
export default Ember.Controller.extend({
    sortedTargets: Ember.computed.sort('model', 'sortDefinition'),
    sortDefinition: ['rack.name']
});

1 个答案:

答案 0 :(得分:2)

感谢@Lux对this thread的评论。这是一个适合我的解决方案。请注意模型上的Ember.computed.alias。

export default DS.Model.extend({
    targetName: DS.attr(),
    targetType: DS.attr(),
    rack: DS.belongsTo('rack', {async: true}),
    rackName: Ember.computed.alias('rack.name'),
});

// this works:
export default Ember.Controller.extend({
    sortedTargets: Ember.computed.sort('model', 'sortDefinition'),
    sortDefinition: ['targetType']
});

// this works too :) :
export default Ember.Controller.extend({
    sortedTargets: Ember.computed.sort('model', 'sortDefinition'),
    sortDefinition: ['rackName']
});