EmberJS compute.sort-按关联的模型属性排序 [英] EmberJS computed.sort - sorting by associated model property

查看:106
本文介绍了EmberJS compute.sort-按关联的模型属性排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的belongsTo模型关系:

I have a simple belongsTo model relationship:

contract.js:

contract.js:

export default DS.Model.extend({
    object               : DS.belongsTo('object'),
    ....
})

object.js:

object.js:

export default DS.Model.extend({
    street              : DS.attr('string'),
    zip                 : DS.attr('string'),
    city                : DS.attr('string'),
    ...
})

在我的拥有很多contracts的实体的控制器中,我想按关联的object的街道名称进行排序,但是不知何故

In my controller for an entity that holds many contracts, I'd like to sort by the street name of the associated object, but somehow this

export default Ember.Controller.extend({

    sortOrder: ['object.street'],
    sortedObjects: Ember.computed.sort('model.contracts', 'sortOrder')

    ...
});

不起作用.

使用自定义比较器函数la

Using a custom comparator function a la

function(a, b){
    if (a.street > b.street) {
      return 1;
    } else if (a.street < b.street) {
      return -1;
    }
}

我发现abPromises,但是以某种方式我看不到如何通过嵌套属性(对象的街道)对它们进行排序

I found out that a and b are Promises, but somehow I don't see how to get them sorted via a nested attribute (the object's street)

要进一步澄清代码:

contracts : Ember.computed.alias('model.contracts'),
street: Ember.computed.alias('realty.street'),

sortOrder: ['realty.street'],
sortedOwnedRealties: Ember.computed.sort('contracts.@each.street', function (a, b) {
    console.log(Ember.get(a, 'id'));
    console.log(Ember.get(a, 'street'));

    //return 1;
})

该功能将undefined打印到street的控制台,但打印正确的id.

That function prints undefined to the console for the street, but the correct id.

为清楚起见,我将object重命名为realty.

I've renamed object to realty for clarity.

推荐答案

它们可能是PromiseObject,它们都是Ember.ObjectPromise.您所做的一些事情可能不是一个好主意:

They are probably PromiseObjects, which are both, Ember.Object and Promise. There are a few things you do that might be not a good idea:

第一个object是保留关键字.即使模型或关系可能起作用,也不建议使用名为object的模型或关系.

First object is a reserved keyword. It's not a good idea to have a model or relationship named object, even if it might work.

第二,您的计算属性具有错误的依赖项键.实际上,您希望使用类似'modelcontracts.@each.object.street'而不是'modelcontracts'的方式,但这将起作用,因为不支持'@each.a.b'.解决方案是在合同上为街道创建别名:

Second your computed property has the wrong dependency key. Actually you would want something like 'modelcontracts.@each.object.street' instead of 'modelcontracts', however this will not work because '@each.a.b' is not supported. The solution is to create an alias to the street on the contract:

street: Ember.computed.alias('object.street'),

然后您可以将其用作依赖项键:modelcontracts.@each.street.

And then you can use this as your dependency key: modelcontracts.@each.street.

接下来,在自定义比较器函数中,您将通过点表示法访问a.street.这将不起作用,并且对于余烬对象而言是不可靠的.始终使用.get()访问属性:

Next in your custom comparator function you access a.street by dot notation. This will not work and is unreliable for ember objects. Always use .get() to access the properties:

import Ember form 'ember';
const {get} = Ember;


function(a, b){
  if(get(a, 'street') > get(b, 'street')) {...}
}

但是,还请注意,没有上面已经提到的computed.alias,它不是get(a, 'street')而是get(a, 'object.street').

However also notice that without the computed.alias I've mentioned above it's not get(a, 'street') but get(a, 'object.street').

为什么您的代码无法正常工作的问题可能是object是异步加载的,因此在评估CP时,尚未加载街道.添加正确的依赖项密钥可能会解决此问题.

The problem why your code is not working is probably that object is loaded async, and so at the time your CP is evaluated the street is not yet loaded. Adding the correct dependency key will probably fix this.

这篇关于EmberJS compute.sort-按关联的模型属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆