2016-07-31 67 views
0

目標: 我有兩個陣列:預定一天的車輛之一,和所有車輛之一。如果司機想在星期一使用車輛,我的計劃應該檢查週一所有司機的時間表,並查看正在使用哪些車輛,並顯示可用車輛列表之前,用戶可以選擇一個(選擇下拉菜單應該永遠不會顯示任何不是可用)如何從Ember中的另一個數組過濾數組?

我似乎無法得到Ember過濾數據。這是全部或沒有。我無法獲得過濾的數據。你願意看看嗎?

Github上庫:https://github.com/djbreen7/ejs-taxi-app/blob/master/app/routes/schedules/view.js][1]

路線/日程安排/ view.js

model(params) { 
    return this.store.findRecord('schedule', params.schedule_id); 
    }, 

    setupController(controller, model) { 
    controller.set('schedule', model); 

    var self = this; 
    this.store.findAll('schedule').then(function(schedules) { 
     controller.set('schedules', schedules); 
     var scheduledVehicles = schedules.filter(function(day) { 
     if (day.get('day_of_week') === model.get('day_of_week') && day.get('vehicle.id')) { 
      return self.store.findRecord('vehicle', day.get('vehicle.id')); 
     } 
     }); 
     var vehicles = self.store.findAll('vehicle'); 
     controller.set('vehicles', vehicles.filter(function(vehicle) { 
     return scheduledVehicles.indexOf(vehicle) === -1; 
     })); 
    }); 
    }, 

回答

0

我認爲所有這些過濾應在後端服務器完成。

無論如何,爲了在客戶端做到這一點,你不應該在setupController中使用異步。我的意思是不要在setupController中使用燼數據查找。

model(params){ 
    return Ember.RSVP.hash({ 
    schedule: this.store.findRecord('schedule', params.schedule_id), 
    allSchedules: this.store.findAll('schedule'), 
    allVehicles: this.store.findAll('vehicle') 
    }); 
} 

setupController(controller, model) { 
    controller.set('schedule', model.schedule); 
    controller.set('schedules', model.allSchedules); 

    var self = this; 
    var scheduledVehicles = schedules.filter(function(day) { 
     if (day.get('day_of_week') === model.get('day_of_week') && day.get('vehicle.id')) { 
      return self.store.peekRecord('vehicle', day.get('vehicle.id')); 
     } 
     }); 
    var destVehicles = model.allVehicles.filter(function(vehicle) { 
     return scheduledVehicles.indexOf(vehicle) === -1; 
     })); 
    controller.set('vehicles', destVehicles); 
} 

同樣對於這種用法我建議使用computed屬性置於控制器甚至。

+0

謝謝。我會給它一個鏡頭。我曾嘗試過'Ember.RSVP.hash',但如果刷新瀏覽器,我遇到了模型消失的問題。 –

相關問題