[英]How to display a hasMany association
我是ember的新手,并且创建了这样的模型:
# app/models/skill.js
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
link: DS.attr('string'),
user: DS.hasMany('user')
});
我有这条路线:
# app/routes/skill.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('skill', params.id);
}
});
此路由器:
# app/router.js
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.resource('users', function() {
this.route('index', { path: '/'} );
this.route('signup');
});
this.resource('skills', function() {
this.route('index', { path: '/' });
});
this.resource('skill', { path: '/skills/:id' });
});
export default Router;
和这个模板:
# app/templates/skill.hbs
{{#link-to 'skills.index'}}Back{{/link-to}}
{{model.name}}
{{model.description}}
{{#each user in model.user}}
{{user.nickname}}
{{/each}}
我只想列出一个昵称的用户列表。 这是我的api的响应:
{
"skills": {
"id": 1,
"name": "Ember",
"description": "JS Framework",
"link": "emberjs.com"
},
"users": [
{
"id": 1,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T10:40:34.555Z",
"updated_at": "2015-06-21T10:40:34.555Z"
},
{
"id": 2,
"email": "test",
"nickname": "dougui",
"bio": "blabla",
"created_at": "2015-06-21T10:40:50.493Z",
"updated_at": "2015-06-21T10:40:50.493Z"
},
{
"id": 3,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T20:51:29.546Z",
"updated_at": "2015-06-21T20:51:29.546Z"
},
{
"id": 4,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T20:55:11.163Z",
"updated_at": "2015-06-21T20:55:11.163Z"
},
{
"id": 5,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T20:55:51.231Z",
"updated_at": "2015-06-21T20:55:51.231Z"
},
{
"id": 6,
"email": "email",
"nickname": "nickname",
"bio": null,
"created_at": "2015-06-22T00:01:47.089Z",
"updated_at": "2015-06-22T00:01:47.089Z"
}
]
}
这里有关于技能和用户的所有信息,但是什么也没有显示。
为什么我的列表没有显示?
谢谢!
现在,我的api正在发送此{"skill":{"id":2,"name":"Rails","description":"test","user_ids":[2]}}
。
我有这个模型:
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
link: DS.attr('string'),
users: DS.hasMany('users', { async: true })
});
我也尝试过:
users: DS.hasMany('users', { async: true })
我不知道该怎么办。 当前版本可能有示例吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.