问题:
_activeAuthor.get('books').pushObject(book).save();
在Chrome中处理时没有错误,但该书未添加到Ember-Data的_activeAuthor实例的books属性中。我不明白为什么?this.modelFor('user').get('latestChapter');
似乎返回了一个承诺。我该如何处理get()
的投标?代码:
createChapter: function() {
//Getting the Author of the latestChapter or getting the first Author in the array
var _activeAuthor = null;
var authors = this.modelFor('user').get('authors').toArray();
var latestChapter = this.modelFor('user').get('latestChapter');
var latestAuthor = latestChapter.get('author');
if (latestChapter.content) {
_activeAuthor = latestAuthor;
} else {
_activeAuthor= authors[0];
}
var book = this.store.createRecord('book', {
title: 'click here to name your book',
author: _activeAuthor,
});
var chapter = this.store.createRecord('chapter', {
title: 'Click here to name your chapter',
book: book, // Add the created Book to the Book property of the Chapter instance
});
_activeAuthor.get('books').pushObject(book).save();
chapter.save();
book.save();
this.modelFor('user').set('latestChapter', chapter).save() //Identifying the latest created chapter at the lastestChapter;
console.log('New chapter created: ' + chapter.get('id'));
},
型号:
App.Author = DS.Model.extend({
type: DS.attr('string'),
authorTitle: DS.attr('string'),
userTitle: DS.attr('string'),
description: DS.attr('string'),
user: DS.belongsTo('user', {inverse: 'authors', async: true}),
books: DS.hasMany('book', { inverse: 'author', async: true}),
});
App.Book = DS.Model.extend({
title: DS.attr('string'),
icon: DS.attr('string'),
description: DS.attr('string'),
frequency: DS.attr('string'),
chapters: DS.hasMany('chapter', { inverse: 'book', async: true}),
author: DS.belongsTo('author', { inverse: 'books', async: true}),
});
App.Chapter = DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string'),
frequency: DS.attr('string'),
unit: DS.attr('string'),
aggregationMode: DS.attr('string'),
dashboard: DS.attr('boolean'),
statData : DS.attr('array'),
book: DS.belongsTo('book', { inverse: 'chapters', async: true}),
});
谢谢!
答案 0 :(得分:1)
1。
author.get('books')
将返回一个承诺,所以你可能要做的就是
author.get('books').then(function(books) {
books.pushObject(book)
});
author.save();
如果这不是问题,你可以给一个jsfiddle整个应用程序代码吗?然后,它会更容易帮助! :)
2。
每次get
模型的属性为async
而不是isLoaded
(未与服务器同步)时,ember会询问服务器,是的,将填写记录中的你的商店是一种理想的行为:)
3。
如果你有一个async
模型属性,那么你总是得到一个承诺,所以你应该以这种方式处理它:
chapter.get('book').then(function(book) {
// here's a book
});
BTW var latestAuthor = latestChapter.get('author');
- > chapter
没有author
属性:)