lundi 19 août 2019

Ember reflexive relationship reloaded after removing it

In my app, recipes has a reflexive relationship called linkedRecipes. I have set the inverse to null. If a recipe has 3 linked recipes, and I remove 2 of them, everything works as intended. However whenever I try to delete the last recipe, whether that means deleting 3 of 3 linked recipes, or deleting 1 of 1, the linked recipe(s) will remain there on save, and will be sent in the PUT request for updating a recipe.

I have gone into the Ember inspector and watched the recipes linkedRecipes length become 0 after the removeLinkedRecipe function, but during the saveExistingRecipe function, and in the PUT request, the linked recipes still show as what they were before the removeLinkedRecipe function.

recipe.js:

export default DS.Model.extend({
  linkedRecipes: DS.hasMany('recipe', { async: true, inverse: 'null' }),
});

recipe-viewer.js:

removeLinkedRecipe: function(recipeId) {
  var self = this;
  this.get('model.linkedRecipes').forEach(function(linkedRecipe) {
    if (linkedRecipe.get('id') == recipeId) {
      self.get('model.linkedRecipes').removeObject(linkedRecipe);
    }
  });
}
saveExistingRecipe: function() {
  return $.ajax({
    url: '/api/v1/recipes/' + this.get('model.id'),
      type: 'PUT',
      data: { recipe: this.get('model').serialize() 
    }
  });
},

I'm working on a large application, and I'm not sure what other code would be helpful to show. Let me know if you need to see anything else.




Aucun commentaire:

Enregistrer un commentaire