DeleteRecord with multiple attributes belongs to relations in ember-cli

What is the best ember-cli practice for removing Record () for a model that belongs to multiple models? Do I have to manually clear relationships with my parents?

Migrating from ember to ember-cli, I am having new problems with deleteRecord () for the star model, which belongs to several models, the post and the user. Before moving on to ember cli, he worked with this solution .

The delete action of the previous solution does not work in the current ember-cli with errors, but never causes api , TypeError: Cannot read property 'modelFor' of undefined and Uncaught Error: Assertion Failed: TypeError: Cannot read property 'modelFor' of undefined in the line

 var inverse = relationship.parentType.inverseFor(name); // name is the string 'post' 

Now I just start again. Here is a simple example of what I'm trying. Maybe I am missing something with es6, explicit inversions, or using needs: :?

http://localhost:4200/post/1

deleteRecord () on child model not cleaning parent relations

model

 // models/star.js import DS from 'ember-data'; export default DS.Model.extend({ created: DS.attr('date'), post: DS.belongsTo('post', { async: true, inverse: 'stars' }), user: DS.belongsTo('user', { async: true, inverse: 'stars' }) }); // models/post.js import DS from 'ember-data'; export default DS.Model.extend({ headline: DS.attr(), body: DS.attr(), stars: DS.hasMany('star', { async: true }) }); // models/users.js import DS from 'ember-data'; export default DS.Model.extend({ username: DS.attr(), stars: DS.hasMany('star', { async: true }) }); 

controller

 //controllers/post.js import Ember from 'ember'; export default Ember.ObjectController.extend({ actions: { createStar: function(){ var self=this, post = this.get('model'), user = this.store.find('user', 2), star; user.then( function(user){ star = self.get('store').createRecord('star', { post: post, user: user }); star.save().then( function(star){ post.get('stars').then( function(stars){ stars.pushObject(star); }); user.get('stars').then( function(stars){ stars.pushObject(star); }); }); }); }, deleteStar: function() { var user = this.store.find('user', 2), self = this; user.then( function(user){ var filtered = self.get('stars').filterProperty('user.id', user.id); var star = filtered[0]; star.deleteRecord(); star.save(); }); return(false); } } }); 

Update: alternative deleteStar post controller action re: @jjwon

 deleteStar: function() { var user = this.store.find('user', 2), self = this; user.then( function(user){ var stars = self.get('stars').then( function(items){ // log the post stars before delete items.forEach(function(item) { console.log(item); }); var filtered = items.filterBy('user.id', user.id); var star = filtered.get('firstObject'); star.deleteRecord(); star.save().then(function(){ // log the post stars after delete items.forEach(function(item) { console.log(item); }); }); }); }); return(false); } 

Interestingly, I found that if I add a star, reload the page and then delete it so that the star is successfully removed from the columns. Excellent!

But if I add a star and delete it without rebooting, there will still be a link to the remote star with its id among the post stars. . Looking at the console, the remote star object still refers to the message with its id , but the user and post attributes are undefined .

+1
source share
2 answers

stars is an asynchronous user attribute. Therefore, when you have:

 var filtered = self.get('stars').filterProperty('user.id', user.id); var star = filtered[0]; star.deleteRecord(); star.save(); 

filtered may be a promise, so it may not work as expected. Also, I'm not very familiar with filterProperty, but you don't have the user.id attribute, so it seems like it can return an empty list?

0
source

At the moment, I’m very carefully cleaning my relationship with my parents. It works, but not perfect.

 deleteStar: function() { var user = this.store.find('user', 2), self = this; user.then( function(user){ var star = self.get('stars').filterBy('user.id', user.id).get('firstObject'); // clean up relationships on parents very manually // user.stars user.get('stars').removeObject(star); // posts.stars self.get('stars').removeObject(star); star.deleteRecord(); star.save(); }); return(false); } 
0
source

Source: https://habr.com/ru/post/970751/


All Articles