2013-01-24 20 views
2

我正在盡我最大的努力找到和/或拼湊一個hasMany/belongsTo關係的工作jsfiddle在最新版本的ember.js和ember-data中利用RESTAdapter。到目前爲止,我發現了一個pre.4 baseline fiddle by @zgramana,它使新路由器通過一些探索和@sly7-7 fiddle,利用必要的DS關係,但爲了簡潔起見繞過路由器。Ember.js pre.4,RESTAdapter和擁有許多關係

我的摸索WIP試圖將這些拼湊成一個有凝聚力的例子,可以在這裏找到:http://jsfiddle.net/W2dE4/5/。我明顯是ember.js的新手,這個小提琴充滿了錯誤,所以請原諒缺乏技巧。

App.Store = DS.Store.extend({ 
    revision: 11, 
    adapter: DS.RESTAdapter.create({}) 
}); 

App.Post = DS.Model.extend({ 
    title: DS.attr('string'), 
    post: DS.attr('string'), 
    comments: DS.hasMany('App.Comment') 
}); 


App.Comment = DS.Model.extend({ 
    post: DS.belongsTo('App.Post'), 
    description: DS.attr('string') 
}); 

store = App.__container__.lookup('store:'); 

store.load(App.Post, { 
    id: 1, 
    title: 'Post 1 Title', 
    post: 'Body of post 1', 
    comments:[1,2] 
    }, 
     { 
    id: 2, 
    title: 'Post 2 Title', 
    post: 'text of post 2', 
    comments:[3,4] 
}, 
{ 
    id: 3, 
    title: 'Post 3 title', 
    post: 'text of post3', 
    comments:[5,6] 
} 
    ); 
store.load(App.Comment, {id: 1, description: "Great post!"}, 
     App.Comment, {id: 2, description: "Post sucks."}, 
     App.Comment, {id: 3, description: "Nice style"}, 
     App.Comment, {id: 4, description: "Horrible writing"}, 
     App.Comment, {id: 5, description: "Ember.js FTW"}, 
     App.Comment, {id: 6, description: "Get up get out n' get something"} 

    ); 

如果任何人都可以在正確的方向得到這個小提琴的工作,或者鏈接到與RESTAdapter和的hasMany關係pre.4的工作示例指向我,我將永遠感激你的慷慨。

謝謝親切!

+0

順便說一句,鏈接到@zgramana小提琴是http://jsfiddle.net/gramana/ZAvWR/ - 我只註冊了stackoverflow,所以我只能發佈最多兩個鏈接,直到我達到10聲望。乾杯! –

回答

4

只有少數語法問題與你的小提琴進行。我在此更新了一個工作版本:http://jsfiddle.net/W2dE4/6/

1)您沒有正確加載商店。要在同一個調用中加載多個項目,您需要使用loadMany,傳入模型類和數組。

所以不是:

store.load(App.Post, { 
    id: 1, 
    title: 'Post 1 Title', 
    post: 'Body of post 1', 
    comments:[1,2] 
}, 
{ 
    id: 2, 
    title: 'Post 2 Title', 
    post: 'text of post 2', 
    comments:[3,4] 
}, 
{ 
    id: 3, 
    title: 'Post 3 title', 
    post: 'text of post3', 
    comments:[5,6] 
}); 

store.load(App.Comment, {id: 1, description: "Great post!"}, 
    App.Comment, {id: 2, description: "Post sucks."}, 
    App.Comment, {id: 3, description: "Nice style"}, 
    App.Comment, {id: 4, description: "Horrible writing"}, 
    App.Comment, {id: 5, description: "Ember.js FTW"}, 
    App.Comment, {id: 6, description: "Get up get out n' get something"} 
); 

它應該是:

store.loadMany(App.Post, [ 
    { id: 1, title: 'Post 1 Title', post: 'Body of post 1', comments: [1,2] }, 
    { id: 2, title: 'Post 2 Title', post: 'text of post 2', comments: [3,4] }, 
    { id: 3, title: 'Post 3 title', post: 'text of post 3', comments: [5,6] } 
]); 

store.loadMany(App.Comment, [ 
    { id: 1, description: "Great post!" }, 
    { id: 2, description: "Post sucks." }, 
    { id: 3, description: "Nice style" }, 
    { id: 4, description: "Horrible writing" }, 
    { id: 5, description: "Ember.js FTW" }, 
    { id: 6, description: "Get up get out n' get something" } 
]); 

2)你的車把模板調用#each被引用了錯誤的性質。

相反的:

{{#each comment in post.comments}} 
    {{comment.description}} 
{{/each}} 

它應該是:

{{#each comment in content.comments}} 
    {{comment.description}} 
{{/each}} 

由於這是保存後的數據內容屬性。

乾杯!

+0

太棒了,非常感謝!我得到15個代表之前,我不能提出你的答案,但我將其標記爲答案。作爲一個便箋,你知道爲什麼點擊一次後,導航欄和下一步>>按鈕linkTo路由會中斷嗎?乾杯! –