dimanche 31 juillet 2016

How Can I Filter an Array from Another Array in Ember?

Goal: I have two arrays: One of vehicles scheduled for a day, and one of all vehicles. If a driver wants to use a vehicle on Monday, my program should check all driver's schedules for Monday and see what vehicles are being used and display a list of available vehicles BEFORE the user can select one (the select dropdown should never show anything that isn't available)

I can't seem to get Ember to filter the data. It's all or nothing. I can't get the filtered data. Are you willing to take a look?

Github Repository: [http://ift.tt/2aofTNh]

routes/schedules/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;
      }));
    });
  },




Aucun commentaire:

Enregistrer un commentaire