2015-07-21 61 views
0

如何在使用Ember Data創建記錄時創建嵌套/嵌入模型?具體來說,我想創建一個嵌套/嵌入式模型作者的後期模型。以下代碼給我錯誤:如何在Ember Data中創建嵌套/嵌入記錄的記錄?

處理路由時出錯:索引斷言失敗:您無法向'post.author'添加'未定義'記錄。您只能爲此關係添加「作者」記錄。錯誤:斷言失敗:您無法向'post.author'添加'未定義'記錄。您只能爲此關係添加「作者」記錄。

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.createRecord('post', { 
     title: 'My first post', 
     body: 'lorem ipsum ...', 
     author: { 
     fullname: 'John Doe', 
     dob: '12/25/1999' 
     } 
    }); 
    } 
}); 

App.Post = DS.Model.extend({ 
    title: DS.attr('string'), 
    body: DS.attr('string'), 
    author: DS.belongsTo('author') 
}); 

App.Author = DS.Model.extend({ 
    fullname: DS.attr('string'), 
    dob: DS.attr('string') 
}); 

有關如何做到這一點的任何想法?我還在JSBin上創建了一個演示:http://emberjs.jsbin.com/depiyugixo/edit?html,js,console,output

謝謝!

回答

2

需要將關係分配給實例化的模型,普通對象不起作用。

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.createRecord('post', { 
     title: 'My first post', 
     body: 'lorem ipsum ...', 
     author: this.store.createRecord('author', { 
     fullname: 'John Doe', 
     dob: '12/25/1999' 
     }) 
    }); 
    }