【发布时间】:2015-01-22 14:13:58
【问题描述】:
当我尝试删除其模型与另一个模型具有多对多关系的记录时,我偶然发现了一个问题。
我有书:
App.Book = DS.Model.extend({
title: DS.attr('string'),
isbn: DS.attr('string'),
category: DS.attr('string'),
publishDate: DS.attr('date'),
authors: DS.hasMany('author', {inverse: 'books'})
});
以及作者:
App.Author = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
birthDate: DS.attr('date'),
books: DS.hasMany('book', {async: true, inverse: 'authors'})
});
我正在删除这样的书:
actions: {
delete: function (book) {
var authors = book.get('authors')
authors.forEach(function(author) {
var books = author.get('books')
books.forEach(function(book){
console.log(book.toJSON());
})
books.removeObject(book);
//books.save doesn't work
})
book.destroyRecord();
this.transitionToRoute('books.index');
},
它正确地删除了这本书,并向支持 REST 服务器发出了 DELETE 请求,但是对于所有在他们的“书籍”集合中拥有这本书的作者没有 PUT 请求。当我将视图更改为作者并返回书籍时,会创建一本带有我之前删除的书籍的 id 的虚拟书籍,并且“作者”也设置为旧作者,其他属性未定义。
如何正确删除图书以便作者也得到更新?
【问题讨论】:
-
您使用 Ember Data Beta 14 吗?当您看到“虚拟”书时,您是否在控制台中收到任何错误?
标签: ember.js many-to-many ember-data