2013-03-01 104 views
1

我正在構建一個Ember.js應用程序,該應用程序允許2個實體之間的多對多關係,例如, Post and Tag:Ember.js:如何堅持多對多關係

App.Post = DS.Model.extend({ 
    title: DS.attr("string"), 
    body: DS.attr("string"), 
    tags: DS.hasMany("App.Tag") 
}); 

App.Tag = DS.Model.extend({ 
    name: DS.attr("string"), 
    posts: DS.hasMany("App.Post") 
}); 

我很難讓Ember在持久化新記錄時序列化多對多關係。這是我當前如何做:

// Create the post 
post = store.createRecord(App.Post, {title: "Example", body: "Lorem ipsum..."}); 

// Create the tag 
tag = store.createRecord(App.Tag, {name: "my-tag"}); 

// Add the tag to the post 
post.get("tags").addObject(tag); 

// Add the post to the tag 
tag.get("posts").addObject(post); 

// Save 
store.commit(); 

新的記錄在DOM中顯示出來,並貼到我的API,但是序列化不包括它們之間的關係。例如,帖子的序列化如下所示:

title=Example&body=Lorem+ipsum... 

我期望它也包含它已關聯的標籤。

我哪裏錯了?

+0

此使用真實的後端或只是燈具現在的問題是? – 2013-03-01 15:22:12

回答

1

默認情況下,如果您將關係配置爲嵌入到序列化程序中,則hasMany關係僅在您的JSON中序列化爲_ids數組。請參閱this answer瞭解更多詳情。