2016-09-29 208 views
0

首先使用计算性能,我目前的版本:在页面加载

Ember  : 2.8.1 
Ember Data : 2.8.1 
jQuery  : 3.1.1 

我使用的查询参数中的控制器。如果用户转到assignments?strm=1292,将调用strm,计算得出term,和计算filteredClusters。但是,在初始页面加载时,filteredClusters将为空(例如,我使用浏览器导航到/assignments?strm=1292)。只有在调用selectStrm动作来更新strm查询参数后,filteredClusters才会开始返回结果。

assignments指数控制列表如下:

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    queryParams: ['strm', 'cluster', 'title'], 
    strm: null, 
    cluster: null, 
    title: null, 

    term: Ember.computed('strm', function() { 
    var strm = this.get('strm'); 
    var terms = this.get('model.terms'); 

    return strm ? terms.filterBy('strm', strm)[0] : null; 
    }), 

    filteredClusters: Ember.computed('term', 'model', function() { 
    var term = this.get('term'); 
    var clusters = this.get('model.clusters'); 

    if(term) { 
     return clusters.filter((cluster, index, clusters) => { 
     var terms = cluster.get('terms'); 

     return terms.mapBy('strm').includes(term.get('strm')); 
     }); 
    } else { 
     return clusters; 
    } 
    }), 

    actions: { 
    selectStrm: function(strms) { 
     var strm = strms[0]; 
     this.set('strm', strm); 
    } 
    } 
}); 

我能做些什么让filteredClusters加载在初始页面加载?

感谢您的任何和所有信息。

+0

尝试在routes/assignments.js中定义'queryParams:{strm:{refreshModel:true},cluster:{refreshModel:true},title:{refreshModel:true}}' – kumkanillam

回答

1

可能你的问题是filteredClusters有错误的依赖关系。如果model可用,是否确定每个群集上的model.clustersterms已经加载?是异步关系吗?

可能您需要一个CP依赖项,如[email protected]@each.strm。然而,这将不工作,因为不支持双@each

现在让我们假设你有三个型号:assignmentclusterterm

分配

clusters: DS.hasMany('cluster') 

集群

terms: DS.hasMany('term') 

长期

strm: DS.attr() 

现在该怎么摆脱这个双@each?答案很简单:对返回所有集群中的所有条款的分配的CP:

分配

terms: Ember.computed('[email protected]', { 
    get() { 
    return get(this, 'clusters') 
     .map(c => get(c, 'terms')) 
     .reduce((a,b) => [...a, ...b], []); 
    } 
}), 

,现在你可以使用[email protected]作为filteredClusters依赖的关键。这将迫使CP在群集或术语加载时重新计算。