简体   繁体   中英

Deleterecord is not deleting object from the HasMany array

I have an event model which has a relationship to a rider model. The event model has a property called riders that is a hasMany of riders and it is also async. The rider model has a property called event that is a belongTo and it contains the proper event that it belongs to. I am loading all of my data from my own REST API that is ran via Node.js and Express 4.

Here is my deletion code:

import Ember from 'ember';

export default Ember.Controller.extend({
    actions: {
        deleteRider: function () {
            var model = this.get('model');
            var evt = model.get('event');

            model.deleteRecord();
            model.save().then(function() {
                evt.save();
            });
        }
    }
});

models/event.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    city: DS.attr('string'),
    state: DS.attr('string'),
    date: DS.attr('string'),
    riders: DS.hasMany('rider', {
        async:true,
        inverse:'event'
    })
});

models/rider.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    city: DS.attr('string'),
    state: DS.attr('string'),
    BB: DS.attr('boolean'),
    SB: DS.attr('boolean'),
    BU: DS.attr('boolean'),
    event: DS.belongsTo('event')
});

Im having problems to understand what are u exactly trying to delete.

But the way i approach this is. There's an array of Model instances and there's an array of toDeleteModelInstances. The first array is shown on template using each method. And during the delete of each instance you use toDeleteModelInstancesArray.pushObject(selectedInstance)

Looks something like this.

Delete

deleteFieldset: function(givenRecord){

     //remove from Each array then push to array containing deleted records
    this.get('controller.markedForDeleted.pushObject(givenRecord));
    // You now have a record in given array [class,class]
}

Now on EDIT action

      var personsToDelete = self.markedForDeleted.map(function(item){ 
        return item.destroyRecord();
      });

      // Checks for deletedInstances array 
      if(Ember.isEmpty(personsToDelete)){
        deletePersonSuccess();
      } else {
        Ember.RSVP.all(personsToDelete).then(deletePersonSuccess, deletePersonFailure);
      }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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