質問:
- このコード行は
_activeAuthor.get('books').pushObject(book).save();
Chrome でエラーなく処理されますが、本は Ember-Data の _activeAuthor インスタンスの books プロパティに追加されません。なぜだか分からない? - 以下のコードは、作成された Book を Chapter インスタンスの Book プロパティに追加します (コメントを参照)。これは 1 対多の関係です (de モデルを参照)。Ember-Data は、Book インスタンスの関連レコードを自動的に入力するようです。これは Ember-Data の通常の動作ですか? Ember-Data に 1 対多の関係の関連する側を入力させるべきですか、それとも両側を指定して両方のインスタンスを保持するべきですか?
- 以下のコードの問題の 1 つは、I do not handle promises であると思われます。このコード:
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}),
});
ありがとう!