I am working with Ember.js using Ember-Data and [Kurko's Ember Data IndexedDB Adapter] (https://github.com/kurko/ember-indexeddb-adapter) with multiple hasMany relationships. I am able to successfully add hasMany relationships without any issues. My issue lies with removing the hasMany relationships.
App.Race = DS.Model.extend({
name: DS.attr('string'),
...
athletes: DS.hasMany('athlete', {async: true}),
...
});
App.Athlete = DS.Model.extend({
bib: DS.attr('number'),
name: DS.attr('string'),
...
splits: DS.hasMany('split', {async: true}),
races: DS.hasMany('race', {async: true}),
...
});
App.Split = DS.Model.extend({
distance: DS.attr('string'),
time: DS.attr('string'),
athlete: DS.belongsTo('athlete', {async:true}),
race: DS.belongsTo('race', {async: true}),
...
});
On on occasion the athlete
reference is not removed from the specified race
model when deleting athletes, breaking the application. The athlete
model is always deleted, and it always deletes all of the necessary splits
models. I know that it has something to do with the asynchronous nature of my storage, but I cannot isolate the issue in the action:
removeAthlete: function() {
var self = this,
athlete = this.get('model');
// Get race from athlete
athlete.get('races').then(function(races){
// Remove athlete from races
races = races.toArray();
races.forEach(function(race){
console.log('removing athlete from race');
race.get('athletes').removeObject(athlete);
race.save();
});
// destroy splits
athlete.get('splits').then(function(splits) {
console.log('retrieved splits');
splits.toArray().forEach(function(split) {
console.log('removing split');
split.destroyRecord();
});
// destroy athlete
athlete.destroyRecord();
});
});
},
...
Edit
After some attempts with notifications, I have realized that the error has to do with the removal of the athlete
reference from the race. I will give more updates as I progress.
race.get('athletes').removeObject(athlete);
? That will retrieve theathletes
RecordArray
from the given race and directly modify that object, but Ember doesn't know you modified it because you're callingget
and notset
. Can you try explicitly adding a call tonotifyPropertyChange
(emberjs.com/api/classes/…), something likerace.notifyPropertyChange('athletes');
to see if that makes a difference? – Josh Padnickrace.save();
so I will give a few test runs and let you know. I don't if that is where the problem is though. The DOM removes the itemview for the athlete, and the athlete is deleted, but the reference to that athlete still remains inrace.get('athletes')
. So it references anathlete that no longer exists
– DFenstermacherrace.notifyPropertyChange('athletes');
that did not fix the problem. The problem I think is in the removal of the athlete itself, not the notification of changes. I am making changes to my post to reflect this. – DFenstermacher