0
問題:保存灰燼,數據記錄
- 這行代碼
_activeAuthor.get('books').pushObject(book).save();
沒有錯誤處理在Chrome,但書中沒有被添加到灰燼,數據的_activeAuthor實例的書籍屬性。我不明白爲什麼? - 以下代碼將創建的Book添加到Chapter實例的Book屬性中(請參閱評論)。這是一種一對多的關係(參見de Models)。 Ember-Data似乎會自動填充Book實例上的相關記錄。這是Ember-Data的正常行爲嗎?我應該讓Ember-Data填充一對多關係的相關方,還是應該指定雙方並堅持這兩個實例?
- 我懷疑下面的代碼的問題之一是我沒有正確處理承諾。此代碼:
this.modelFor('user').get('latestChapter');
似乎返回一個承諾。我應該如何處理get()
promisses?
代碼:
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}),
});
謝謝!
這是非常感謝。我現在有一個關於模型鉤子承諾的相關問題:http://stackoverflow.com/questions/27976746/how-to-handle-promises-with-models-hooks-of-nested-routes –