简体   繁体   English

EmberJS如何在单个排序的数组内的同一路线上加载多个模型

[英]EmberJS How to load multiple models on the same route inside a single sorted array

I would like to display a merged list of item from different models. 我想显示来自不同模型的项目的合并列表。

I found out here that using Ember.RSVP.hash is the trick to load multiple models. 我在这里发现,使用Ember.RSVP.hash是加载多个模型的窍门。 It's not enough for me since I want to have all of them in a single list. 这对我来说还不够,因为我想将所有这些都放在一个列表中。

How can I merge all the model together and sort them? 如何将所有模型合并在一起并对它们进行排序?

I recommend using a Computed.property which monitors the models that are changing and combines them into the array you need in your template . 我建议使用Computed.property来监视正在更改的模型,并将它们组合到template所需的数组中。

When modelA or modelB changes your computed property will update with those results. modelAmodelB更改时,您的计算属性将使用这些结果进行更新。

myList: Ember.computed('modelA', 'modelB', function() {
    let combinedModels = [];
    this.get('modelA').forEach( item => {
        // pull out what you need from each model item
        combinedModels.push(item);
    });
    this.get('modelB').forEach( item => {
        // pull out what you need from each model item
        combinedModels.push(item);
    });
    return combinedModels;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM