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);
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

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){
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 .