gpt4 book ai didi

Ember.js pre.4、RESTAdapter 和 hasMany 关系

转载 作者:行者123 更新时间:2023-12-02 06:01:07 25 4
gpt4 key购买 nike

我正在尽最大努力在最新版本的 ember.js 和使用 RESTAdapter 的 ember-data 中找到和/或拼凑一个 hasMany/belongsTo 关系的工作 jsfiddle。到目前为止,我找到了一个 pre.4 baseline fiddle by @zgramana这使新路由器经过了一些探索和@sly7-7 fiddle它利用了必要的 DS 关系,但为简洁起见绕过了路由器。

我笨拙的 WIP 尝试将这些拼凑成一个有凝聚力的例子可以在这里找到:http://jsfiddle.net/W2dE4/5/ .我显然是 ember.js 的新手,这个 fiddle 充满了错误,所以请原谅缺乏技能。

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"}

);

如果有人能指出我正确的方向来让这个 fiddle 工作,或者链接到 pre.4 与 RESTAdapter 和 hasMany 关系的工作示例,我将永远感激你的慷慨。

非常感谢你!

最佳答案

你的 fiddle 只有几个语法问题。我在这里用工作版本更新了它: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 的 Handlebars 模板调用引用了错误的属性。

代替:
{{#each comment in post.comments}}
{{comment.description}}
{{/each}}

它应该是:
{{#each comment in content.comments}}
{{comment.description}}
{{/each}}

因为它是保存帖子数据的内容属性。

干杯!

关于Ember.js pre.4、RESTAdapter 和 hasMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com