2014-07-16 33 views
1

我是Ember.js的新手,我想寫一個小頁面,在這裏我們可以發佈一些小的地位,人們可以爲它添加各種評論。模型定義如下以及數據如何在Ember數據中使用一對多關係(hasMany和belongssto)?

Posts.Post= DS.Model.extend({ 
    title: DS.attr('string'), 
    user: DS.attr('string', {defaultValue: 'post user'}), 
    comments: DS.hasMany('comment', {async: true}) 
}); 
Posts.Post.FIXTURES = [ 
{ 
    id: 1, 
    title: 'Learn Ember.js', 
    user: 'Post User 1', 
    comments: [1,2] 
} 
]; 

Posts.Comment= DS.Model.extend({ 
    title: DS.attr('string'), 
    user: DS.attr('string', {defaultValue: 'comment user'}), 
    post: DS.belongsTo('post') 
}); 
Posts.Comment.FIXTURES = [ 
{ 
    id: 1, 
    post_id: 1, 
    title: 'Learn Ember.js', 
    user: 'Comment User 1' 

}, 
{ 
    id: 2, 
    post_id: 1, 
    title: 'Post Item 2', 
    user: 'Comment User 2' 

}, 
]; 

我不確定路線和控制器,我需要能夠根據用戶的更新更新評論和帖子。

任何幫助表示讚賞。

在此先感謝。

回答

0

你有一個postpost_id之間的不匹配。下面的工作:

你樁模型:

Posts.Post= DS.Model.extend({ 
    title: DS.attr('string'), 
    user: DS.attr('string', { defaultValue: 'post user' }), 
    comments: DS.hasMany('comment', { async: true }) 
}); 

Posts.Post.reopenClass({ 
    FIXTURES = [ 
    { 
     id: 1, 
     title: 'Learn Ember.js', 
     user: 'Post User 1', 
     comments: [1,2] 
    } 
    ], 
}); 

您的評論模式:

Posts.Comment = DS.Model.extend({ 
    title: DS.attr('string'), 
    user: DS.attr('string', { defaultValue: 'comment user' }), 
    post: DS.belongsTo('post') 
}); 

Posts.Comment.reopenClass({ 
    FIXTURES = [ 
    { 
     id: 1, 
     post: 1, 
     title: 'Learn Ember.js', 
     user: 'Comment User 1' 
    }, 
    { 
     id: 2, 
     post: 1, 
     title: 'Post Item 2', 
     user: 'Comment User 2' 
    }, 
    ], 
}); 
+0

是否有使用標題,而不是ID的方式。 – Chaitanya

相關問題