2015-01-14 117 views
0

問題:保存灰燼,數據記錄

  1. 這行代碼_activeAuthor.get('books').pushObject(book).save();沒有錯誤處理在Chrome,但書中沒有被添加到灰燼,數據的_activeAuthor實例的書籍屬性。我不明白爲什麼?
  2. 以下代碼將創建的Book添加到Chapter實例的Book屬性中(請參閱評論)。這是一種一對多的關係(參見de Models)。 Ember-Data似乎會自動填充Book實例上的相關記錄。這是Ember-Data的正常行爲嗎?我應該讓Ember-Data填充一對多關係的相關方,還是應該指定雙方並堅持這兩個實例?
  3. 我懷疑下面的代碼的問題之一是我沒有正確處理承諾。此代碼: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}), 
}); 

謝謝!

回答

1

1. author.get('books')會返回一個承諾,那麼可能是你想要做的是

author.get('books').then(function(books) { 
    books.pushObject(book) 
}); 
author.save(); 

如果這不是一個問題,你可以給與整個應用程序代碼的jsfiddle?然後,它會更容易幫助! :)

2. 每次getasync而不是isLoaded(不與服務器同步),燼會要求服務器和肯定,將填充在您的商店的記錄是一個期望的行爲模型的屬性:)

3. 如果你有一個async模型屬性,那麼你總會得到一個承諾,所以你應該處理它在例如這樣:

chapter.get('book').then(function(book) { 
    // here's a book 
}); 

BTW var latestAuthor = latestChapter.get('author'); - >chapter沒有author財產:)

+0

這是非常感謝。我現在有一個關於模型鉤子承諾的相關問題:http://stackoverflow.com/questions/27976746/how-to-handle-promises-with-models-hooks-of-nested-routes –