Lets say I have two models, Book and Chapter.
App.Book = DS.Model.extend
name: DS.attr 'string'
chapters: DS.hasMany 'chapters', async: true
App.Chapter = DS.Model.extend
name: DS.attr 'string'
book: DS.belongsTo 'book', async: true
Using emberFire.
App.ApplicationAdapter = DS.FirebaseAdapter.extend
firebase: new Firebase('http://<my-firebase>.firebaseio.com/')
App.ApplicationSerializer = DS.FirebaseSerializer.extend()
According to the documentation on relationships, using the async: true option, Firebase would expect my data to look something like the following.
{
'books': [{
'book_id_1': {
'name': 'Example',
'chapters': [{
'chapter_id_1': true
}]
}],
'chapters': [{
'chapter_id_1': {
'name': 'Chapter 1'
}
}]
}
I can save a Chapter just fine, however I can't seem to get the chapters property of a Book populated with a newly created Chapter.
In the chrome console:
var store = App.__container__.lookup('store:main')
store.find('book', 'book_id_1').then(function(b) { window.book = b });
var chapter = store.createRecord('chapter', { name: 'Chapter 1' });
chapter.save().then(function() { // Saves the chapter
book.get('chapters').then(function(chapters) {
chapters.addObject(chapter);
book.save(); // Doesn't append to the chapters object
}
});
The above code does not save the chapters property on the book the way I would expect it to. Any help would be appreciated.
EDIT
It looks like the proper way to add the chapter object to the book.chapters property is:
book.get('chapters').then(function(chapters) {
chapters.addObject(chapter);
book.save().then(function() {
chapter.save();
});
});
But if there are validation errors on the chapter object, it will be added to the book.chapters property but won't be saved (creating an invalid reference). How do you get around this?