对Promise的Ember对象数组进行排序 [英] Sort Ember Object Array with Promises

查看:98
本文介绍了对Promise的Ember对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模特 型号/人

I have a model model/person

{

  firstName: DS.attr( 'string'),
  lastName: DS.attr( 'string'),
  email: DS.attr( 'string' ),
}

和另一个模型 模型/项目

and another model model/project

{

  name:            DS.attr( 'string' ),
  code:            DS.attr( 'string' ),
  startDate:       DS.attr( 'date' ),
  endDate:         DS.attr( 'date' ),
  users : DS.hasMany('person', {async: true}),

}

然后,我将所有项目检索为包含ember对象的数组. 因为项目->用户是异步的,所以它是一个承诺.我想使用该人的名字对该数组进行排序.当数据到达相应位置并重新渲染使用列表的hbs

then i'm retrieving all the projects with as an array which contains ember objects. since the project -> users is async its a promise. and i want to sort that array using the first name of the person. when the data is arrived accordingly and re render the hbs that is using the list

我有一个称为的计算属性

i have a computed property called

renderProjects = computed ('model.projects.[]')
{
 // trying to sort in here but data is not avaiable so its not getting sorted
}

推荐答案

解决方案只是使用.@each:

renderProjects: computed ('model.projects.@each.firstName', function() {
  return this.users.sortBy('firstName');
})

这将在项目列表更改或任何项目上的任何firstName更改时重新计算renderProjects CP,然后自动更新视图.

this will recompute the renderProjects CP whenever the list of projects change or any firstName on any of the projects changes and then automagically update your view.

一个重要的提示:您不能.@each.foo.bar. 这就是您在 twiddle model.@each.myUser.name.

One important notice: You can not do .@each.foo.bar. This is what you did in your twiddle with model.@each.myUser.name.

最简单的解决方法是在video模型中添加computed.alias:

In your twiddle the easiest fix is to add a computed.alias to the video model:

username: computed.alias('myUser.name'),

然后您可以执行以下操作:

Then you can do this:

sortedVideos: computed('model.@each.username', function() {
  return this.get('model').sortBy('username');
})

这里是固定的旋转器.

这篇关于对Promise的Ember对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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