1

我对整个 Ember 的东西还很陌生,我真的很想获得更多真实场景​​的示例代码。我看过有关身份验证的 Embercast,这很有帮助,但我正在尝试创建一个应用程序,其中包含各种用户拥有的帖子列表,其他用户可以将其添加到他们的收藏夹中。

当然,这对我目前的水平来说是雄心勃勃的,但我希望这里的某个人能够为我指出一个例子,甚至创造一个例子。我认为这样的东西对其他人来说是一个极好的资源——比 Ember 指南中的 Todo 列表示例更重要,它真的不足以展示我如何创建一个真正的应用程序。我认为,我们需要更多关于身份验证的示例。

到目前为止,这是我的模型和固定装置:

App.User = DS.Model.extend({
    name: DS.attr('string'),
    email: DS.attr('string'),
    posts: DS.hasMany('post'),
    favourites: DS.hasMany('favourite')
});
App.User.FIXTURES = [{
    id: 1,
    name: 'Bob Jones',
    email: 'bob@jones.com',
    favourites: [2]
}];

App.Post = DS.Model.extend({
    date_posted: DS.attr('date'),
    title: DS.attr('string'),
    description: DS.attr('description'),
    comments: DS.hasMany('comment'),
});
App.Post.FIXTURES = [{
    id: 1,
    date_posted: new Date,
    title: 'Red',
    description: 'Great colour'
    comments: []
}, {
    id: 2,
    date_posted: new Date,
    title: 'Blue',
    description: 'Makes me sad',
    comments: [1]
}];

App.Comment = DS.Model.extend({
    post: DS.belongsTo('post'),
    date_posted: DS.attr('date'),
    author: DS.attr('string'),
    message: DS.attr('string'),
});
App.Comment.FIXTURES = [{
    id: 1,
    post: [2],
    date_posted: new Date,
    author: 'Aaron',
    message: 'I agree with the description.'
}];

App.Favourite = DS.Model.extend({
    user: DS.belongsTo('user'),
    post: DS.belongsTo('post')
});
App.Favourite.FIXTURES = [{
    user: 1,
    post: 2
}];

我觉得这是最简单的部分,我什至不能 100% 确信这些是正确组合在一起的。

感谢您对此的任何指导!

4

1 回答 1

0

您将在这里遇到几个问题。首先,您使用的是夹具,因此您应该使用 FixtureAdapter。由于您在夹具数据中定义了关系,但数据只有一个 id,因此您需要将这些关系标记为异步。此外,在您的用户夹具中,它有一个最喜欢的 id 2,但您最喜欢的夹具没有 id。

App.User = DS.Model.extend({
    name: DS.attr('string'),
    email: DS.attr('string'),
    posts: DS.hasMany('post', {async:true}),
    favourites: DS.hasMany('favourite', {async:true})
});
App.User.FIXTURES = [{
    id: 1,
    name: 'Bob Jones',
    email: 'bob@jones.com',
    favourites: [2],
    // you have no posts defined here??
}];

App.Post = DS.Model.extend({
    date_posted: DS.attr('date'),
    title: DS.attr('string'),
    description: DS.attr('description'),
    comments: DS.hasMany('comment', {async:true}),
});
App.Post.FIXTURES = [{
    id: 1,
    date_posted: new Date,
    title: 'Red',
    description: 'Great colour', // also missing this comma
    comments: []
}, {
    id: 2,
    date_posted: new Date,
    title: 'Blue',
    description: 'Makes me sad',
    comments: [1]
}];

App.Comment = DS.Model.extend({
    post: DS.belongsTo('post', {async:true}),
    date_posted: DS.attr('date'),
    author: DS.attr('string'),
    message: DS.attr('string'),
});
App.Comment.FIXTURES = [{
    id: 1,
    post: [2],
    date_posted: new Date,
    author: 'Aaron',
    message: 'I agree with the description.'
}];

App.Favourite = DS.Model.extend({
    user: DS.belongsTo('user', {async:true}),
    post: DS.belongsTo('post', {async:true})
});
App.Favourite.FIXTURES = [{
    id: 2, // it needs some sort of unique identifier
    user: 1,
    post: 2
}];

此外,您可能需要阅读 Ember 数据转换文档,这可能有助于解决您的一些问题,https://github.com/emberjs/data/blob/master/TRANSITION.md

这是我用你的数据制作的一个小例子,http ://emberjs.jsbin.com/OxIDiVU/204/edit

于 2014-02-16T07:13:59.313 回答