2015-07-03 35 views
0

我有一個routedestination模型,並想創建一個filteredRoutes計算屬性,它僅包含routes其中有一個destination.nameselectedDestination值。我無法弄清楚最後一塊拼圖。我如何過濾?過濾通過的hasMany連接

controller.js

filteredRoutes: Ember.computed('model.routes', 'selectedDestination', function() { 
    var selectedDestination = this.get('selectedDestination'); 
    var routes = this.get('model.routes'); 

    if(selectedDestination) { 
     routes = routes.filter(function(route) { 
     // ???? 
     }); 
    } 
    }), 

應用程序/目的地/ model.js

import DS from 'ember-data'; 

export default DS.Model.extend({ 
    name: DS.attr('string') 
}); 

應用程序/路線/ model.js

import DS from 'ember-data'; 

export default DS.Model.extend({ 
    name: DS.attr('string'), 
    destinations: DS.hasMany('destinations', { async: true }) 
}); 

回答

1

使用filter,正如您所建議的那樣,過濾條件是至少存在一個名稱與選定名稱相同的目的地。

filteredRoutes: Ember.computed(
    '[email protected]@each.name', 
    'selectedDestination', 

    function() { 
    var selectedDestination = this.get('selectedDestination'); 

    return this.get('model.routes') . filter(
     route => 
      route.get('destinations') . find(
      destination => 
       destination.get('name') === selectedDestination 
     ) 
    ); 
    } 
) 

英文:

查找具有至少一個目的地名稱的路線是一樣的選擇的目的地。